ESP8266 TIMER INTERRUPT
Posted: Sun Jul 12, 2020 9:45 am
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'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.