/* www.MicroDigitalEd.com * p6_5.c Toggling LEDs independently using Timer32 interrupts * * This program uses Timer32.1 and Timer32.2 to generate interrupts. * In the interrupt handlers, the red and green LEDs are toggled. * In the infinite loop the blue LED is toggled. * All three LEDs are blinked independently. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0 * on XMS432P401R Rev C. */ #include "msp.h" void delayMs(int n); int main(void) { __disable_irq(); /* initialize P2.2-P2.0 for tri-color LEDs */ P2->SEL1 &= ~7; /* configure P2.2-P2.0 as simple I/O */ P2->SEL0 &= ~7; P2->DIR |= 7; /* P2.2-P2.0 set as output */ /* configure Timer32.1 */ TIMER32_1->CONTROL = 0xC2; /* no prescaler, periodic mode, 32-bit timer. */ TIMER32_1->LOAD = 2300000-1; /* set the reload value */ TIMER32_1->CONTROL |= 0x20; /* enable interrupt */ NVIC_SetPriority(T32_INT1_IRQn, 3); /* set priority to 3 in NVIC */ NVIC_EnableIRQ(T32_INT1_IRQn); /* enable interrupt in NVIC */ /* configure Timer32.2 */ TIMER32_2->CONTROL = 0xC2; /* no prescaler, periodic mode, 32-bit timer. */ TIMER32_2->LOAD = 1900000-1; /* set the reload value */ TIMER32_2->CONTROL |= 0x20; /* enable interrupt */ NVIC_SetPriority(T32_INT2_IRQn, 4); /* set priority to 4 in NVIC */ NVIC_EnableIRQ(T32_INT2_IRQn); /* enable interrupt in NVIC */ __enable_irq(); /* global enable IRQs */ while (1) { P2->OUT ^= 4; /* blink blue LED */ delayMs(1000); } } void T32_INT1_IRQHandler(void) { TIMER32_1->INTCLR = 0; /* clear raw interrupt flag */ P2->OUT ^= 1; /* toggle red LED */ } void T32_INT2_IRQHandler(void) { TIMER32_2->INTCLR = 0; /* clear raw interrupt flag */ P2->OUT ^= 2; /* toggle green LED */ } /* delay milliseconds when system clock is at 3 MHz */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* wait 1 ms */ }