Timer A interrupt examples from today’s lecture

Example 1: Use a Timer A0 interrupt to flash some LEDs on and off

In this example, we configure Timer A0 to increment every 8 microseconds and generates an interrupt whenever it reaches 12500, which occurs every 100ms. The resulting interrupt service routine (ISR) either switches on or switches off all three LEDs (red LED on P2.0, white LED on P2.1, green LED on P2.2).

//
// Timer A interrupt example for MSP430G2452
// Written by Ted Burke - last updated 5-4-2017
//
 
#include <msp430.h>

// Timer A0 interrupt service routine (ISR)
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
    // Toggle LEDs
    if (P2OUT == 0) P2OUT = 0b00000111;
    else            P2OUT = 0b00000000;
}

// Main function
void main()
{
    // Disable watchdog timer
    WDTCTL = WDTPW + WDTHOLD;
    
    // Configure inputs and outputs 
    P1DIR = 0b00000000; // P1.0-7 are all inputs
    P2DIR = 0b00000111; // LEDs: P2.2=green, P2.1=white, P2.0=red
    
    // Configure TimerA0 and its interrupt
    TA0CTL = TASSEL_2 + MC_1 + ID_3; // "Up" mode, divide SMCLK by 8
    TA0CCR0 =  12500;                // Set timer period to 100ms
    TA0CCTL0 = CCIE;                 // Compare interrupt enabled    
    _BIS_SR(GIE);                    // Global interrupt enable
    
    while(1)
    {
        // Nothing required here because the ISR does all the work
    }
}

Example 2: Use a Timer A0 interrupt to add a millisecond counter to a state machine

In this example, we configure Timer A0 to increment every 8 microseconds and generate an interrupt whenever it reaches 125, which occurs every 1ms. The resulting interrupt service routine (ISR) increments the variable “milliseconds” which is used to count the number of milliseconds that have elapsed since the program entered the current state. Each state lights a different LED (state 1: red LED on P2.0; state 2: white LED on P2.1; state 3: green LED on P2.2).

//
// Millisecond timer example for MSP430G2452
// Written by Ted Burke - last updated 5-4-2017
//
 
#include <msp430.h>

// Declare variables to stores the current state number
// and counts the number of milliseconds that have
// elapsed in current state.
int state = 1;
int milliseconds = 0;

// This function is used to change states.
// The input value s is the state to switch to.
// The function updates the state variable and resets
// the millisecond counter variable.
void change_state(int s)
{
    state = s;
    milliseconds = 0;
}

// This is the Timer A0 interrupt service routine.
// Once the timer is configured, this function will
// be called every 100ms.
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
    milliseconds = milliseconds + 1;
}

// This is the main function
void main()
{
    // Disable watchdog timer
    WDTCTL = WDTPW + WDTHOLD;
    
    // Configure inputs and outputs 
    P1DIR = 0b00000000; // P1.0-7 are all inputs
    P2DIR = 0b00000111; // LEDs: P2.2=green, P2.1=white, P2.0=red
    
    // Configure TimerA0 and its interrupt
    TA0CTL = TASSEL_2 + MC_1 + ID_3; // "up" mode, divide SMCLK by 8
    TA0CCR0 =  125;                  // set timer period to 1ms
    TA0CCTL0 = CCIE;                 // compare interrupt enabled    
    _BIS_SR(GIE);                    // global interrupt enable
    
    // State loop
    while(1)
    {
        if (state == 1)
        {
            P2OUT = 0b00000001; // Red
            
            if (milliseconds > 1000) change_state(2);
        }
        else if (state == 2)
        {
            P2OUT = 0b00000010; // White

            if (milliseconds > 2000) change_state(3);
        }
        else if (state == 3)
        {
            P2OUT = 0b00000100; // Green

            if (milliseconds > 3000) change_state(1);
        }
    }
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s