-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path09_LED_Timer_Interrupt.c
38 lines (31 loc) · 1.32 KB
/
09_LED_Timer_Interrupt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* This code blinks the LED connected to P1.0 using an interrupt generated by the timer module
*
* We use the Timer0 module in CONTINUOUS mode to count from 0 to 0xFFFF and back to 0
* Everytime the counter "overflows" to zero, an interrupt is generated and it is used to
* toggle the LED
*
* We could use the Timer in UP mode and then the TACCR0 register to have finer control of the period
* We could also use the clock divider on the Timer0 module to extend the period of switching
* (Or we can implement the same with more control by using a variable as a counter, in software)
*
*/
#include <msp430.h>
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR|=BIT0; //Set P1.0 as output pin
TACTL|=TASSEL_2+MC_2+TAIE; //Start Timer0 with SMCLK clock source, Continuous mode and enable overflow interrupt
__low_power_mode_0();//Go into low power mode 0
return 0;
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TMR0()
{
if(TAIV==TA0IV_TAIFG) //Check if Timer overflow caused the interrupt
//This would be required in projects where multiple interrupts have
//the same interrupt vector. Here it is only optional.
{
P1OUT^=BIT0; //Toggle the LED
TACTL&=~(TAIFG); //Reset the interrupt flag
}
}