frequency meter tmr2 blunder
Moderator: Benj
-
brandonb
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
frequency meter tmr2 blunder
this frequency meter works great, however i discovered something odd--> this only works accuratly with a tmr2 frequency of 1000 hz, for example reading 5khz input the meter reads 5Khz but if i set tmr2 interrupt to 250hz it reads 4988 hz instead,.... so for kicks i tried a 16.384Mhz crystal instead of internal osc for a lower even tmr2 interrupt freq, i tried 1000 hz again and it was spot on, then i tried 62hz tmr2 interrupt frequency, it was 30 hz low at 4970 roughly,......
i would have figured that using a low tmr2 interrupt frequency my interrupt on pin input frequency would have been more accurate than at a high frequency tmr2 interrupt since at a high interrupt speed there is less time to service the interrupt on pin macro, but that theory was proven backwards(why did this happen?) 
- Attachments
-
- freq_meter_v1.0.00.fcf
- (18.79 KiB) Downloaded 399 times
-
Gary Freegard
- Posts: 45
- Joined: Mon Nov 07, 2011 6:36 pm
- Has thanked: 1 time
- Been thanked: 30 times
Re: frequency meter tmr2 blunder
Hi Brandon
Had a look at your program, and was getting a bit confused by the results.
Have written my own version, differences from yours are:
. So after a look at the datasheet I saw that it was possible to use a timer as a counter with an external input. In the Timer_Enable and Timer_Disable macros the C code is used to enable and disable the counter. Also in these macros is an output to port A for timing measurements/calibration.
The macro Frequency is used to read the counter and reset it with the C code, and then display the result.
Using the output on port A I have got the following results, freq is the timer2 int and time is measured on port A
If a higher accuracy is required then use the highest frequency possible, as this will allow finer calibration.
So for the above reading, target should be
Gary
Had a look at your program, and was getting a bit confused by the results.
Have written my own version, differences from yours are:
- uses 16f877a
the timer enable and disable are in macros
LCD update is in a macro
uses timer1 as counter
main is used to initialize and has an empty loop
not using variables to control count etc
has an output on A0 for calibration (1sec)
The macro Frequency is used to read the counter and reset it with the C code, and then display the result.
Using the output on port A I have got the following results, freq is the timer2 int and time is measured on port A
- freq time
10000 0.9986
1000 0.9986
500 0.9975
250 0.9954
If a higher accuracy is required then use the highest frequency possible, as this will allow finer calibration.
So for the above reading, target should be
- 10014
1001
501
251
Gary
- Attachments
-
- frequency.fcf
- (19.83 KiB) Downloaded 383 times