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.

USCI UART



Hello! I'm trying to understand hardware UART for a project.

I have managed a print function, but I cannot, for the life of me, understand how to store received characters.

I have tried, with the help of another :

static volatile uint8_t cmd=0;
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
// save RXed char before sending it back..
cmd = UCA0RXBUF;
UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
}


It doesn't seem to work out at all.

Say I press a character on the keyboard, UCA0RXBUF is where it is stored, right?


SO in theory, I should be able to pull that up, right?
It doesn't seem to work.


This is for an MSP430 LaunchPad
  • This often-cited demo code is a demo for exactly what it demonstrates: echoing back a received character. It makes several implicit and uncommented assumptions that make it work for exactly this purpose and for nothing else.

    When the USCI receives a character from teh serial line, this character appears in RXBUF, right. That is, if it is received correctly. Else a parity or framing error is flagged.

    In the above code, when a character is received, the RX ISR is called. All you need to do inside this ISR is to 1) check which USCI was triggering the interrupt (if you simultaneously use USCIB0 for I2C or SPI) and then read the content of RXBUF into a global variable (and optionally set a global flag). Note that these global variables need to be declared volatile, or else the main code won't notice that the global variables have 'magically' changed. The volatile keyword forces the compiler to re-check the real global memory location instead of a register copy (which of course will never change).

    Never ever do a busy-waiting loop for a different interrupt flag inside an ISR, as the demo code does (and which is superfluous in this case too). Doing so would render the whole concepts of interrupts 8event-driven code execution) void.

  • Something like this.....

    pseudocode:

    "msp430.h"

    int myvar = 0;//initializing variable

    void main()

    {

    initializing the registers....

    port interrupts enabled...

    low_power_mode_enable()....

    interrupts_void()

    {

    myvar = RX_REGISTER_OF_THE_COM_USED

    }

    regards

    gastón

  • Gaston_Melo_Arg said:
    Something like this.....

    Myvar must be declared volatile. or else main probably won't notice the change.

    Also, it shoudl be noted that in thsi pseudocode, main enters LPM and the ISR should exit LPM when a new value arrived (so it can be processed by main). In this case, the fact that main was awakened mans a new byte has arrived in myvar, even if it should have the same value.
    If this implicite signalling cannto be used (e.g. no LPM or data is coming in while main is still busy) then an additional flag should be used to indicate the arrival of new data. (of course also declared volatile).

**Attention** This is a public forum