FreqCount example: strange behaviour ?

ninja2

Well-known member
I've been playing with the Serial_Output_T4 example sketch in the FreqCount library, on a T4.1
As provided the sketch reports exxactly 50MHz on pin 9, as expected.
However other frequency values don't give expected results
Requesting 40MHz reports 37.5 MHz
Requesting 10MHz reports 10 MHz
Requesting 4MHz reports 3.947368 MHz
Code:
#include <FreqCount.h>

void setup() {
  Serial.begin(57600);
 
  delay(2000);
  //alogWriteFrequency(8, 40000000);  // 40MHz - reports 37.5 MHz
  //alogWriteFrequency(8,  4000000);  //  4MHz - reports 3.947368 MHz
  analogWriteFrequency(8, 10000000);  // 10MHz - works fine
  //alogWriteFrequency(8, 50000000);  // 50MHz - works fine
  analogWrite(8, 128);
 
  FreqCount.begin(1000000);  //Time in microseconds
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
  }
}
I'm wondering what the issue is?

Also, what is purpose of this line? :
Code:
  analogWrite(8, 128);
 
The frequency generated using PWM has to either be a multiple or integer factor of that module's clock source, so some values will be rounded up or down.
analogWrite() sets the duty cycle of the signal, in this case 128 out of 256 time units per cycle (50%). See https://www.pjrc.com/teensy/td_pulse.html for more info.
 
Ahah, OK thanks.
Basically the sketch uses a generator with a limited selection of set frequencies.
If instead I use an external pulse generator as the input source I expect FreqCount will report correclly.
I must give that a go ... thanks!
 
Back
Top