请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
部件号:MSP-EXP430FR2355 在UART TxD完成发送 字符串后发送了额外的NULL字符。
我将WFP 4.3 设置为我的UART TxD。 按下MSP-EXP430FR2355启动板上的S1按钮时,WFP 4.1 将触发中断,从而 启用UART IRQ。 逻辑 非常简单,我可以 在 串行终端中读取字符串OK (我正在发送一个简单的字符串"AB")。 但是当我检查示波器上的信号时,我注意到在字符串后面插入了两个NULL字符。
为什么会发生这种情况,以及如何清除这些NULL?
#include <msp430.h>
char message[] = "AB";
unsigned int pos;
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
UCA1CTLW0 |= UCSWRST; // put eUSCI_A1 into SW reset
UCA1CTLW0 |= UCSSEL__SMCLK; // clock source: use SMCLK 1MHz
UCA1BRW = 8; // (from table) 115200 baud
UCA1MCTLW |= 0xD600; // (from table) 115200 baud
P4DIR &= ~BIT1; // P4.1 (SW1) clear bit to set as input
P4REN |= BIT1; // P4.1 (SW1) enable pull-up/down resistor
P4OUT |= BIT1; // P4.1 (SW1) pull-up
P4IES |= BIT1; // P4.1 (SW1) interrupt edge select, high-to-low transition (SW is pressed)
P4SEL1 &= ~BIT3; // P4.3 {UART} TxD
P4SEL0 |= BIT3; // P4.3 (UART) TxD
PM5CTL0 &= ~LOCKLPM5; // get out from LPM
UCA1CTLW0 &= ~UCSWRST; // take eUSCI_A1 out of SW reset (activate)
P4IFG &= ~BIT1; // P4.1 (SW1) clear interrupt flag
P4IE |= BIT1; // P4.1 (SW1) enable interrupt
__enable_interrupt();
while(1)
{
// do nothing
}
return 0;
}
#pragma vector = PORT4_VECTOR // triggered when SW1 is pressed
__interrupt void ISR_PORT4_S1(void)
{
pos = 0; // when SW1 is pressed, initialize string index
UCA1IE |= UCTXCPTIE; // P4.3 (UART) enable IRQ
UCA1IFG &= ~UCTXCPTIFG; // P4.3 (UART) clear IFG
UCA1TXBUF = message[pos]; // P4.3 (UART) put the first character in Tx buffer ... buffer starts shifting out
P4IFG &= ~BIT1; // clear P4.1 (SW1) IFG
}
# pragma vector = EUSCI_A1_VECTOR // triggered right after the first character is sent
__interrupt void ISR_EUSCI_A1(void)
{
if(pos == sizeof(message)) // if all characters in a string is sent
{
UCA1IE &= ~UCTXCPTIE; // P4.3 (UART) disable IRQ
}
else // more characters to be sent
{
pos++;
UCA1TXBUF = message[pos];
}
UCA1IFG &= ~UCTXCPTIFG; // P4.3 (UART) clear IFG
}
