UART Challenge 1
1. Here was the challenge:
Write a program that uses the UART to transmit a rocket countdown at 9600 baud. In your main
program, set up the peripheral and enable the transmit interrupt. Then transmit 0x0A (that is,
10 decimal).
In your ISR, continue the countdown by sending 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
0x02, 0x01, and finally 0x00. When you transmit 0x00, you should also light the red LED.
(Sorry, we did not include a rocket in the class lab kit….)
2. The program on the next page is one way to do this.
We have not included the select_clock_signals(), assign_pins_to_uart(), or the
use_9600_baud() functions, but they are identical to our previous programs.
After setting up the UART peripheral, the program transmits the first count (10 or 0x0A) and then
puts the microcontroller into an infinite loop.
After the UART completes transmission of the stop bit, an interrupt is generated and the program
jumps to the ISR.
The ISR begins by determining if the count has been decremented to 0 yet. If it has, it transmits
the final 0x00 data byte and launches the rocket. If the countdown is still above zero, the
updated countdown is transmitted.
The ISR ends by clearing the TX ComPleTe Interrupt FlaG, and the microcontroller then returns to
the infinite loop in main().
Page 1 of 3
#include <msp430.h>
#define ENABLE_PINS 0xFFFE // Required to use inputs and outputs
#define UART_CLK_SEL 0x0080 // Specifies accurate clock for UART peripheral
#define BR0_FOR_9600 0x34 // Value required to use 9600 baud
#define BR1_FOR_9600 0x00 // Value required to use 9600 baud
#define CLK_MOD 0x4911 // Microcontroller will "clean-up" clock signal
void select_clock_signals(void); // Assigns microcontroller clock signals
void assign_pins_to_uart(void); // P4.2 is for TXD, P4.3 is for RXD
void use_9600_baud(void); // UART operates at 9600 bits/second
main()
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
PM5CTL0 = ENABLE_PINS; // Enable pins
P1DIR = BIT0; // Make P1.0 an output for red LED
P1OUT = 0x00; // Red LED initially off
select_clock_signals(); // Assigns microcontroller clock signals
assign_pins_to_uart(); // P4.2 is for TXD, P4.3 is for RXD
use_9600_baud(); // UART operates at 9600 bits/second
UCA0IE = UCTXCPTIE; // Interrupt when TX stop bit complete
_BIS_SR(GIE); // Activate enabled UART TXD interrupt
UCA0TXBUF = 10; // Send the UART message 0x0A out pin P4.2
while(1); // Wait here unless you get UART interrupt
}
//*********************************************************************************
//* UART Interrupt *
//*********************************************************************************
#pragma vector=USCI_A0_VECTOR
__interrupt void UART_ISR(void)
{
static unsigned char countdown = 10; // Countdown state to transmit
countdown = countdown - 1; // Decrement countdown each time
if(countdown == 0) // If countdown is “over”
{
P1OUT = BIT0; // Launch the rocked (red LED)
UCA0TXBUF = 0x00; // Countdown complete
UCA0IE = UCA0IE & (~UCTXCPTIE); // Disable future UART interrupts
}
else // Else countdown is not over
{
UCA0TXBUF = countdown; // Transmit the next count
}
UCA0IFG = UCA0IFG & (~UCTXCPTIFG); // Clear TX ComPleTe Interrupt FlaG
}
Page 2 of 3
All tutorials and software examples included herewith are intended solely for
educational purposes. The material is provided in an “as is” condition. Any
express or implied warranties, including, but not limited to the implied warranties
of merchantability and fitness for particular purposes are disclaimed.
The software examples are self-contained low-level programs that typically
demonstrate a single peripheral function or device feature in a highly concise
manner. Therefore, the code may rely on the device's power-on default register
values and settings such as the clock configuration and care must be taken when
combining code from several examples to avoid potential side effects.
Additionally, the tutorials and software examples should not be considered for use
in life support devices or systems or mission critical devices or systems.
In no event shall the owner or contributors to the tutorials and software be liable
for any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services; loss of
use, data, or profits; or business interruption) however caused and on any theory
of liability, whether in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even if advised of
the possibility of such damage.
Page 3 of 3