kapraran / FreqCountESP

A frequency counter library for esp32
https://kapraran.github.io/FreqCountESP/
MIT License
31 stars 7 forks source link

timerMs corrupts readings #7

Open davidllpz opened 1 year ago

davidllpz commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 4 months ago

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 commented 4 months ago

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