Hello,
I have a problem with the MSP430FR6047 for communication with pc using uart. I'm trying to send data with different lenght and echo it.
for example i'm sending command = bytes([0x04, 0x81, 0x01, 0xFF, 0x2C, 0x01, 0x01]) as komand: b'\x04\x81\x01\xff,\x01\x01' and the recieved data is wrong Response: b'\x04\x81\x01,\x01\x01'.
but when i send only 2 byte and echo the same 2 byte, it works without problem. what am i doing wrong? I'm sending the command using python. below is my code for sending and recieving using python and the code in MCU.
import serial
import time
# Define serial port settings
com_port = 'COM3'
baud_rate = 115200
Data_bits = 8
parity = 'N'
stop_bits = 1
# Open serial port
ser = serial.Serial(port=com_port, baudrate=baud_rate, bytesize=Data_bits, parity=parity, stopbits=stop_bits, timeout=0.5)
def read_response():
response = ser.read(7) # Read 7 bytes (packet size)
return response
def send_command():
command = bytes([0x04, 0x81, 0x01, 0xFF, 0x2C, 0x01, 0x01])
print("komand:", command)
print("len:", len(command))
# Send command
ser.write(command)
time.sleep(0.5)
while True:
# Send command
send_command()
# Read response
response = read_response()
# Print response
print("Response:", response)
code for MCU
#include <msp430.h>
#include <stdint.h>
//******************************************************************************
// Pin Config ******************************************************************
//******************************************************************************
static void UART_sendByte(uint8_t byte);
#define CENTER_ID 0x04
#define COMMAND_ID 0x80
#define WRITE_CMD 0x01
#define READ_CMD 0x00
#define DATA_SIZE 2
volatile uint16_t received_data = 0;
//******************************************************************************
// UART Initialization *********************************************************
//******************************************************************************
#define SMCLK_115200 0
#define SMCLK_9600 1
#define ACLK_9600 2
#define SMCLK_19200 3
//#define UART_MODE ACLK_9600//SMCLK_115200//
#define UART_MODE SMCLK_115200//
void initUART()
{
// Configure USCI_A0 for UART mode
UCA0CTLW0 = UCSWRST; // Put eUSCI in reset
#if UART_MODE == SMCLK_115200
UCA0CTLW0 |= UCSSEL__SMCLK; // CLK = SMCLK
// Baud Rate Setting
// Use Table 30-5 in Family User Guide
UCA0BR0 = 8;
UCA0BR1 = 0;
UCA0MCTLW |= UCOS16 | UCBRF_10 | 0xF700; //0xF700 is UCBRSx = 0xF7
//UCA0MCTLW |= UCOS16 | UCBRF_5 | 0xD600; //0xF700 is UCBRSx = 0xF7
#elif UART_MODE == SMCLK_9600
UCA0CTLW0 |= UCSSEL__SMCLK; // CLK = SMCLK
// Baud Rate Setting
// Use Table 30-5 in Family User Guide
UCA0BR0 = 3;
UCA0BR1 = 0;
UCA0MCTLW |= UCOS16 | UCBRF_8 | 0xD600; //0xD600 is UCBRSx = 0xD6
#elif UART_MODE == ACLK_9600
UCA0CTLW0 |= UCSSEL__ACLK; // CLK = ACLK
// Baud Rate calculation
// 32768/(9600) = 3.4133
// Fractional portion = 0.4133
// Use Table 24-5 in Family User Guide
UCA0BR0 = 3; // 32768/9600
UCA0BR1 = 0;
UCA0MCTLW |= 0x9200; //0x9200 is UCBRSx = 0x92
#elif UART_MODE == SMCLK_19200
UCA0CTLW0 |= UCSSEL__SMCLK; // CLK = SMCLK
// Baud Rate Setting
// Use Table 30-5 in Family User Guide
UCA0BR0 = 3;
UCA0BR1 = 0;
UCA0MCTLW |= UCOS16 | UCBRF_4 | 0x0200; //0xF700 is UCBRSx = 0xF7
#else
# error "Please specify baud rate to 115200 or 9600"
#endif
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
//******************************************************************************
// Device Initialization *******************************************************
//******************************************************************************
void initGPIO()
{
// Configure UART
P2SEL0 &= ~(BIT0 | BIT1);
P2SEL1 |= BIT0 | BIT1; // USCI_A0 UART operation
//set P1.6 und P1.7 as output
P1DIR |= BIT6 | BIT7;
// Configure PJ.5 PJ.4 for external crystal oscillator
PJSEL0 |= BIT4 | BIT5; // For XT1
PM5CTL0 &= ~LOCKLPM5;
}
void initClockTo16MHz()
{
// Configure one FRAM waitstate as required by the device datasheet for MCLK
// operation beyond 8MHz _before_ configuring the clock system.
FRCTL0 = FRCTLPW | NWAITS_1;
// Clock System Setup
CSCTL0_H = CSKEY_H; // Unlock CS registers
CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
// Set SMCLK = MCLK = DCO, ACLK = LFXTCLK (VLOCLK if unavailable)
CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
// Per Device Errata set divider to 4 before changing frequency to
// prevent out of spec operation from overshoot transient
CSCTL3 = DIVA__4 | DIVS__4 | DIVM__4; // Set all corresponding clk sources to divide by 4 for errata
CSCTL1 = DCOFSEL_4 | DCORSEL; // Set DCO to 16MHz
// Delay by ~10us to let DCO settle. 60 cycles = 20 cycles buffer + (10us / (1/4MHz))
__delay_cycles(60);
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers to 1 for 16MHz operation
CSCTL4 &= ~LFXTOFF;
do
{
CSCTL5 &= ~LFXTOFFG; // Clear XT1 fault flag
SFRIFG1 &= ~OFIFG;
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
CSCTL0_H = 0; // Lock CS registerss
}
//******************************************************************************
// Main ************************************************************************
// Enters LPM0 if SMCLK is used and waits for UART interrupts. If ACLK is used *
// then the device will enter LPM3 mode instead. The UART RX interrupt handles *
// the received character and echoes it. *
//******************************************************************************
int main(void)
{
uint8_t test1[10] = {0};
int i;
WDTCTL = WDTPW | WDTHOLD; // Stop Watchdog
initGPIO();
initClockTo16MHz();
initUART();
P1OUT &= ~BIT6;
P1OUT &= ~BIT7;
while(1)
{
P1OUT &= ~BIT6;
P1OUT &= ~BIT7;
while(!(UCA0IFG & UCRXIFG));
for(i = 0; i<6; i++)
{
test1[i] = UCA0RXBUF;
__delay_cycles(2000);
}
P1OUT |= BIT6;
P1OUT |= BIT7;
while(!(UCA0IFG & UCTXIFG));
for(i = 0; i<6; i++)
{
UCA0TXBUF = test1[i];
__delay_cycles(2000);
}
}
}