/* www.microDigitalEd.com * p5_11.c using Timer_A2 as an event counter on pin P4.2 * * The Timer_A2 is configured to use P4.2 as the clock. * Clock input dividers are disabled. Timer counter TA2R * is incremented for every pulse of P4.2. TA2 is configured * as up count mode and TIMER_A2->CCR[0] is set to 10. For every 10 * pulses from P4.2, the CCIFG flag of TIMER_A2->CCTL[0] is set. * * The content of timer counter TA2R is read and dumped at * the tri-color LEDs so for every pulse of P4.2, the LED * color changes. For every 10 pulses, the red LED1 is toggled. * * Connect a 0V-3V low frequency signal to P4.2 and see * the result. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0. */ #include "msp.h" int main(void) { /* initialize 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-P2.0 set as output */ P2->OUT &= ~7; /* turn off all LEDs */ /* initialize P1.0 for red LED1 */ P1->SEL1 &= ~1; /* configure P1.0 as simple I/O */ P1->SEL0 &= ~1; P1->DIR |= 1; /* P1.0 set as output */ P1->OUT &= ~1; /* turn off LED1 */ /* initialize P4.2 as TA2CLK */ P4->SEL1 |= 4; P4->SEL0 &= ~4; P4->DIR &= ~4; /* Configure TA2 as up counter using external clock, no input divides */ TIMER_A2->CTL = 0x0014; /* TA2CLK, ID = /1, up mode, TA clear */ TIMER_A2->EX0 = 0; /* IDEX = /1 */ TIMER_A2->CCR[0] = 10; /* set the flag every 10 pulses */ while (1) { P2->OUT &= ~7; /* turn off tri-color LEDs */ P2->OUT |= TIMER_A2->R & 7; /* set tri-color LEDs with counter value */ if((TIMER_A2->CCTL[0] & 1) == 1) { /* when the CCIFG is set (10 count) */ TIMER_A2->CCTL[0] &= ~1; /* clear the CCIFG flag */ P1->OUT ^= 1; /* toggle red LED1 */ } } }