【发布时间】:2021-04-28 09:34:47
【问题描述】:
我有以下代码可以正常运行且没有错误: 现在我的主要目标是,我想向终端发送一个字符串而不是一个字符(在线:if (rcv == 'a')),我将 rcv 变量的类型从 unsigned char 更改为 char rcv [] 但无济于事。
而且我也不知道如何正确地将字符串重置为 0(在 rcv = 0x00 行)。 发送字符串以取回字符串的最佳或最简单的解决方案是什么? 我会非常感谢大家的每一个帮助! 提前致谢
// ----------------- 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;
}
【问题讨论】: