I try to learn how to use UART and for starters I want to just send something from the microcontroller to the PC. I orient myself with this code files.
Microcontroller: MSP430FR5994 LaunchPad Development Kit
UART: FTDI cable to Pins 2.6(UCA1RXD) and 2.5(UCA1TXD)
The microcontroller is programmed using CCS on a virtualbox Windows machine. The python script is run with python3 on the main linux system.
What I have done so far:
Python Script for Receiving
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 Script for Microcontroller
#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
}
Then I flash the microcontroller code and afterwards run the python code. But I do not get anything. After the timeout of 1s I just get b''.
Where could be the error? Is there no communication? Are the pins wrong? As I am just learning it, I am really clueless why it is not working. Maybe you have some suggestions :)
Regards, straumle
DONE SO FAR So I tried the following things and it is still not working:
enter low power mode
#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
}
and never exit with a while(1) loop with and without entering low power mode
#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
}
I also tried inserting
while(!(UCA1IFG & UCTXIFG));
in both scenarios. Finally, I also tried it with an interrupt:
#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';
}
Ok wow! I found my mistake... It is a really stupid one... The following line brought me up to it:
Also remember that UART connections cross from device to device. MSP430 UART TX -> External Device UART RX and MSP430 UART RX <- External Device UART TX.
I am really sorry! Everything is working fine now that I have switched the cables. Puh a little embarrassing. But hey, it works now :D
Thanks @CL for your answers and your patience.