/* www.MicroDigitalEd.com * p5_5.c Toggling green LED using Timer32 delay * This program uses Timer32 to generate one second delay to * toggle the green LED. * Master clock is running at 3 MHz. Timer32 is configure * to count down from 3000-1 to give a 1 ms delay. * For every 1000 delays (1 ms * 1000 = 1 sec), toggle the * green LED once. The green LED is connected to P2.1. * * 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) { /* initialize P2.1 for green LED */ P2->SEL1 &= ~2; /* configure P2.1 as simple I/O */ P2->SEL0 &= ~2; P2->DIR |= 2; /* P2.1 set as output */ while (1) { P2->OUT ^= 2; /* toggle green LED */ delayMs(1000); /* delay 1000 ms */ } } void delayMs(int n) { int i; TIMER32_1->LOAD = 3000 - 1; /* reload with number of clocks per millisecond */ /* no prescaler, periodic wrapping mode, disable interrupt, 32-bit timer. */ TIMER32_1->CONTROL = 0xC2; for(i = 0; i < n; i++) { while((TIMER32_1->RIS & 1) == 0); /* wait until the RAW_IFG is set */ TIMER32_1->INTCLR = 0; /* clear RAW_IFG flag */ } }