/* www.MicroDigitalEd.com
 * p5_4.c Toggling blue LEDs at 1 Hz using Timer32
 *
 * This program uses Timer32 to generate 1 second delay to
 * toggle the blue LED.
 * Master clock is running at 3 MHz. Time32 is configured
 * to count down from 3,000,000-1 to 0 to give a 1 second delay.
 * The blue LED is connected to P2.2.
 *
 * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0
 * on XMS432P401R Rev C.
 */

#include "msp.h"

int main(void) {
    /* initialize P2.2 for blue LED */
    P2->SEL1 &= ~4;             /* configure P2.2 as simple I/O */
    P2->SEL0 &= ~4;
    P2->DIR |= 4;               /* P2.2 set as output */

    TIMER32_1->LOAD = 3000000-1;	/* set the reload value */
    /* no prescaler, periodic wrapping mode, disable interrupt, 32-bit timer. */
    TIMER32_1->CONTROL = 0xC2;

    while (1) {
        while((TIMER32_1->RIS & 1) == 0); /* wait until the RAW_IFG is set */
        TIMER32_1->INTCLR = 0;  /* clear raw interrupt flag */
        P2->OUT ^= 4;           /* toggle blue LED */
    }
}