简体   繁体   中英

How can I send a string instead of a character through Serial with the MSP430? ( Code Composer Studio(C Language))

I have the following code that functions properly and without mistakes: Now my main goal is that, I want to send instead of a character(on the line: if (rcv == 'a')) a string to the terminal, i changed the type of the rcv variable from unsigned char to char rcv[] but no avail.

And I also do not know how to reset the string to 0 properly (on the line rcv = 0x00). What is the best or the easiest solution to send a string in order to get a string back? I would be really grateful for every help from everyone! Thanks in Advance

// ----------------- Gl. Variablen --------------------------------------------- 

unsigned char rcv;                                                              // Global variable für empfangene Daten per IR aus UART UCA0

// ------------------ PROTOTYPEN -----------------------------------------------

void UART_init(void);                                                          
void UART_send_string(char* str);                                               
__interrupt void UART_receive_ISR(void);                                       

// -------------------- MAIN ---------------------------------------------------

void main( void )
{
   WDTCTL = WDTPW + WDTHOLD;                                                    
   
   DCOCTL = 0;                                                                
   BCSCTL1 = CALBC1_1MHZ;                                                       
   DCOCTL = CALDCO_1MHZ;                                                        // .-
   
   UART_init();                                                                
   _EINT();                                                                     
   
   while (1)                                                                   
   { 
     if (rcv == 'a')                                                                 
{
       rcv = 0x00;                                                        
       UART_send_string("Hello world");                                         
     } // if
   } // while
}

// ------------------ FUNKTIONEN -----------------------------------------------

//
// Initialisiert das UART-Modul UCA0 des MSP430G2553. Dazu werden Pins 1.1 und 
// 1.2 für TX und RX eingestellt. Baudrate 9600 bei einer Frequenz von 1MHz.
//
void UART_init(void)
{
  P1SEL = BIT1 + BIT2 ;                                                       
  P1SEL2 = BIT1 + BIT2 ;                                                        // .-

  UCA0CTL1 |= UCSSEL_2;                                                         // SMCLK
  UCA0BR0 = 104;                                                                // 1MHz 9600
  UCA0BR1 = 0;                                                                  // 1MHz 9600
  UCA0MCTL = UCBRS0;                                                            // Modulation UCBRSx = 1
  UCA0CTL1 &= ~UCSWRST;                                                      
  
  UC0IE |= UCA0RXIE;                                                           
}

//
// Sende einen String über UART-Modul UCA0 des MSP430G2553. 
//
void UART_send_string(char* str)
{
  while (*str != 0)                          
  {
    while (!(IFG2 & UCA0TXIFG));                         
    UCA0TXBUF = *str++;                                                       
  } // while
}

//
// 
// MSP430G2553.
//
#pragma vector=USCIAB0RX_VECTOR
__interrupt void UART_receive_ISR(void)
{
  while (!(IFG2&UCA0RXIFG));                                                  
  rcv = UCA0RXBUF;                                                              
}

There are some fundamental errors with this code that you need to address before you can get down to the actual functionality. You have the classic and very common bug where the variable shared with the ISR isn't protected nor volatile qualified. There is no guarantee that the compiler generates atomic access nor that it generates correct code for accessing the rcv variable. More info in this answer here: Using volatile in embedded C development .

Once you understand how to do that, you can swap the single character for a ring buffer. Either implemented as a simple array or as a stand-alone "ADT". You can then either fill this up with one interrupt per byte, or by triggering one interrupt for the first byte, then poll the UART rx registers for all pending ones, given that you expect a certain UART protocol.

Modern MCUs uses DMA instead of manual interrupts but I'm not sure if that's an option here. It offloads a lot of intensive interrupt work from the CPU so it's often preferred, even though it might come with various quirks of its own.

For a simple program that doesn't do much else but UART communication, you don't really need interrupts in the first place, but could probably use polling from the loop in main(). That is the easiest by far but also doesn't allow your program to do much else, since it must be fast enough to complete all work in a 10 bit UART frame cycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM