frequency meter tmr2 blunder

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
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

Post by brandonb »

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,...... :arrow: 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 380 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

Post by Gary Freegard »

Hi Brandon

Had a look at your program, and was getting a bit confused by the results. :cry:

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)
As I thought that there could be a problem with using multiple interrupts, I decided to try something different :idea: . 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
  • freq time
    10000 0.9986
    1000 0.9986
    500 0.9975
    250 0.9954
By changing the target value of tick you should be able to calibrate the unit, plus you could use it to change the frequency range if so desired.
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
Still cannot explain the difference in reading :(

Gary
Attachments
frequency.fcf
(19.83 KiB) Downloaded 365 times

Post Reply