Page 1 of 1

ESP32 timer interrupt problem [SOLVED]

Posted: Tue May 28, 2024 3:17 pm
by Sasi
Hello,

I would like to ask for help.
Now I'm stuck on timer interrupts. :(
According to my intention, the attached program would produce a 500Hz square signal on the Pin17 output.
Instead, Pin17 will go low level for 700us every 480ms.
In fact, the MCU restarts every 480ms because the pin12 pin also behaves the same way.
Timer interrupt test.fcfx
(10.38 KiB) Downloaded 714 times
Regards,
Sasi

Re: ESP32 timer interrupt problem

Posted: Tue May 28, 2024 3:45 pm
by medelec35
Hello.
have you tried setting the pin directly from within the interrupt, instead of the variable value?
Also before the main loop, set a pin hight for say 2 seconds then low again, with an LED attached to it.
That way you can tell if the chip is resetting due to the watchdog timer.

Re: ESP32 timer interrupt problem

Posted: Tue May 28, 2024 4:18 pm
by Sasi
Hi Martin,

Yes, I tried setting port 17 directly in the ISR, the result is the same. In the test program, I used port 12 to demonstrate MCU restart. It goes low level when the program starts, and goes high level in the main loop. It can only go low lewel if the MCU reboots for some reason. However, this pin of the port behaves the same as 17.

Sasi

Re: ESP32 timer interrupt problem

Posted: Tue May 28, 2024 5:19 pm
by mnfisher
As the code stands - the wdt will cause a reboot repeatedly. Your main loop needs to have a delay in it to allow the (co-operative) multi-tasking to work (and there are at least 5 tasks running created by the RTOS)

One option might be to create a separate task (with xTaskCreate) -

(In pseudocode)

.led = false
Loop
.led = !.led
set pin .led
delay 2ms
end loop

But I don't think that a 2ms second delay is long enough - so this technique would work for slower pulse rates but not 500Hz...
So - plan B is to do the job in the timer ISR and toggle the pin every time the ISR is called.

ISR:
$PIN17 = !$PIN17

Note that this isn't 'guaranteed' to give exact times (it relies on the multi-tasking and tasks 'yielding') but I've found this to be pretty good!

Plan C is to either use the SPI or RMT hardware to do the job - perfect if the timing needs to be 'just so' - but rather more involved to write :-(


Martin

Re: ESP32 timer interrupt problem

Posted: Tue May 28, 2024 6:52 pm
by Sasi
Hi Martin,

Thank you for your reply and suggestions.
According to them, I have to think differently when using ESP32 than with Microchip processors. (this is my first more complex project with this MCU)
So now I understand why Martin (medelec35) suggested flashing the LED in the main loop.
I'm doing a bit of reading on the subject now. :)

Regards,
Sasi

Re: ESP32 timer interrupt problem [SOLVED]

Posted: Tue May 28, 2024 10:28 pm
by mnfisher
It is an amazingly powerful chip - with a lot of features. It does take a slightly different 'mindset' to MCUs that run a single program though..

I did a very simple SQW example using a timer. Pulses are 2.00 or 2.01ms according to my scope. However the chip is doing nothing except looping on a delay. The pin to toggle is in properties (Pin1) - I used A22.
sqw.fcfx
(9.01 KiB) Downloaded 731 times
Martin

Re: ESP32 timer interrupt problem [SOLVED]

Posted: Wed May 29, 2024 8:42 am
by Sasi
Thank you very much for your help.
I tried the program you provided. Interrupt timing accuracy will be on target. :D
Regards,
Sasi

Re: ESP32 timer interrupt problem [SOLVED]

Posted: Wed May 29, 2024 2:11 pm
by mnfisher
Cool - glad it's working for you...