uart - MSP430FR5994 UART 与 Python 中的 PC 通信
问题描述
我尝试学习如何使用 UART,对于初学者来说,我只想将一些东西从微控制器发送到 PC。我用这个代码文件定位自己。
微控制器: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''。
错误可能在哪里?没有交流吗?针脚错了吗?因为我只是在学习它,所以我真的不知道为什么它不起作用。也许你有一些建议:)
问候,
到目前为止 ,我尝试了以下方法,但仍然无法正常工作:
进入低功耗模式
#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';
}
解决方案
好哇!我发现了我的错误......这是一个非常愚蠢的错误......以下行让我想到了它:
还要记住,UART 连接在设备之间交叉。MSP430 UART TX -> 外部设备 UART RX 和 MSP430 UART RX <- 外部设备 UART TX。
真的对不起!现在我已经切换了电缆,一切正常。噗有点尴尬。但是,嘿,它现在可以工作了:D
感谢@CL 的回答和耐心。
推荐阅读
- ios - 将图像转换为 pdf 但不保存在文件中
- shell - 使用 shell 脚本读取 html 文件中的值
- r - 使用 R 估计 NetCDF 数据的每月气候学
- symfony - symfony 2.8 如何获取具有当前登录用户认证的多个扇区列表
- javascript - 错误:
属性 y1:预期长度,“{{iy}}”。错误显示在 chrome 控制台中,但 svg 工作正常 - javascript - Vuex 更新数组作为对象的值
- php - 带有 PHP 的 Youtube Scraper
- javascript - chrome 扩展代码来获取当前活动的标签 url 并检测其中的任何 url 更新
- java - 是否可以说服 Swing 缓存使用jTable 单元格中的标记加载的图像,而不是在每次重绘时重新下载图像?
- ibm-mq - 连接到云上的 IBM MQ 的问题