Odd hardware timer behavior
Posted: Sun Jan 07, 2018 7:08 pm
Hello,
I tried to use built in 64 bits hardware timers to measure pulse width inside interrupts using following method:
Pseudo code:
I first tryed with a 10khz PWM signal and the width measure was fine. I tryed after that with an ultrasound pulse high signal and started to see completly wrong values... In this case, the only way I found to make the measure works was with replacing timerRead(timer) by micros() (with the 80 prescaler it should have been the same... )
Also I tryed this simple example to find out an explanation:
Depending on the value of Time_To_Read, the width measure is always false from (long)timerRead(timer) - t_now2 method, often true with micros() - t_now but can be sometime false as well (Time_To_Read = 50 for example).
I tryed to change timer number with timer = timerBegin(x, 80, true); without effect.
Something I missed?
Thanks in advance,
Denis
I tried to use built in 64 bits hardware timers to measure pulse width inside interrupts using following method:
Pseudo code:
Code: Select all
timer = timerBegin(0, 80, true);
attach interrupt RISING;
rising_interrupt() {
t_now = timerRead(timer);
attach interrupt FALLING;
}
falling_interrupt() {
Pulse_Width = timerRead(timer) - t_now;
attach interrupt RISING;
}
...
Also I tryed this simple example to find out an explanation:
Code: Select all
long t_now;
long t_now2;
uint16_t Time_To_Read = 500;
hw_timer_t * timer = NULL;
void setup() {
Serial.begin(250000);
timer = timerBegin(1, 80, true);
}
void loop() {
t_now = micros();
t_now2 = (long)timerRead(timer);
delayMicroseconds(Time_To_Read);
Serial.print((long)timerRead(timer) - t_now2);
Serial.print("\t");
Serial.println(micros() - t_now);
delayMicroseconds(10);
}
I tryed to change timer number with timer = timerBegin(x, 80, true); without effect.
Something I missed?
Thanks in advance,
Denis