This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553: Problem writing through the UART when running from a timer callback

Part Number: MSP430G2553


Hello, I'm starting to learn how to program the msp430g2553 with the launchPad. I have my own library to write through the UART. This works correctly. On the other hand, I'm using this timer library:

Both libraries work properly separately but when I try to use both I have problems and I do not know how to locate the error. As you can see in the main code, I initially write through the UART 'Hello world' and I launch two timers. The first timer blinks the green led every second and the second timer writes through the UART 'bye' every five seconds. The first five blinks work correctly but when the second timer runs 'timeToGo' callback, the green led stops blinking and nothing is written through the UART. I think it's because the execution thread stops in the 'while' of the 'uartWriteChar' method that checks the writing flag of the UART. Why is this happening? Do you think it is for another reason?Thanks in advance.

Main Code:

int timer_handle = -1;

static void blinkGreenLED(void *arg){

    (void) arg;
    P1OUT ^= BIT6;  // Toggle P1.6

}

static void timeToGo(void *arg){

    (void) arg;
    //P1OUT ^= BIT0;  // Toggle P1.0
    uartWriteString((char *)"Bye\n\r");
}

int main(void) {


    WDTCTL = WDTPW + WDTHOLD;           //Stop WDT
    BCSCTL1 = CALBC1_1MHZ;              //Set DCO to 1Mhz
    DCOCTL = CALDCO_1MHZ;               //Set DCO to 1Mhz

    uartInit();                        //Initialize the UART connection

     __enable_interrupt();               //Interrupts Enabled

    uartWriteString((char *)"Hello World\n\r");

    P1OUT &= 0x00;               // Shut down everything
    P1DIR &= 0x00;
    P1DIR |= BIT0 + BIT6;            // P1.0 and P1.6 pins output the rest are input

    timer_init();

    timer_handle = timer_create(500, 1, blinkGreenLED, 0);
    timer_handle = timer_create(5000, 1, timeToGo, 0);

    _bis_SR_register(GIE);

    while(1){}

}

uart.c Lib:

#define LED BIT0
#define RXD BIT1
#define TXD BIT2

volatile unsigned int txFlag;          //Mailbox Flag for the tx_char.
volatile unsigned char txChar;         //This char is the most current char to go into the UART

/*uartInit
* Sets up the UART interface via USCI
*/
void uartInit(void)
{
    P1SEL = RXD + TXD;                  //Setup the I/O
    P1SEL2 = RXD + TXD;

    P1DIR |= LED;                       //P1.0 red LED. Toggle when char received.
    P1OUT |= LED;                       //LED off

    UCA0CTL1 |= UCSSEL_2;               //SMCLK
                                        //1,000,000Hz, 9600Baud, UCBRx=6, UCBRSx=0, UCBRFx=1
    UCA0BR0 = 6;                        //8MHz, OSC16, 9600
    UCA0BR1 = 0;                        //((1MHz/9600)/16)
    UCA0MCTL = UCBRF3 + UCOS16;             //UCBRFx=1,UCBRSx=0, UCOS16=1
    UCA0CTL1 &= ~UCSWRST;               //USCI state machine
    IE2 |= UCA0RXIE;                    //Enable USCI_A0 RX interrupt

    rxFlag = 0;                        //Set rxFlag to 0
    txFlag = 0;                        //Set txFlag to 0

    return;
}
 
/*uartWriteChar
* Sends a char to the UART. Will wait if the UART is busy
*/
void uartWriteChar(unsigned char c)
{
    txChar = c;                        //Put the char into the tx_char
    IE2 |= UCA0TXIE;                    //Enable USCI_A0 TX interrupt
    while(txFlag == 1);                //Have to wait for the TX buffer
    txFlag = 1;                        //Reset the txFlag
    return;
}

/*uartWriteString
* Sends a string to the UART. Will wait if the UART is busy
*/
void uartWriteString(char *str)               //Sends a String to the UART.
{
     while(*str) uartWriteChar(*str++);     //Advance though string till end
     return;
}

#pragma vector = USCIAB0TX_VECTOR       //UART TX USCI Interrupt
__interrupt void USCI0TX_ISR(void)
{
    UCA0TXBUF = txChar;                //Copy char to the TX Buffer
    txFlag = 0;                        //ACK the txFlag
    IE2 &= ~UCA0TXIE;                   //Turn off the interrupt to save CPU
}

 

**Attention** This is a public forum