Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timerMs corrupts readings #7

Open
davidllpz opened this issue Mar 3, 2023 · 4 comments
Open

timerMs corrupts readings #7

davidllpz opened this issue Mar 3, 2023 · 4 comments

Comments

@davidllpz
Copy link

Reads 25MHz with timerMs = 1000 and 100. Other values corrupts the value of frequency.

#include "FreqCountESP.h"
void setup()
{
int inputPin = 14;
int timerMs = 100;
FreqCountESP.begin(inputPin, timerMs);
Serial.begin(115200);
}
void loop()
{
if (FreqCountESP.available())
{
uint32_t frequency = FreqCountESP.read();
Serial.println(frequency);
// Do something with the frequency...
}
}

@c5gary2
Copy link

c5gary2 commented Mar 6, 2023

I have a similar problem.
If I keep reading a stable frequency source, the reading varies slightly on successive reads.
I had the same problem with Arduino ATmega328 device.
Arduino delay() statement uses Timer0 which causes interrupts every 10ms(?). I had to stop Timer0 interrupts during the frequency count, using a register write, that worked! How can I do that for the ESP32? I am using Arduino IDE.

@c5gary2
Copy link

c5gary2 commented Mar 7, 2023

davidllpz,
timerMs is the gate for the frequency counter. I have tried different values it works fine. timerMs=100 gives you the frequency to 10Hz resolution, 1000 gives you 1Hz resolution. If you used something like 500, you will need to multiply the result by 2 to see the frequency in 1Hz (with only 2Hz resolution. Also, be aware that the crystal is not exactly 40MHz and there is no way to correct it, so the accuracy in not 100%.

@detroittommy879
Copy link

I added some 'correction' code so any integer works for timerMs

''' #include "FreqCountESP.h"

int inputPin = 14;
int timerMs = 250; // how many milliseconds to count, the longer the more accurate

// slight mod so that any number can be used for timerMs, the original library only shows hz when it is set to 1000
int correction = 1000 / timerMs;
int corrected = 0;

void setup()
{
Serial.begin(9600);
FreqCountESP.begin(inputPin, timerMs);
}

void loop()
{
if (FreqCountESP.available())
{
uint32_t frequency = FreqCountESP.read();
corrected = frequency * correction;

Serial.println(corrected);

}
}'''

@c5gary2
Copy link

c5gary2 commented Feb 24, 2024

If you guys are reading a stable signal, you should be seeing my problem. Set your signal generator to something like 10.0000MHz.
You might get a reading of 10.0001 or 10.0009 or something. That would be due to the crystal not being exact, etc. and YES,
you can correct for that.
BUT, you should get a very stable reading, maybe +/-1LSB.
ESP32 can not do that for me. Readings are much more variable +/-MANY counts, different with each count.
I attribute that to the built in real time multi-tasking operating system that you can not stop during
the frequency count. There SHOULD be a way but I can't find it, help would be appreciated. Gary WB6OGD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants