UART Challenge 2
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….)
This time, however, you were to include a 1 second delay between the counts.
2. The program on the next page is one way to do this.
It does not use a UART interrupt.
Rather, after setting up the UART, it sets up Timer0 to count for approximately 1 second. Each
second, the microcontroller will leave the main() function and jump to the Timer0 ISR.
Inside the Timer0 ISR, instructions similar to UART Challenge 1 check the status of the
countdown and either transmit the next number or transmits 0x00 and launches the rocket.
Page 1 of 3
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
TA0CCR0 = ONE_SECOND; // This number will vary so I #defined it
TA0CTL = ACLK | UP; // Set ACLK, UP MODE
TA0CCTL0 = CCIE; // Enable interrupt for Timer_0
_BIS_SR(GIE); // Activate interrupts previously enabled
UCA0TXBUF = 10; // Send the UART message 0x0A out pin P4.2
while(1); // Wait here for interrupt
}
//************************************************************************
// Timer0 Interrupt Service Routine
//************************************************************************
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer0_ISR (void)
{
static unsigned char countdown=9; // Value to be transmitted
if(countdown == 0) // If countdown is "over"
{
UCA0TXBUF = countdown; // Transmit "Zero"
P1OUT = BIT0; // Launch rocket (red LED)
TA0CCTL0 = TA0CCTL0 & (~CCIE); // Disable future Timer interrupts
}
else // Else, still counting down
{
UCA0TXBUF = countdown; // Transmit present count state
countdown = countdown - 1; // Decrement count for next time
}
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