简体   繁体   English

如何使用 MSP430 通过串行发送字符串而不是字符? ( Code Composer Studio(C 语言))

[英]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.我有以下代码可以正常运行且没有错误:现在我的主要目标是,我想向终端发送一个字符串而不是一个字符(在线:if (rcv == 'a')) 一个字符串,我改变了rcv 变量的类型从 unsigned char 到 char rcv[] 但无济于事。

And I also do not know how to reset the string to 0 properly (on the line rcv = 0x00).而且我也不知道如何正确地将字符串重置为 0(在 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.您有一个经典且非常常见的错误,其中与 ISR 共享的变量不受保护,也不受volatile限制。 There is no guarantee that the compiler generates atomic access nor that it generates correct code for accessing the rcv variable.不能保证编译器会生成原子访问,也不能保证生成正确的代码来访问rcv变量。 More info in this answer here: Using volatile in embedded C development .此答案中的更多信息:在嵌入式 C 开发中使用易失性

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".要么作为一个简单的数组实现,要么作为一个独立的“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.然后,您可以用每个字节一个中断来填充它,或者通过为第一个字节触发一个中断,然后轮询 UART rx 寄存器以查找所有未决的寄存器,假设您期望某个 UART 协议。

Modern MCUs uses DMA instead of manual interrupts but I'm not sure if that's an option here.现代 MCU 使用 DMA 而不是手动中断,但我不确定这是否是一个选项。 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.它从 CPU 中卸载了大量密集的中断工作,因此它通常是首选,即使它可能带有自己的各种怪癖。

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().对于一个除了 UART 通信之外不做太多其他事情的简单程序,您实际上并不需要中断,但可能可以使用 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.这是迄今为止最简单的方法,但也不允许您的程序做很多其他事情,因为它必须足够快才能在 10 位 UART 帧周期内完成所有工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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