I thought this would be a simple matter but I have spent DAYS trying to resolve it for myself with no luck.
I simply want to create a countdown timer using hardware interrupts (because there a number of other things going on) and that the countdown appear on a display, counting down seconds to zero. Therefore, the display should be refreshed every second.
I have searched the internet for an example of setting the direction of countdown and found only ONE example. And that was changed.
The key points are (I think):
timer = timerBegin(0, 80, false); - this should be set to 'false' to count down, according to the documentation. Is that correct?
timerWrite(timer,10000); - this is supposed to be the value from which to count down but I can't find anywhere to tell me what the units are.
I don't want the counter to start immediately - I thought that could be achieved using the timerStart. So, do I need timerBegin?
I want to display the number of seconds remaining on the display: can that be achieved using "timerReadSeconds(timer)"?
Thanks,
Connal
Here is the code I have so far (which doesn't work).
Code: Select all
volatile bool interruptCounter;
int timeStat;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
interruptCounter = true;
portEXIT_CRITICAL_ISR(&timerMux);
}
void setup() {
Serial.begin(115200);
timer = timerBegin(0, 80, false);
timerWrite(timer,10000);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
}
void loop() {
if (interruptCounter) {
portENTER_CRITICAL(&timerMux);
interruptCounter = false;
timeStat = timerReadSeconds(timer);
portEXIT_CRITICAL(&timerMux);
Serial.print(timeStat);
Serial.println(" seconds");
}
}