Timers and interrupts not working as expected
Posted: Fri Jan 12, 2024 7:34 pm
Hello, I'm facing some unexpected behavior regarding timers and interrupts. I'm hoping to get some insights or suggestions.
My intention is to use the timerWrite(timer, 50); function to set the timer counter to 50, then have it count up to 100, trigger an interrupt, and reset to zero because of the auto-reload feature. However, the behavior I'm observing is different. Once I set timerWrite(timer, 50);, the timer is counting only between 50 and 100 and does not reset to zero. It only resets to zero if I explicitly call timerWrite(timer, 0);.
Here is code for reference:
My intention is to use the timerWrite(timer, 50); function to set the timer counter to 50, then have it count up to 100, trigger an interrupt, and reset to zero because of the auto-reload feature. However, the behavior I'm observing is different. Once I set timerWrite(timer, 50);, the timer is counting only between 50 and 100 and does not reset to zero. It only resets to zero if I explicitly call timerWrite(timer, 0);.
Here is code for reference:
Code: Select all
hw_timer_t * timer = NULL;
void IRAM_ATTR onTimer()
{
bool state = digitalRead(5);
digitalWrite(5, !state);
}
void setup() {
pinMode(5, OUTPUT);
Serial.begin(115200);
timer = timerBegin(0, 80, true); // Timer 0, prescaler 80, count up
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 100, true);
timerAlarmEnable(timer);
timerWrite(timer, 50);
}
void loop() {
}