I am trying out the project application of frequency measurement by AVR1617 document application. I wonder if anyone has tried this before? i have some questions to ask regarding its code. Appreciate any help that can be given!
Its main void code is as shown below. the rest of the codes are unchanged after download. I am trying to vary the result with constant changing of frequency, and trigger the LEDs accordingly.
I noted from the point that the result is = 1/4 of the actual frequency. But, no matter how i vary the frequency from function generator, the output on LEDs always hang at PORTE = 0xF0 no matter what was my input frequency. i have tested from 1kHz to 30kHz.
Would anyone be able to help this frequency measurement? Sorry i am rather new in this.
Much thanks!
Code: Select all
int main(void){
PORTE.DIR = 0xFF; //Set as ouput, the 8 MSB of the result is output here
PORTE.OUT = 0xFF; //Default off for LED
/*
* For the best possible resolution of the results, the XMEGA clock frequency changed to 32 MHz.
* The function xmega_freq_cntr_start_meas() starts the TCCA counter and
* connects the signal to be measured with TCCB. When the Gate Interval Timer, TCCA, reaches OVF,
* then Event Channel 1 triggers an Input Capture of TCCB and also triggers a TCCA interrupt,
* terminating the frequency measurement\n
*/
cpu_clk_to_32_mhz(); // Changes XMEGA frequency to 32 MHz
// Initialize frequency counter system
xmega_freq_cntr_init(); // Initializes XMEGA timer/counters and Event System
xmega_tcca_clk_freq_sel(CLK_SEL); // Selects the clock divider of TCCA. In this example to 1/64 of CLKcpu.
// Enter main application loop
while(1){
/* User Application code */
xmega_freq_cntr_start_meas(); // take a frequency measurement
/* More User Application code */
result = xmega_get_result(); // result available here
//while(xmega_get_result() ==0){
/* More User Application code here, if desired */
//}
if(result < 20000)
{
PORTE.OUT = 0xF0; //4 LEDs On
}
else
{
PORTE.OUT = 0x00; //All 8 LEDs On
}
/*
At this point in the application code ,'result' is the sum of both positive and negitave edges
being counted. This makes the result double the actual frequency. However, since the counting
time interval is 1/8 second (125 milliseconds), * the result must be both multiplied by 8 and
divided by two, for a resulting multiplation factor of four. The 'result', above, is 1/4 of the
actual frequency. Thus a 16 bit result(2^16 -1) will allow frequencies up to 262143 (2^18 -1)
to be measured.
*/
xmega_freq_cntr_clr_result();// clears result coming from driver
} // end while(1)
} // Bottom of main()