/* p5_10.c using Timer_A0.2 for capture mode of pin P2.5. * * This program uses TA0.2 for capture mode of pin P2.5. * SMCLK is running at 3 MHz. Timer_A0 is free-running * without input clock dividers. When rising edge of P2.5 * occurs, the content of TA0R is copied into TIMER_A0->CCR[2] and * CCIFG flag in TIMER_A0->CCTL[2] is set. * Two timestamps are taken and the difference is the * period of the input signal. By shifting the difference * 13 bits to the right, it is displayed on the tri-color LEDs. * It works well up to about 150 Hz with 13-bit shift. * To run this program, you need to connect a 0-3V periodic * signal to pin P2.5. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0 * on XMS432P401R Rev C. */ #include "msp.h" int main(void) { unsigned short last, current, diff; /* 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 */ /* configure P2.5 as TA0.CCI1A */ P2->SEL1 &= ~0x20; P2->SEL0 |= 0x20; P2->DIR &= ~0x20; TIMER_A0->CTL = 0x0224; /* SMCLK, ID = /1, up mode, TA clear */ TIMER_A0->CCTL[2] = 0x4900; /* rising edge, CCI2A, SCS, capture mode */ TIMER_A0->EX0 = 0; /* IDEX = /1 */ while (1) { TIMER_A0->CCTL[2] &= ~1; /* clear interrupt flag */ while((TIMER_A0->CCTL[2] & 1) == 0); /* wait until the CCIFG is set */ last = TIMER_A0->CCR[2]; /* save first timestamp */ TIMER_A0->CCTL[2] &= ~1; /* clear interrupt flag */ while((TIMER_A0->CCTL[2] & 1) == 0); /* wait until the CCIFG is set */ current = TIMER_A0->CCR[2]; /* save second timestamp */ diff = current - last; /* display the time interval */ P2->OUT = diff >> 13; } }