FreqMeasure stops reporting values after a few seconds

Status
Not open for further replies.

wargpath

New member
Hi, I am a new teensy 3.2 owner and I am experimenting with the FreqMeasure library. I am seeing the library work for just a few seconds, and then it ceases to print out frequency data. It works for about 5 seconds, then it stops. If I swap the teensy out for an arduino uno, it works as expected.

I'm using the sample code provided.

Code:
#include <FreqMeasure.h>

void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}

The sketch continues to run, but FreqMeasure.available() ceases to evaluate to true after a few seconds, stopping the printout of the frequency information.

This post seems to report a similar issue.
https://forum.pjrc.com/threads/26173-FreqMeasure-freezing

Anybody else encountered this or have any troubleshooting tips?
Thanks!
 
I happen to have a T_3.6 here blinking away running a ZILCH example. Using this as my source freq it works as below on T_3.6 and T_3.0 and T_3.2 and shows freq about 12.5.

I copied your posted code to a T_3.6 - wired pin 3 to T_3.6's LED and it was running fine. The other Teensy on my desk is a 3.0 so I moved the wire to it's pin 3 and sent the sketch over and it ran well a couple minutes.

The only time it doesn't run is when there isn't a measurable freq on the pin it seems? If I remove the wire or try going through a breadboard.

I pulled over a T_3.2 and it works when I touch the LED wire to pin3. But I routed through a breadboard to a second wire and get no measure. How is your pin3 wired to your source freq?

Adding a blink to your sketch so you can see if the Teensy is still running. Tested to work as below. Then if printing stops and the blink continues the sketch is running - if not something is taking it down. Then if you saw continuous blink - remove the other pin 3 wire and put one over from Pin 13 LED and re-run it - the LED should be cycling at 10/sec.

On my T_3.6 I get 10.00 at 72 MHz and 240 MHz with the code below. If I take out the "if ( some_time > 50 ) {...}" code and just qBlink() each loop() at 72 MHz I get 102,857.14 and at 240 MHz I get 337,078.66 and 171,428.58 at 120 MHz. I set to this to reduce printing at this higher sample rate: "if (count > 10000)". Then looking a bit more I changed to "float sum = 0;" and the T_3.6 ( with single precision FPU ) now gets a bit faster at 419,580.41. And it still runs well, just much faster.

Code:
#define qBlink() (digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) ))
#include <FreqMeasure.h>
// FreqMeasure stops reporting values after a few seconds
// https://forum.pjrc.com/threads/40000-FreqMeasure-stops-reporting-values-after-a-few-seconds

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(57600);
  FreqMeasure.begin();
}

double sum = 0;
int count = 0;

elapsedMillis some_time;
void loop() {
  if ( some_time > 50 ) {
    some_time -= 50;
    qBlink();
  }

  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}
 
Thanks for your help defragster, first of all the qBlink technique you had in your sketch is a handy troubleshooting tool. I confirmed that the sketch continued to run (light kept flashing) after the frequency output stopped.

Next, your comment about passing the signal through a breadboard got me to thinking that somehow I was not delivering enough gain. I upped the gain and it has been working much better. I think I was only delivering something like .5V, and certainly less than 1V of amplitude from my source. That's a guess because my scope is not so good and appears to have just died!!

I was off the scent of low gain here because FreqMeasure was putting out some readings, instead of nothing, and once the frequency output stopped it would not resume without disconnecting and reconnecting the audio source, even though the amplitude of the signal had not significantly changed.
 
Awesome - glad to help. Given the trouble seeing what the Teensy is doing inside: qBlink() is a trivial way to see signs of activity!

... you are on your own with the dead scope though :)

I went to the other thread noted in OP and put a link to my post above - you might follow up there with your discovery/solution.

After writing this I'm thinking of making a dedicated debug Teensy. For starters so I can see the qBlink() Freq and perhaps a serial port monitor to feed to a display and/or USB. I justified buying a cheap DVM with FreqMeas when I was on a road trip - it is disappointing to say the least. Seeing this work so well with Teensy - it could easily do a better job. The limit of 419,580.41 measured above on the T_3.6 (both outputting the signal and reading it) was because the testing in loop() for FreqMeasure.available() limited the number of times qBlink could run.
 
If you have problems with signal amplitude, you could use this. It uses the analog comparator in the Teensy and can detect 200 mV 10 MHz signals.

void ComparatorSetup(void) {
// Output is on pin10 (PTC4, MCU pin 49, Teensy pin 10)
// Input is pin A9 (PTC2, Teensy pin 23, MCU pin 45)
// Reference is 6-bit internal DAC
// DAC reference VIN1 = VREF (1.19 V), VIN2 = VDD (3.3)

SIM_SCGC4 |= SIM_SCGC4_CMP; //Clock to Comparator
CORE_PIN10_CONFIG = PORT_PCR_MUX(6); //Alternate function 6: Teensy pin10 = CMP1_OUT, PTC4

CMP1_CR0 = 0b00000000; // FILTER_CNT=0; HYSTCTR=0
CMP1_CR1 = 0b00010111; // SE=0, high power, COUTA, output pin, enable; mode #2A
// read CMP1_SCR LSB is analog comparator output state
CMP1_DACCR = 0; //Disable DAC ==> reference = 0 V
//PTC2 (pin 23) is default CMP1_IN0
//PTC3 (pin 9) is default CMP1_IN1

// IN0 (Comparator plus) =
CMP1_MUXCR = CMP_MUX_PSEL(0) | CMP_MUX_MSEL(7); // Input pins select; plus = IN0 (pin 23), neg = DAC (code 7)
Serial.println("Comparator CMP1 Setup Complete");
}

Put your signal on pin23, and connect the output from pin10 to the frequency counter. More info at https://forum.pjrc.com/threads/3082...g-external-pulses-accurately?highlight=jp3141
 
Status
Not open for further replies.
Back
Top