ESP8266 Timer Interrupt
Posted: Wed Mar 04, 2020 9:20 pm
Hey there...
I've been struggling with a timing problem and was hoping to maybe find some help here.
Essentially I've built an AC phase angle dimmer using an ESP8266. I've done this before using an ATmega328P and it works fine.
Essentially a pin change interrupt is triggered at the zero cross of the sine wave and I need the the ESP to pulse a pin high for half a millisecond or so a specific time after that. For example if the phase angle is set to 50 % the ESP would have to pulse a pin high 5000 microseconds later. On the ATmega I did this using one of the hardware timer without any issues. And I tried doing this using an ESP also with the hardware timer. Which does work, but the timing isn't stable. There is too much jitter. I've tried many different things. But so far the ONLY thing that has worked reliably (without jitter) is to delay the whole 5000 microseconds. The problem with that is that, if the phase angle is set to 10 % the ESP will jitter 9000 microseconds every 10000 microseconds so 90 % of the time. And will occasionally even crash due to watchdog resets.
Obviously delaying is a terrible solution. And I've been looking for a solution for a long time but I just can't find any way to do this. I even tried setting the hardware timer to earlier than necessary and then going into a while loop and waiting the last 1000 microseconds to ensure accurate timing. I tried doing it in the main loop and delaying the last X microseconds in a while loop to ensure accurate timing. But for some reason the jitters always persists. It seems as if micros() itself loses accuracy as soon as the processor does anything else in the meantime.
I'm using a generic ESP8266 (ESP-12) with the Arduino Core and currently I'm using Wifi and AsyncWebserver to control the phase angle. I tried to oberseve the jitter with an oscilloscope. And it seems as though the timing is generally accurate but every now and then it is about 50 microseconds later than normal. So I assume it's something like the wifi ISRs or such messung up the timing. I tried looking for ways to set ISR priority etc. But unfortunately I can't find a whole lot of information about this online.
Anyway, sorry for the long explanation. If anyone has any experience with this or can point me into the right direction that would be awesome.
Thank you in advance!
Regards
I've been struggling with a timing problem and was hoping to maybe find some help here.
Essentially I've built an AC phase angle dimmer using an ESP8266. I've done this before using an ATmega328P and it works fine.
Essentially a pin change interrupt is triggered at the zero cross of the sine wave and I need the the ESP to pulse a pin high for half a millisecond or so a specific time after that. For example if the phase angle is set to 50 % the ESP would have to pulse a pin high 5000 microseconds later. On the ATmega I did this using one of the hardware timer without any issues. And I tried doing this using an ESP also with the hardware timer. Which does work, but the timing isn't stable. There is too much jitter. I've tried many different things. But so far the ONLY thing that has worked reliably (without jitter) is to delay the whole 5000 microseconds. The problem with that is that, if the phase angle is set to 10 % the ESP will jitter 9000 microseconds every 10000 microseconds so 90 % of the time. And will occasionally even crash due to watchdog resets.
Obviously delaying is a terrible solution. And I've been looking for a solution for a long time but I just can't find any way to do this. I even tried setting the hardware timer to earlier than necessary and then going into a while loop and waiting the last 1000 microseconds to ensure accurate timing. I tried doing it in the main loop and delaying the last X microseconds in a while loop to ensure accurate timing. But for some reason the jitters always persists. It seems as if micros() itself loses accuracy as soon as the processor does anything else in the meantime.
I'm using a generic ESP8266 (ESP-12) with the Arduino Core and currently I'm using Wifi and AsyncWebserver to control the phase angle. I tried to oberseve the jitter with an oscilloscope. And it seems as though the timing is generally accurate but every now and then it is about 50 microseconds later than normal. So I assume it's something like the wifi ISRs or such messung up the timing. I tried looking for ways to set ISR priority etc. But unfortunately I can't find a whole lot of information about this online.
Anyway, sorry for the long explanation. If anyone has any experience with this or can point me into the right direction that would be awesome.
Thank you in advance!
Regards