Quantcast
Channel: MSP low-power microcontrollers
Viewing all articles
Browse latest Browse all 62309

Forum Post: MSP430G2553: Frequency meter code

$
0
0
Part Number: MSP430G2553 Hi I'm looking at some code for a frequency counter. I'm still learning but I think the code should be fairly straightforward. #include /*** Global variables ***/ volatile unsigned int CounterValue = 0; volatile unsigned int StoredCount = 0; unsigned int Result = 0; int main(void) { /*** Watchdog timer and clock Set-Up ***/ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer BCSCTL1 = CALBC1_8MHZ; // Set range DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation /*** GPIO Set-Up ***/ P2DIR &= ~BIT0; // P2.0 set as input P2SEL |= BIT0; // P2.0 set to primary peripheral Timer1_A /*** Timer_A Set-Up ***/ TA0CCR0 = 20000; // 20000*400 = 8000000 or 8MHz TA0CCTL0 |= CCIE; // Interrupt enable TA0CTL |= TASSEL_2; // SMCLK TA1CCTL0 |= CCIE + CCIS_0 + CM_2 + CAP; // Interrupt enable, capture select CCIxA, Positive edge, capture mode TA1CTL |= TASSEL_2; // SMCLK _BIS_SR(GIE); // Enter LPM0 with interrupts enabled while(1) { TA0CTL |= MC0; // Start timer TA1CTL |= MC1; // Start timer while(CounterValue != 400); // Wait while CounterValue is not equal to 400 TA0CTL &= ~MC0; // Stop timer TA1CTL &= ~MC1; // Stop timer Result = StoredCount; // Store frequency in Result CounterValue = 0; // Zero CounterValue StoredCount = 0; // Zero StoredCount TA0R = 0; // Zero Timer_A0 register TA1R = 0; // Zero Timer_A1 register } } //Timer_A0 TACCR0 Interrupt Vector Handler Routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer0_A0(void) { CounterValue++; } //Timer_A1 TACCR0 Interrupt Vector Handler Routine #pragma vector=TIMER1_A0_VECTOR __interrupt void Timer1_A0(void) { StoredCount++; } I have two questions: 1) How come TA0CCR0 is set to the decimal value of 20000? What's the significance of 20000? 2) How come the routine waits for CounterValue to reach 400? My understanding is that CounterValue is incremented for each positive edge. Why does it need to wait for 400 positive edges? Any thought appreciated.

Viewing all articles
Browse latest Browse all 62309

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>