/* www.MicroDigitalEd.com * p5_3.c Toggling green LED using SysTick delay * This program uses SysTick to generate one second delay to * toggle the green LED. * master clock is running at 3 MHz. SysTick is configure * to count down from 3000-1 to 0 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) { delayMs(1000); /* delay 1000 ms */ P2->OUT ^= 2; /* toggle green LED */ } } void delayMs(int n) { int i; SysTick->LOAD = 3000 - 1; /* reload with number of clocks per millisecond */ SysTick->VAL = 0; /* clear current value register */ SysTick->CTRL = 0x5; /* Enable the timer */ for(i = 0; i < n; i++) { while((SysTick->CTRL & 0x10000) == 0) /* wait until the COUNTFLAG is set */ { } } SysTick->CTRL = 0; /* Stop the timer (Enable = 0) */ }