Hello,
I've been experimenting with (and possibly abusing?) the FreqMeasure library on a Teensy 3.1. I'm using it to read an encoded pulse stream that encodes binary data as a series of 16us and 24us pulses (idle = high, 16us with 8 us low pulse followed by 8us high = bit “0”, and 24us with 16us low followed by 8us high = bit "1"). I'm compiling my code in the IDE with clock speed set to 96 MHz.
The documentation at https://www.pjrc.com/teensy/td_libs_FreqMeasure.html states the following for FreqMeasure.read:
FreqMeasure.read();
Read a measurement. An unsigned long (32 bits) containing the number of CPU clock cycles that elapsed during one cycle of the waveform. Each measurement begins immediately after the prior one without any delay, so several measurements may be averaged together for better resolution.
From this I expect FreqMeasure.read() to give me (more or less) a series of 16*96=1536 and 24*96=2304 values indicating the pulse with. Instead it gives me a series of ~768 and ~1152 pulse widths. So almost exactly half of what I expected based on the function description. The great news is that it works quite reliably. But I would like to better understand why I get the values that I am seeing.
1.) I assume FreqMeasure.read() gives the elapsed time from rising to rising or falling to falling clock edges. Is this a correct assumption and is this an adjustable setting?
2.) Is the actual .read clock frequency indeed 1/2 of the CPU clock speed?
3.) Is there a more appropriate library to do what I am attempting to do here?
I've started going over the library code at the detail level but I have a ways to go understanding the FTM registers. Any help or pointers would be greatly appreciated.
I am using the following code (a lightly modified version of the Serial_Output example).
And I get this as typical serial stream output:
Thanks!
George
Pulse stream on Chn0 (ignore the rest) as measured on my Saleae Logic analyzer:

I've been experimenting with (and possibly abusing?) the FreqMeasure library on a Teensy 3.1. I'm using it to read an encoded pulse stream that encodes binary data as a series of 16us and 24us pulses (idle = high, 16us with 8 us low pulse followed by 8us high = bit “0”, and 24us with 16us low followed by 8us high = bit "1"). I'm compiling my code in the IDE with clock speed set to 96 MHz.
The documentation at https://www.pjrc.com/teensy/td_libs_FreqMeasure.html states the following for FreqMeasure.read:
FreqMeasure.read();
Read a measurement. An unsigned long (32 bits) containing the number of CPU clock cycles that elapsed during one cycle of the waveform. Each measurement begins immediately after the prior one without any delay, so several measurements may be averaged together for better resolution.
From this I expect FreqMeasure.read() to give me (more or less) a series of 16*96=1536 and 24*96=2304 values indicating the pulse with. Instead it gives me a series of ~768 and ~1152 pulse widths. So almost exactly half of what I expected based on the function description. The great news is that it works quite reliably. But I would like to better understand why I get the values that I am seeing.
1.) I assume FreqMeasure.read() gives the elapsed time from rising to rising or falling to falling clock edges. Is this a correct assumption and is this an adjustable setting?
2.) Is the actual .read clock frequency indeed 1/2 of the CPU clock speed?
3.) Is there a more appropriate library to do what I am attempting to do here?
I've started going over the library code at the detail level but I have a ways to go understanding the FTM registers. Any help or pointers would be greatly appreciated.
I am using the following code (a lightly modified version of the Serial_Output example).
Code:
/* FreqMeasure - Example with serial output
* http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
*
* This example code is in the public domain.
*/
#include <FreqMeasure.h>
void setup() {
Serial.begin(115200);
FreqMeasure.begin();
}
double f=0;
int count=0;
void loop() {
if (FreqMeasure.available()) {
// average several reading together
f = FreqMeasure.read();
count = count + 1;
if (count > 30) {
//float frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(f);
f = 0;
count = 0;
}
}
}
And I get this as typical serial stream output:
Code:
768
765
768
768
1155
765
768
768
768
1152
771
768
768
1149
1149
768
1155
771
768
1152
1152
771
768
768
768
292225
768
765
768
1152
774
765
1146
771
768
1155
762
Thanks!
George
Pulse stream on Chn0 (ignore the rest) as measured on my Saleae Logic analyzer:

Last edited: