我正在为MSP430FR5994微控制器上的嵌入式UART发射机编写一些代码。我对这个UART发射机的计划是最终让它从一个来自不同电压的ADC获取int。但是在我谈到这一点之前,我遇到了一些UART传输的问题。
下面的代码是将int设置为消息,配置UART设置,然后在底部的switch...case操作中通过UART传输缓冲区发送消息。我能够通过RS232终端访问消息(我使用了Termite)。
#include <msp430.h>
int message = 65;
const unsigned char messageLength = sizeof(message);
unsigned char TXbytes = 0;
int main(void)
{
/*There was some code here about configuring other parts of the MCU. There were pin outputs,
clock settings, and a watchdog timer. I have removed them for your sake
but please let me know if you want them back.*/
// Configure USCI_A0 for UART mode
UCA0CTLW0 = UCSWRST; // Put eUSCI in reset (p788)
UCA0CTLW0 |= UCSSEL__SMCLK; // CLK = SMCLK
// Baud Rate calculation for 19200
// 8000000/(16*19200) = 26.042
// Fractional portion = 0.042
// User's Guide Table 21-4: UCBRSx = 0xD6
// UCBRFx = int ( (0.042)*16) = 1
UCA0BRW = 26; // 8000000/16/19200, p789
UCA0MCTLW |= UCOS16 | UCBRF_1 | 0xD600; // UCOS16 = Oversampling enable, used when high frequency clk is used, probably divides everything by 16, UCBRF = fine turner when UCOS16 is active.
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
while(1){
__delay_cycles(200000);
UCA0IE |= UCTXIE; //Flag for transmission is enabled
__bis_SR_register(LPM3_bits | GIE); // Enter LPM3, interrupts enabled
__no_operation(); // For debugger
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=EUSCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(EUSCI_A0_VECTOR))) USCI_A0_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
{
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
break;
case USCI_UART_UCTXIFG: //TXIFG = transmission flag, needs manual activation
// Transmit the byte
//UCA0TXBUF is taking message and sending it over.
UCA0TXBUF = message; //TXBUF = Transmit Buffer, p791
// If last byte sent, disable the interrupt
if(TXbytes == messageLength)
{
UCA0IE &= ~UCTXIE;
TXbytes = 0;
}
__bic_SR_register_on_exit(LPM3_bits);
break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG: break;
default: break;
}
}然而,我正在访问的信息与我所期望的不一样。我希望收到的信息是"65",而不是我收到的"A“。考虑到ASCII中的65 =A,这有点道理,但我希望终端打印"65",而不是"A“。我不知道该怎么做。

将char[]消息放置到UART上,如下面所示,仍然有效。
char message[] = "Help!\n";

如果需要更多的信息,请告诉我。或者如果有什么明显的事情我错过了。
发布于 2022-04-11 13:38:55
message声明为字符数组。message[TXbytes]。要使这一方法有效,TXbytes实际上必须在每次访问ISR时都增加。volatile,以防止危险的优化。如果这只是一个快速和肮脏的程序,它可能更容易跳过中断,只发送数据繁忙-等待轮询tx标志。
https://stackoverflow.com/questions/71828689
复制相似问题