FreqMeasure Teensy 3.2 Library question - measuring Freq and PWM (or low time)

Status
Not open for further replies.

tadzio93

Member
Hey guys, I'm trying to use the FreqMeasure library from 50Hz - 150Hz. I have already found the frequency measure to be very accurate and it is meeting my requirements perfectly. I am also looking to see what the negative (or positive, both work) duty cycle is of the signal to calculate the low time. My signal is encoded in both the frequency and negative pulse width (which I can measure from positive duty cycle).

For reference: I have tried using the pulseIn function that comes with AVR, and it has a bit of error it it. I don't know if it is due to it being a very generic function and has some overhead or if it is a different problem. (For a frequency of 75Hz, I can see anywhere from 74 to 76). I would like to be within at least 1/2 of a Hz.

Is there a way to edit the FreqMeasure library to output the PWM which would let me calculate negative pulse width? Any help and guidance would be appreciated :).
 
Here is code written with interrupts, however I see a pretty big error with this too, which I'm not sure about.

void loop() {
timeStart = millis();
attachInterrupt(digitalPinToInterrupt(ETHANOL_SENS_PIN), falling, FALLING);

delay(timeStart + 100 - millis());
}

void falling() {
t1 = micros();
//detachInterrupt(digitalPinToInterrupt(ETHANOL_SENS_PIN));
attachInterrupt(digitalPinToInterrupt(ETHANOL_SENS_PIN), rising, RISING);
}

void rising() {
t2 = micros();
//detachInterrupt(digitalPinToInterrupt(ETHANOL_SENS_PIN));
attachInterrupt(digitalPinToInterrupt(ETHANOL_SENS_PIN), falling_2, FALLING);
}

void falling_2() {
t3 = micros();
detachInterrupt(digitalPinToInterrupt(ETHANOL_SENS_PIN));
period = t3 - t1;
freq = 1000000.0/period;
// Some other return code
}

OUTPUT: https://imgur.com/A2rMIPW

As you can see, something is causing the signal to fluctuate and it is fairly repeating. Any ideas?
 
Status
Not open for further replies.
Back
Top