Tool/software: Code Composer Studio
I'm trying to establish a UART communication channel (PIN 4 and 5) for MSP430FR2433. I found this example code and thought I'll try with this first. For some reason, I'm getting corrupted data on my serial terminal. I did config the baud rate to 115200. I tried the same hardware on another example code by TI and it works just fine. Please help!
// ========== header file includes ============================================
#include <msp430fr2433.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// ========== constants =======================================================
#define CR 13
#define LF 10
#define UART_TIMEOUT 9876
// ========== global variables ================================================
char rx_data;
char debug_string [123];
uint64_t seconds = 0;
// ========== function prototypes =============================================
void uart_init(void);
bool uart_send_debug_string(char []);
bool uart_send_byte(char);
void init_RTC(void);
// ========== function definitions ============================================
/*
* @brief This is the main function
* @param None
* @return None
*/
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
init_RTC();
uart_init();
// Disable the GPIO power-on default high-impedance mode
// to activate previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
__enable_interrupt(); // enable all interrupts --> GIE = 1 (HIGH)
uart_send_debug_string("Hello World");
while(1)
{
}
}
// UCA0 UART ISR
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
{
case USCI_NONE:
break;
case USCI_UART_UCRXIFG:
UCA0IFG &=~ UCRXIFG; // Clear interrupt
rx_data = UCA0RXBUF; // Store buffer
//sprintf(debug_string, "Received: %c", rx_data);
uart_send_debug_string(debug_string);
UCA0TXBUF = 'Q';
break;
case USCI_UART_UCTXIFG:
break;
case USCI_UART_UCSTTIFG:
break;
case USCI_UART_UCTXCPTIFG:
break;
}
}
// end UCA0 UART ISR
// ****************************************************************************
// RTC interrupt service routine
#pragma vector=RTC_VECTOR
__interrupt void RTC_ISR(void)
{
switch(__even_in_range(RTCIV,RTCIV_RTCIF))
{
case RTCIV_NONE:
break; // No interrupt
case RTCIV_RTCIF: // RTC Overflow
P1OUT ^= BIT0; // Blink P1.0
seconds++;
//sprintf(debug_string, "Seconds: %llu", seconds);
uart_send_debug_string(debug_string);
break;
default:
break;
}
}
// end RTC interrupt service routine
// ****************************************************************************
/*
* @brief Initialize the USCI A0 UART communication
* @param None
* @return None
*/
void uart_init (void)
{
// Configure UART pins
P1SEL0 |= BIT4 | BIT5; // set 2-UART pin as second function
// Configure UART
UCA0CTLW0 |= UCSWRST; // Put eUSCI in reset
UCA0CTLW0 |= UCSSEL__SMCLK; // Chose SMCLK as clock source
// Baud Rate calculation for 115200 with SMCLK (1MHz)
// 1000000/115200 = 8.68
// 1000000/115200 - INT(1000000/115200)=0.68
// UCBRSx value = 0xD6 (See UG)
UCA0BR0 = 8;
UCA0MCTLW = 0xD600;
UCA0BR1 = 0;
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
// end uart_init
// ****************************************************************************
/*
* @brief This function send the given byte over USCI A0 UART
* @param[in] data - char byte to send
* @return true is successful, false if failed
*/
bool uart_send_byte (char data)
{
uint16_t uart_timeout = UART_TIMEOUT; // load time out value
while((!(UCA0IFG & UCTXIFG))&&(uart_timeout>0))
{
uart_timeout--; // wait till TX interrupt pending
}
if(uart_timeout == 0)
{
return false; // failure if time out
}
UCA0TXBUF = data;
UCA0TXBUF = 'R';
uart_timeout = UART_TIMEOUT; // again load time out value
while((!(UCA0IFG & UCTXIFG))&&(uart_timeout>0))
{
uart_timeout--; // wait till TX interrupt pending
}
if(uart_timeout == 0)
{
return false; // failure if time out
}
return true; // success otherwise
}
// end uart_send_byte
// ****************************************************************************
/*
* @brief This function send the given string over USCI A0 UART
* @param array char array to send
* @return true is successful, false if failed
*/
bool uart_send_debug_string (char array[])
{
uint16_t size = strlen(array);
uint16_t index = 0;
for(index=0; index<size; index++)
{
if(!uart_send_byte(array[index])) // send char one by one
{
return false;
}
}
if(!uart_send_byte(CR)) // send carriage return
{
return false;
}
if(!uart_send_byte(LF)) // send line feed for new line
{
return false;
}
return true;
}
// end uart_send_debug_string
// ****************************************************************************
/*
* @brief Initialize the system RTC, to tick multiple of 1 second
* @param None
* @return None
*/
void init_RTC (void)
{
// RTC count re-load compare value
RTCMOD = (1026);
// Source = SMCLK (1MHz), divided by 1024
RTCCTL = RTCSS__SMCLK | RTCSR | RTCPS__1024 | RTCIE;
P1DIR |= BIT0; // Set P1.0 to output direction
}
// end init_RTC
// ****************************************************************************