Odd hardware timer behavior

Weskrauser
Posts: 10
Joined: Thu Sep 07, 2017 8:39 pm

Odd hardware timer behavior

Postby Weskrauser » 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:

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;
}
...
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:

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);

}
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

Weskrauser
Posts: 10
Joined: Thu Sep 07, 2017 8:39 pm

Re: Odd hardware timer behavior

Postby Weskrauser » Sun Jan 14, 2018 10:34 am

Hello,

just to give an answer if someone is facing the same problem with timerRead:

-Use timer_get_counter_value instead of timerRead, it works perfectly!

Who is online

Users browsing this forum: No registered users and 38 guests