Hi,
I am trying to develop a frequency counter based on TimerA's capture mode. I set up CCI with 1kHz for avoiding missing captures and timer with 26MHz radio clock, which I think is faster and provides a more accurate reference.
Till now I tried with some slower clocks, the accuracy looks great. But when I playing with 1kHz and 26MHz, the count value is having a larger range thus the frequency I measured is not accurate and stable enough.
Is there any way to improve this situation? And if there is any algorithm I could put in to improve my frequency counter?
Below is my interrupt service code, if you have a better solution than my interrupt please let me know and give me some hints!
#define NCAPTURES 32 // Number of captures to accumulate
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMERA1_ISR ( void ) // ISR for CCIFGn and TAIFG
{
static int Captures = 0; // Number of captures accumulated
volatile unsigned int StartTime ; // Time at start of sampling
if ( TA0IV == TA0IV_TACCR2 ) { // CCIFG4 vector (4)
switch ( Captures ) {
case 0: // Starting new sequence of captures
StartTime = TA0CCR2 ; // Starting time
Captures = NCAPTURES ; // Initialize down counter
break ;
case 1: // Final capture of sequence
printf("num=%u\r\n" ,TA0CCR2 - StartTime);
Captures = 0; // Finished
//TA0CCTL2=~CCIE; // Disable further interrupts
break ;
default : // Sequence of captures continues
--Captures ;
break ;
}
}
}
Thanks in advance!