areben 4 Posted April 7, 2012 Share Posted April 7, 2012 Hello I'm sure this is obvious, but I used the example code for hardware serial for the MSP430 My problem is: The code never runs my while(1) The code returns "Hello World" when I send u The code does not toggle the pin when I send c Tips appreciated #include "msp430g2553.h" #define BEEP_PIN BIT0 const char string1[] = { "Hello World\r\n" }; unsigned int charLoop; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz 9600 UCA0BR1 = 0; // 1MHz 9600 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt //set outputs P1DIR |= BEEP_PIN; P1OUT = 0x00; //clear port 1 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled while (1) { P1OUT ^= BEEP_PIN; // Toggle LED on P1.0 __delay_cycles(1000000); // Wait ~100ms at default DCO of ~1MHz } } #pragma vector=USCIAB0TX_VECTOR __interrupt void USCI0TX_ISR(void) { UCA0TXBUF = string1[charLoop++]; // TX next character if (charLoop == sizeof string1 - 1) // TX over? IE2 &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt } #pragma vector=USCIAB0RX_VECTOR __interrupt void USCI0RX_ISR(void) { if (UCA0RXBUF == 'u') // 'u' received? { charLoop = 0; IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt UCA0TXBUF = string1[charLoop++]; } else if (UCA0RXBUF == 'c') { P1OUT &= BEEP_PIN; __delay_cycles(100000); P1OUT &= ~BEEP_PIN; } } Quote Link to post Share on other sites
areben 4 Posted April 7, 2012 Author Share Posted April 7, 2012 Ok, Solved my own problem in the example code __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled puts the CPU to sleep changing it to __bis_SR_register(GIE); worked Quote Link to post Share on other sites
DanAndDusty 62 Posted April 7, 2012 Share Posted April 7, 2012 __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled This puts the 430 into low power mode 0. The CPU clock is off.. So any code after this will never get reached unless the CPUOFF bits in the SR are cleared inside one of your interrupt handlers. The code for this is __bic_sr_register_on_exit(LPM0_bits). Basically this says "when this handler exits clear the bits for LMP0". This will enable your CPU clock again. Alternatively you can change the line to read __bis_SR_register(GIE); which will enable the interrupt handlers but won't switch off the CPU clock. P1OUT &= BEEP_PIN; This line won't turn on your BEEP_PIN. Infact it will turn off EVERY OTHER pin and leave BEEP_PIN alone. You need to research binary maths. &= says "turn on bits ONLY where both sides are a 1".. Anding 0b11110000 with 0b10101010 will result in 0b10100000. Only where both bits are 1 is the result a 1. You meant to have P1OUT |= BEEP_PIN; |= is an or opperation and says "Set the bit to 1 where either of the comparison bits is 1".. so Oring 0b11110000 with 0b10101010 will result in 0b11111010. P1OUT &= ~BEEP_PIN; This line is correct. Basically it says "Set BEEP_PIN to 0 and leave everything as it was".. Basically its an AND opperation (&=) and the ~ means toggle each bit.. so you are ANDing with a value with every bit set except BEEP_PIN. Reading back through this it doesn't make as good sense as Id have hoped it would. However luckily ZeroSkillz did a good tutorial Here which makes for good reading. Hope this helps.. Dan *EDIT*: I see you answered your own problem.. Though you didn't mention the pin toggling.. Hope you get that sorted yourself too.. areben 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.