MSP430FR5994 在 python 中与 PC 进行 UART 通信

MSP430FR5994 UART communication with PC in python

我尝试学习如何使用 UART,对于初学者,我只想将一些东西从微控制器发送到 PC。我用 this code files.

定位自己

微控制器:MSP430FR5994 LaunchPad 开发套件

UART:FTDI 电缆到引脚 2.6(UCA1RXD) 和 2.5(UCA1TXD)

微控制器是在 virtualbox Windows 机器上使用 CCS 编程的。 python 脚本是 运行,主 linux 系统上有 python3。

到目前为止我做了什么:

Python 接收脚本

import serial                                     # import pySerial module
ComPort = serial.Serial('/dev/ttyUSB0',timeout=1) # open ttyUSB0

ComPort.baudrate = 9600                           # set Baud rate
ComPort.bytesize = 8                    # Number of data bits = 8
ComPort.parity   = 'N'                  # No parity
ComPort.stopbits = 1                    # Number of Stop bits = 1

data = ComPort.read()               # Wait and read data from serial port
print(data)                             # print the received data

ComPort.close()                         # Close the COM Port

微控制器的 C 脚本

#include <msp430.h> 
void UART_init(void);

void main(void) {
  WDTCTL = WDTPW | WDTHOLD;   //Stop watchdog timer
  PM5CTL0 &= ~LOCKLPM5;

  UART_init();

  UCA1TXBUF = 'A';

}

void UART_init(void){
    P2SEL1 |= BIT5 + BIT6;              //Activate Pin for UART use
    P2SEL0 &= ~BIT5 + ~BIT6;            //Activate Pin for UART use

    UCA1CTLW0 |= UCSSEL_2;              //Select clock SMCLK

    UCA1BRW = 0x6;                      //Set Baud rate 9600 : UCA1BRW = INT(F_CPU/BAUD_soll) =         INT(1MHz/9600) = 104 with oversampling: 6
    UCA1MCTLW |= UCBRS5 + UCOS16 + UCBRF3;     //Modulation according to datasheet table: UCBRS = 0x20 = b100000 and UCOS16 = 1 and UCBRF = 8 = 0x8 = b1000

    UCA1CTLW0 &= ~UCSWRST;  //Reset UART module
 }

然后我刷入微控制器代码,然后 运行 python 代码。但我没有得到任何东西。在 1 秒超时后,我只得到 b''。

可能哪里出错了?没有沟通吗?针脚错了吗?因为我刚刚学习它,所以我真的不知道为什么它不起作用。也许你有一些建议:)

问候,straumle

到目前为止完成 所以我尝试了以下方法,但仍然无法正常工作:

进入低功耗模式

#include <msp430.h> 
void UART_init(void);

void main(void) {
  WDTCTL = WDTPW | WDTHOLD;   //Stop watchdog timer
  PM5CTL0 &= ~LOCKLPM5;

  UART_init();

  UCA1TXBUF = 'A';
  __bis_SR_register(LPM0_bits);

}

void UART_init(void){
    P2SEL1 |= BIT5 + BIT6;              //Activate Pin for UART use
    P2SEL0 &= ~BIT5 + ~BIT6;            //Activate Pin for UART use

    UCA1CTLW0 |= UCSSEL_2;              //Select clock SMCLK

    UCA1BRW = 0x6;                      //Set Baud rate 9600 : UCA1BRW = INT(F_CPU/BAUD_soll) =         INT(1MHz/9600) = 104 with oversampling: 6
    UCA1MCTLW |= UCBRS5 + UCOS16 + UCBRF3;     //Modulation according to datasheet table: UCBRS = 0x20 = b100000 and UCOS16 = 1 and UCBRF = 8 = 0x8 = b1000

    UCA1CTLW0 &= ~UCSWRST;  //Reset UART module
}

在进入和不进入低功耗模式的情况下都不会以 while(1) 循环退出

#include <msp430.h> 
void UART_init(void);

void main(void) {
  WDTCTL = WDTPW | WDTHOLD;   //Stop watchdog timer
  PM5CTL0 &= ~LOCKLPM5;

  UART_init();

  while(1){
  UCA1TXBUF = 'A';
  __bis_SR_register(LPM0_bits); // with and without the entering LPM0 mode
  }
}

void UART_init(void){
    P2SEL1 |= BIT5 + BIT6;              //Activate Pin for UART use
    P2SEL0 &= ~BIT5 + ~BIT6;            //Activate Pin for UART use

    UCA1CTLW0 |= UCSSEL_2;              //Select clock SMCLK

    UCA1BRW = 0x6;                      //Set Baud rate 9600 : UCA1BRW = INT(F_CPU/BAUD_soll) =         INT(1MHz/9600) = 104 with oversampling: 6
    UCA1MCTLW |= UCBRS5 + UCOS16 + UCBRF3;     //Modulation according to datasheet table: UCBRS = 0x20 = b100000 and UCOS16 = 1 and UCBRF = 8 = 0x8 = b1000

    UCA1CTLW0 &= ~UCSWRST;  //Reset UART module
 }

我也试过插入

 while(!(UCA1IFG & UCTXIFG));

在这两种情况下。 最后用中断也试了一下:

#include <msp430.h> 
void UART_init(void);

void main(void) {
  WDTCTL = WDTPW | WDTHOLD;   //Stop watchdog timer
  PM5CTL0 &= ~LOCKLPM5;

  UART_init();

  __bis_SR_register(LPM0_bits + GIE);

}

void UART_init(void){
    P2SEL1 |= BIT5 + BIT6;              //Activate Pin for UART use
    P2SEL0 &= ~BIT5 + ~BIT6;            //Activate Pin for UART use

    UCA1CTLW0 |= UCSSEL_2;              //Select clock SMCLK

    UCA1BRW = 0x6;                      //Set Baud rate 9600 : UCA1BRW = INT(F_CPU/BAUD_soll) =         INT(1MHz/9600) = 104 with oversampling: 6
    UCA1MCTLW |= UCBRS5 + UCOS16 + UCBRF3;     //Modulation according to datasheet table: UCBRS = 0x20 = b100000 and UCOS16 = 1 and UCBRF = 8 = 0x8 = b1000

    UCA1CTLW0 &= ~UCSWRST;  //Reset UART module
    UCA1IE |= UCTXIE;       //enable transmitting interrupts
}

#pragma vector= EUSCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void){
   while(!(UCA1IFG & UCTXIFG)); //with and without this line
   UCA1TXBUF = 'A';
}

微控制器启动时发送字节运行。如果稍后启动 Python 程序,则为时已晚。

而且你永远不应该从 main() return 因为没有操作系统可以 return 到;发生的事情是不确定的。要停止执行代码但保持 SMCLK 开启(UART 传输完成需要它),请输入 LPM0。

好的哇!我发现了我的错误...这是一个非常愚蠢的错误...下面这行让我想到了它:

另请记住,UART 连接在设备之间交叉。 MSP430 UART TX -> 外部设备 UART RX 和 MSP430 UART RX <- 外部设备 UART TX。

真的很抱歉!现在我已经切换了电缆,一切正常。噗有点尴尬。但是,嘿,它现在可以工作了:D

感谢@CL 的耐心解答。