我试着学习如何使用UART,对于初学者,我只想把一些东西从微控制器发送到PC。我用这个代码文件给自己定位。
微控制器: MSP430FR5994 LaunchPad开发工具包
UART: FTDI电缆到Pins 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代码。但我什么都没得到。在1s超时之后,我只得到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
}永远不要在没有进入低功耗模式的情况下退出。
#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';
}发布于 2020-02-03 12:38:34
好哇!我发现我的错误..。这真的很蠢..。下面这句话让我明白了这一点:
还请记住,UART连接从一个设备交叉到另一个设备。MSP430 UART TX ->外部设备UART RX和MSP430 UART RX <-外部设备UART TX。
我真的很抱歉!现在我换好了电缆,一切都很好。有点尴尬。但是,现在起作用了:D
谢谢你的回答和耐心。
发布于 2020-01-31 16:36:09
该字节是在微控制器开始运行时发送的。如果您稍后启动Python程序,则为时已晚。
您不应该从main()返回,因为没有可返回的操作系统;所发生的事情是未定义的。若要停止执行代码但保留SMCLK (您需要完成UART传输),请输入LPM0。
https://stackoverflow.com/questions/60005647
复制相似问题