Jump to content

Serial return problem

Recommended Posts



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 :D


#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
 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
 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')

Link to post
Share on other sites

__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.



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.



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..




*EDIT*: I see you answered your own problem.. Though you didn't mention the pin toggling.. Hope you get that sorted yourself too..

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...