News:

Let's find out together what makes a PIC Tick!

Main Menu

FREQUENCY COUNTER

Started by Abdullah, Oct 08, 2025, 12:46 PM

Previous topic - Next topic

Abdullah

frequency.rar
HELLO EVERYONE
 I have tried to make a frequency meter but its frequency is not stable for me, the frequency keeps increasing and decreasing.

charliecoutas

How can we help you Abdullah?

Charlie

Abdullah

Quote from: charliecoutas on Oct 08, 2025, 03:08 PMHow can we help you Abdullah?

Charlie
By solving my problem
Frequency is not stable on the LCD

trastikata

#3
Is it certain the counter doesn't overflow?

Also your pulse edge detection logic in the loop has a flaw, take a better look and try to find it.

Fanie

#4
Very easy to do !

Use the Counter command
Syntax
Variable = Counter Pin, Period 

With this you count a certain count of pulses for the time you want
To calibrate, feed a known frequency to the pin
You can then multiply / divide the counted pulses to be the same as the frequency you applied.

Make the variable a Word to start with and keep the period short enough, say half a second or a quarter second or how frequent you want to display the value.

Make sure the pic runs fast enough to execute the code to do the counting.

From the maual
25KHz using a 4MHz oscillator. 
125KHz using a 20MHz oscillator. 

calculated from the above a 64MHz pic will count to 400kHz
And can I add a 240MHz ESP32 will count to  1,5MHz  ;D

Abdullah

Quote from: Fanie on Oct 08, 2025, 06:21 PMVery easy to do !

Use the Counter command
Syntax
Variable = Counter Pin, Period 

With this you count a certain count of pulses for the time you want
To calibrate, feed a known frequency to the pin
You can then multiply / divide the counted pulses to be the same as the frequency you applied.

Make the variable a Word to start with and keep the period short enough, say half a second or a quarter second or how frequent you want to display the value.

Make sure the pic runs fast enough to execute the code to do the counting.

From the maual
25KHz using a 4MHz oscillator. 
125KHz using a 20MHz oscillator. 

calculated from the above a 64MHz pic will count to 400kHz
And can I add a 240MHz ESP32 will count to  1,5MHz  ;D

THANK YOU SIR FANIE
I RESOLVE THE PROBLEM
MANY THANKS TO SIR FANIE AND SIR TRASTIKATA

Stephen Moss

OffTime = TMR1L + (TMR1H * 145)Is wrong, for the following reasons...
  • TMR1H is 8 bits and so can be any value between 0 and 255, if you multiply that by 145 it will overflow a byte value and the results will be random
  • Even without the multiplication TMR1L+TMR1H is adding 0-255 to 0-255 = maximum 510, I don't think that is what you wanted to do as the resulting count would be wrong

Also you have Dimmed SumFreq as Float when it only contains integer values, so Dim as Long or Dword would make more sense.

What you should have done is loaded OffTime with the TMR1 value and then if you needed to scale it multiple the result, but if multiplyinmg by 145 you would need to be certain that the timer values would not exceed 451 otherwise again your would get an random values as the result overflows a Word value (451*145 = 65535)

I am not sure using the Pulse In command is the best method anyway, I would have used an interrupt on change pin, starting/stopping the time on either a low to high or high to low transition, so you time the entire period rather than timing both the high and low periods and adding them. There is no reason to count them separately if all you want it the period.

Abdullah

#7
Quote from: Stephen Moss on Oct 09, 2025, 09:22 AM OffTime = TMR1L + (TMR1H * 145)Is wrong, for the following reasons...
  • TMR1H is 8 bits and so can be any value between 0 and 255, if you multiply that by 145 it will overflow a byte value and the results will be random
  • Even without the multiplication TMR1L+TMR1H is adding 0-255 to 0-255 = maximum 510, I don't think that is what you wanted to do as the resulting count would be wrong

Also you have Dimmed SumFreq as Float when it only contains integer values, so Dim as Long or Dword would make more sense.

What you should have done is loaded OffTime with the TMR1 value and then if you needed to scale it multiple the result, but if multiplyinmg by 145 you would need to be certain that the timer values would not exceed 451 otherwise again your would get an random values as the result overflows a Word value (451*145 = 65535)

I am not sure using the Pulse In command is the best method anyway, I would have used an interrupt on change pin, starting/stopping the time on either a low to high or high to low transition, so you time the entire period rather than timing both the high and low periods and adding them. There is no reason to count them separately if all you want it the period.
Yes sir, you are right, this value is wrong, but because of this, I was getting the correct frequency on the LCD. If I set the value to 256, that is, 8 bits, then I would get half or double the result, that is, if I gave 400 Hz input, it would either show 200 or 800 Hz.
But after using the counter variable, my problem was solved. I got what I wanted. Thank you for your help. Best wishes to you.
If you can improve this code, i.e. make the frequency as accurate as possible, it would be greatly appreciated.