/* www.MicroDigitalEd.com * p6_2: Toggle red LED on P2.0 continuously. Upon pressing * SW1, the green LED on P2.1 blinks three times. * If SW2 is pressed, the blue LED on P2.2 blinks three times. * main program toggles red LED while waiting for interrupt from SW1 or SW2. * When green LED is blinking, the red LED ceases to blink. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0. */ #include "msp.h" void delayMs(int n); int main(void) { __disable_irq(); /* configure P1.1, P1.4 for switch inputs */ P1->SEL1 &= ~0x12; /* configure P1.1, P1.4 as simple I/O */ P1->SEL0 &= ~0x12; P1->DIR &= ~0x12; /* P1.1, P1.4 set as input */ P1->REN |= 0x12; /* P1.1, P1.4 pull resistor enabled */ P1->OUT |= 0x12; /* Pull up/down is selected by P1->OUT */ P1->IES |= 0x12; /* make interrupt trigger on high-to-low transition */ P1->IFG = 0; /* clear all pending interrupt from PORT1 */ P1->IE |= 0x12; /* enable interrupt from P1.1, P1.4 */ /* configure 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-2.0 set as output */ P2->OUT &= ~7; /* turn all three LEDs off */ NVIC_SetPriority(PORT1_IRQn, 3); /* set priority to 3 in NVIC */ NVIC_EnableIRQ(PORT1_IRQn); /* enable interrupt in NVIC */ __enable_irq(); /* global enable IRQs */ /* toggle the red LED (P2.0) continuously */ while(1) { P2->OUT |= 0x01; delayMs(500); P2->OUT &= ~0x01; delayMs(500); } } /* SW1 is connected to P1.1 pin, SW2 is connected to P1.4 * Both of them trigger PORT1 interrupt */ void PORT1_IRQHandler(void) { int i; if (P1->IFG & 2) { /* from SW1 (P1.1) */ /* toggle green LED (P2.1) three times */ for (i = 0; i < 3; i++) { P2->OUT |= 0x02; delayMs(500); P2->OUT &= ~0x02; delayMs(500); } P1->IFG &= ~2; /* clear interrupt flag */ } if (P1->IFG & 0X10) { /* from SW2 (P1.4) */ /* toggle blue LED (P2.2) three times */ for (i = 0; i < 3; i++) { P2->OUT |= 0x04; delayMs(500); P2->OUT &= ~0x04; delayMs(500); } P1->IFG &= ~0x10; /* clear interrupt flag */ } } /* delay n milliseconds (3 MHz CPU clock) */ void delayMs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 250; j++) {} /* do nothing for 1 ms */ }