FreqCount() not working as I expected

Status
Not open for further replies.

RichardL

Member
Hi,
I'm using a Teensy 4 and 'FreqCount' to measure frequencies in the range 1KHz to 60MHz (Higher would be good!).
The user is able to select the gate time in the range 1mSec to 10Seconds, trading precision for speed.
I'm using a very high speed comparator to drive the Teensy and a signal generator to check performance.
The drive waveform at Teensy Pin 9 is good.
My code is working perfectly well up to 60MHz with gate times of 100mSec, 1 Seconds and 10 Seconds.
I'm adjusting the gate time very slightly at 1 Second and 10 Seconds to correct for the Teensy's xtal error. (Works well).

My problem is with short gate times and I suspected my code.
However, I'm also able to reproduce the problem with the example code, so maybe not me:

Code:
#include <FreqCount.h>

void setup() {
  Serial.begin(57600);
  FreqCount.begin(1000*1);  // Gate time in uSec
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
    //delay(1000);
  }
}

With the delay() commented out the result is as expected, a 40MHz input shows 40000 +/-1
With the delay enabled the result is 40000417.
It looks to me as if the sampling period is being set by the loop() time and not the interval defined
by FreqCount.begin(1000*1).

Am I doing something stupid?
(It wouldn't be the first time! :D)

RichardL
 
Last edited:
Hi,
Further observations on FreqCount() performance
===============================
This code does not exhibit the same feature (bug?) as my previous example and will accept gate times as short as 1uS and works up to 70MHz.
The call to delay() in my original code (simulating 'other things' happening **) is replaced with myDelay()
While this may indicate calling delay() is a cause of the problem it does not solve the problem in my main project.

** Other things are: Monitoring a shaft encoder, inbound serial comms, stylus operations on a touch screen, updating display ... etc. all of which are coded to prevent CPU hogging. My loop() executes in 220nS when nothing is going on and about 1.3mSec when busy servicing nine peripheral items of peripheral hardware.

Observations anyone?

RichardL

Code:
void myDelay(int milliseconds){
    unsigned long time_now = 0;
    while (millis() < time_now + milliseconds) {
        //wait approx. [period] ms
    }
}


void setup(){
    Serial.begin(57600);
    FreqCount.begin(1);  // Gate time in uSec.
}


void loop(){
     if (FreqCount.available()) {
         unsigned long count = FreqCount.read();
        Serial.println(count);
    }
    myDelay(10); // Simulate lots of other things happening <<<<<<<<<<<<<<<<<<<<<
}
 
Try the original p#1 code with delayMicroseconds(100000); // value of choice
> delay() calls yield() that may lead to other stuff going on - that yield() also happens on exiting every loop().

If the above delay works like the indicate myDelay() then try adding this to the main sketch at hand?

The yield() is defined as 'weak' to the linker and a local copy in the sketch will be used instead try : void yield(){}

That prevents any testing or calling of Events code like serialEvent() for USB or Serial UART ports and eventResponder that are probably not used.
 
Status
Not open for further replies.
Back
Top