Write a program that configures the UART for operation at 11
Write a program that configures the UART for operation at 115200 baud, 8 data bits, no parity and 1 stop bit (assume an 8 MHz clock for MSP430). Repeat the same procedure for baud rate 9600 using ACLK (32768 Hz). Calculate required values of “UART Mode Registers” and show how you set those values in your program.
Solution
int uart_init(uart_config_t *config)
{
int status = -1;
/* USCI should be in reset before configuring - only configure once */
if (UCA0CTL1 & UCSWRST) {
size_t i;
/* Set clock source to SMCLK */
UCA0CTL1 |= UCSSEL_2;
/* Find the settings from the baud rate table */
for (i = 0; i < ARRAY_SIZE(baud_tbl); i++) {
if (baud_tbl[i].baud == config->baud) {
break;
}
}
if (i < ARRAY_SIZE(baud_tbl)) {
/* Set the baud rate */
UCA0BR0 = baud_tbl[i].UCAxBR0;
UCA0BR1 = baud_tbl[i].UCAxBR1;
UCA0MCTL = baud_tbl[i].UCAxMCTL;
/* Enable the USCI peripheral (take it out of reset) */
UCA0CTL1 &= ~UCSWRST;
status = 0;
}
}
return status;
}
Use the following structure to call the above program. Also, a baud rate table needs to be initialized.
typedef struct
{
uint32_t baud;
} uart_config_t;
baud rate table
struct baud_value
{
uint32_t baud;
uint16_t UCAxBR0;
uint16_t UCAxBR1;
uint16_t UCAxMCTL;
};
The baud rate table
1
2
3
const struct baud_value baud_tbl[] = {
{115200, 9600, 104, 0, 0x2}
};
| struct baud_value { uint32_t baud; uint16_t UCAxBR0; uint16_t UCAxBR1; uint16_t UCAxMCTL; }; |

