FreqMeasureMulti nanosecond issue

Status
Not open for further replies.

turtle9er

Well-known member
Hi,

I have been using the freqmeasuremulti library to measure frequency very well, however now I want to use the function freqmeasuremulti.countToNanoseconds() and I am getting some odd results. I tested it in my own program that is measuring the rpm of a bicycle crank and noticed an issue, so used a very stripped down version of the example provided and still get the wrong nanosecond values. I even put in 60,000,000 as the count expecting 1 billion ns as the return value, however I get 69,423,752. I have looked in the source code and have done the math by hand and it seems to work, but the value I am getting is not correct. I have also compared the count value vs the pulse width on the oscilloscope and they match up, so something is happening with the function to return the correct ns value. Maybe it is some simple error I am doing, but can't figure it out for the life of me. Thanks in advance.
PS- Hooked up a signal generator just to test more HZ and it seems to stop sending the correct nanoseconds at 13.9hz, 14hz is fine (71427248ns), however 13.9hz gives me 358328.41ns, but the sum of the count is correct. This is driving me crazy and hope someone has a bit more insight into the issue. Also countToFrequency returns the correct value in Hz.

Code:
#include <FreqMeasureMulti.h>

// Measure 3 frequencies at the same time! :-)
FreqMeasureMulti freq1;


void setup() {
  Serial.begin(57600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(10);
  Serial.println("FreqMeasureMulti Begin");
  delay(10);
  freq1.begin(6);

}

float sum1=0, sum2=0, sum3=0;
int count1=0, count2=0, count3=0;
elapsedMillis timeout;
static uint32_t a=60000000;//should be 1,000,000,000ns

void loop() {
  if (freq1.available()) {
    sum1 = sum1 + freq1.read();
    count1 = count1 + 1;
  }

  // print results every half second
  if (timeout > 500) {
    if (count1 > 0) {
      Serial.print(freq1.countToNanoseconds(a));
    } else {
      Serial.print("(no pulses)");
    }

    Serial.println();
    sum1 = 0;
    sum2 = 0;
    sum3 = 0;
    count1 = 0;
    count2 = 0;
    count3 = 0;
    timeout = 0;
  }
}
 
Last edited:
Code:
float FreqMeasureMulti::countToNanoseconds(uint32_t count)
{
#if defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
	return (float)([B]count * 1000[/B]) / (float)(F_BUS / 1000000);
#elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
	return (float)(count * 1000) / (float)(F_PLL / 2000000);
#else
	return 0.0;
#endif
}

Your count is too large. 'count * 1000' is using 32-bit math and overflows.
 
Ok thanks, guess I thought ns could be 2^32 max. But does that mean max count is 2^32/1000? That does not seem right to me also. Thanks again for the information.
 
Status
Not open for further replies.
Back
Top