首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未按预期发送值的UART发射机

未按预期发送值的UART发射机
EN

Stack Overflow用户
提问于 2022-04-11 13:26:46
回答 1查看 127关注 0票数 0

我正在为MSP430FR5994微控制器上的嵌入式UART发射机编写一些代码。我对这个UART发射机的计划是最终让它从一个来自不同电压的ADC获取int。但是在我谈到这一点之前,我遇到了一些UART传输的问题。

下面的代码是将int设置为消息,配置UART设置,然后在底部的switch...case操作中通过UART传输缓冲区发送消息。我能够通过RS232终端访问消息(我使用了Termite)。

代码语言:javascript
复制
#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上,如下面所示,仍然有效。

代码语言:javascript
复制
char message[] = "Help!\n";

如果需要更多的信息,请告诉我。或者如果有什么明显的事情我错过了。

EN

回答 1

Stack Overflow用户

发布于 2022-04-11 13:38:55

  • message声明为字符数组。
  • 如果某些数据应在PC终端上打印为ASCII数字,则将每个数字隐藏到相应的ASCII字符,并将其存储为数组中的字符串(可选为CR+LF ),可选终止为null。
  • 在ISR中发送message[TXbytes]。要使这一方法有效,TXbytes实际上必须在每次访问ISR时都增加。
  • 将所有变量与ISR共享为volatile,以防止危险的优化。
  • 保护与ISR共享的所有变量不受竞争条件的影响,这样在ISR运行时主程序不会写入这些变量。

如果这只是一个快速和肮脏的程序,它可能更容易跳过中断,只发送数据繁忙-等待轮询tx标志。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71828689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档