This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] MSP430FR5994:使用UART传输不同的int

Guru**** 2539500 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/1092154/msp430fr5994-transmitting-a-varying-int-with-uart

部件号:MSP430FR5994

你(们)好 我正在尝试使用 MSP430FR5994通过UART传输从1到100的序列。 我无法这样做,因为我的代码似乎不接受变量值。

我的代码:

#include <msp430.h>

char RXbuffer[32];
const unsigned char maxRXbytes = sizeof(RXbuffer);
unsigned char RXbytes = 0;
int i = 0;

char message[] = i;
const unsigned char messageLength = sizeof(message);
unsigned char TXbytes = 0;

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;               // Stop Watchdog


    // Configure GPIO
    P2SEL0 &= ~(BIT0 | BIT1);
    P2SEL1 |= (BIT0 | BIT1);                // USCI_A0 UART operation (p93_s)

    // Disable the GPIO power-on default high-impedance mode to activate
    // previously configured port settings
    PM5CTL0 &= ~LOCKLPM5;

    // Startup clock system with max DCO setting ~8MHz
    CSCTL0_H = CSKEY_H;                       // Unlock CS registers
    CSCTL1 = DCOFSEL_3 | DCORSEL;             // Set DCO to 8MHz
    CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK;
    CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1;     // Set all dividers
    CSCTL0_H = 0;                             // Lock CS registers

    // 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 ( (52.083-52)*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
                                            // 0xD600 is for first 8 bits,
    UCA0CTLW0 &= ~UCSWRST;                  // Initialize eUSCI
    UCA0IE |= UCRXIE;                       // Enable USCI_A0 RX interrupt
    while(1){
    __delay_cycles(20000);
    UCA0IE |= UCTXIE;
    i++;
    __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 = UCA0RXBUF;
          UCA0TXBUF = message[TXbytes++];   //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;
    }
}

如果需要更多信息来解决此问题,请告诉我;我对此非常陌生。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我不知道您认为这是在做什么:

    char message[] = i;
    const unsigned char messageLength = sizeof(message);

    更有用的是char message[]="这是一个测试。\r\n";因为它有一个实际的数据字符串要发送。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我对该代码的尝试是,每次发送UART时,我都会增加其值。 所以在UART上,我将收到“1”,“2”,“3”,“4”等...
    我可能对UART的工作方式有一些误解。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您不了解C初始化程序的工作方式。 在main()开始前正好一次。

    或者当函数启动时(如果函数用于自动变量)。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我已经修改了我的代码;现在我要使用UART打印int,然后再改变UART。 这就是我目前为止所拥有的。

    #include <msp430.h>
    
    
    int message = 65;
    //const char message2[] = "/n";
    const unsigned char messageLength = sizeof(message);
    
    unsigned char TXbytes = 0;
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;               // Stop Watchdog
    
    
        // Configure GPIO
        P2SEL0 &= ~(BIT0 | BIT1);
        P2SEL1 |= (BIT0 | BIT1);                // USCI_A0 UART operation (p93_s)
    
        // Disable the GPIO power-on default high-impedance mode to activate
        // previously configured port settings
        PM5CTL0 &= ~LOCKLPM5;
    
        // Startup clock system with max DCO setting ~8MHz
        CSCTL0_H = CSKEY_H;                       // Unlock CS registers
        CSCTL1 = DCOFSEL_3 | DCORSEL;             // Set DCO to 8MHz
        CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK;
        CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1;     // Set all dividers
        CSCTL0_H = 0;                             // Lock CS registers
    
        // 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 ( (52.083-52)*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
                                                // 0xD600 is for first 8 bits,
        UCA0CTLW0 &= ~UCSWRST;                  // Initialize eUSCI
        UCA0IE |= UCRXIE;                       // Enable USCI_A0 RX interrupt
        while(1){
        __delay_cycles(200000);
        UCA0IE |= UCTXIE;
        //i++;
        __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 char message.
              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;
        }
    }
    

    现在,我的UART终端(我正在使用术语)从int打印"A"。 这是有意义的,65表示ASCII代码中的。 问题是我想打印"65",而不是"A"。 有哪些方法可以做到这一点? 我找到 了有类似问题的人 ,但答案不完整。

    我想这与我对"UCA0TXBUF =消息"的确切工作方式的误解有关。 如果使用ASCII代码,当我给消息的值超过127时会发生什么情况? 由于UART是8位的,它是否只是不传输消息? 还是拆分邮件?

    如果需要更多信息来解决此问题,请告诉我。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    en.wikipedia.org/.../The_C_Programming_Language