/* www.MicroDigitalEd.com * P6_8.c Testing nested interrupts * Timer1 is setup to interrupt at 1 Hz. In timer interrupt handler, * the red LED is turned on and a delay function of 350 ms is called. * The red LED is turned off at the end of the delay. * * Timer2 is setup to interrupt at 10 Hz. In timer interrupt handler, * the blue LED is turned on and a delay function of 20 ms is called. * The blue LED is turned off at the end of the delay. * * When Timer1 has higher priority (the way this code is), the Timer2 * interrupts are blocked by Timer1 interrupt handler. You can see * that when the red LED is on, the blue LED stops blinking. * * When Timer2 has higher priority (you need to switch the priority of * the two timers at the #defines), the Timer1 interrupt handler is * preempted by Timer2 interrupts and the blue LED is blinking all the time. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0 * on XMS432P401R Rev C. */ #include "msp.h" void Timer1_init(void); void Timer2_init(void); void delayMs(int n); int main (void) { __disable_irq(); /* initialize P2.2, P2.0 for blue and red LED */ P2->SEL1 &= ~5; /* configure P2.2, P2.0 as simple I/O */ P2->SEL0 &= ~5; P2->DIR |= 5; /* P2.2, P2.0 set as output */ Timer1_init(); Timer2_init(); __enable_irq(); while(1) { /*wait here for interrupt */ } } void T32_INT1_IRQHandler(void) { P2->OUT |= 4; /* turn on blue LED */ delayMs(350); P2->OUT &= ~4; /* turn off blue LED */ TIMER32_1->INTCLR = 0; /* clear raw interrupt flag */ } void T32_INT2_IRQHandler(void) { P2->OUT |= 1; /* turn on red LED */ delayMs(20); P2->OUT &= ~1; /* turn off red LED */ TIMER32_2->INTCLR = 0; /* clear raw interrupt flag */ } /* priority of Timer1 and Timer2 should be between 0 and 7 */ #define PRIO_TMR1 2 #define PRIO_TMR2 3 void Timer1_init(void) { TIMER32_1->CONTROL = 0xC2; /* no prescaler, periodic mode, 32-bit timer. */ TIMER32_1->LOAD = 3000000-1; /* set the reload value for 1 Hz */ TIMER32_1->CONTROL |= 0x20; /* enable interrupt */ NVIC_SetPriority(T32_INT1_IRQn, PRIO_TMR1); NVIC_EnableIRQ(T32_INT1_IRQn); /* enable interrupt in NVIC */ } void Timer2_init(void) { TIMER32_2->CONTROL = 0xC2; /* no prescaler, periodic mode, 32-bit timer. */ TIMER32_2->LOAD = 300000-1; /* set the reload value for 10 Hz */ TIMER32_2->CONTROL |= 0x20; /* enable interrupt */ NVIC_SetPriority(T32_INT2_IRQn, PRIO_TMR2); NVIC_EnableIRQ(T32_INT2_IRQn); /* enable interrupt in NVIC */ } /* delay n milliseconds (3 MHz CPU clock) */ void delayMs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 750; j++) {} /* do nothing for 1 ms */ }