-
Notifications
You must be signed in to change notification settings - Fork 27
ESP8266Timer and PWM --> wdt reset #8
Comments
You have the big WDT problem because you're using "time-consuming code" inside the ISR. The best way to do inside the ISR is just to set a flag, then using the flag to run the actual "time-consuming code" Have a look at the modified code hereafter and modify the time accordingly to your use case. I expand the time to just demonstrate the operation 1.Revised code to use "lean and mean" code inside ISR : setting just a flag#include "ESP8266TimerInterrupt.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
#endif
const int sinus[] = { 0, 20, 45, 89, 134, 178, 221, 265, 308, 350, 391, 432,
472, 512, 550, 587, 623, 658, 691, 723, 754, 784,
812, 838, 863, 886, 907, 927, 945, 961, 976, 988,
999, 1007, 1014, 1019, 1022, 1023, 1023, 1023
};
const int sinuslen = sizeof(sinus) / sizeof(sinus[0]);
const int sinusschrittzeit = 60;
const long sinusdauer = sinusschrittzeit * sinuslen;
int step = 0;
int direction = 1;
ESP8266Timer ITimer;
volatile bool startRunning = false;
void runSinus(void)
{
analogWrite(LED_BUILTIN, 1023 /*sinus[step]*/);
Serial.print("step = ");
Serial.print(step);
Serial.print(", sinus = ");
Serial.println(sinus[step]);
//Serial.print(".");
step += direction;
if (step < 0)
{
step = 0;
direction = 1;
ITimer.disableTimer();
Serial.println("\nDisable ITimer");
}
if (step > sinuslen - 1)
{
step = sinuslen - 1;
direction = -1;
}
startRunning = false;
}
void ICACHE_RAM_ATTR TimerHandler(void)
{
startRunning = true;
}
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
// Interval in microsecs
if (ITimer.attachInterruptInterval(1000L /*60L*/ * 1000L, TimerHandler))
{
ITimer.disableTimer();
Serial.println("\n\nStarting ITimer OK");
}
else
Serial.println("\n\nCan't set ITimer correctly. Select another freq. or interval");
}
void loop()
{
static unsigned long ticker = 0;
if (startRunning)
{
runSinus();
}
if ( (ticker == 0) || (millis() - ticker > 60000 /*6000*/) )
{
step = 0;
direction = 1;
ticker = millis();
ITimer.enableTimer();
Serial.println("\nRestarting ITimer");
}
} 2. Terminal output
|
Thanks for the very fast support. But your solution is not what I wanted to archive. I wanted to create a isr-task that sets the pwm so I don't need the loop-stuff any more. As outlined in my example, I only want to start in the loop, then the timer isr shall walk through the sinus, each step lasting 60 ms. My code works fine without the analogWrite... |
That's just an example to show you why you got WDT reset and what's wrong with your code. You certainly can write a software timer to take care of the work, instead of relying on loop(). It's up to you now. |
The real reason seems to be that both analogWrite()/PWM and your lib use Timer1. So both together simply don't work. |
That's correct. But Timer0 is already used by WiFi and can not be used for PWM analogWrite() or creating unexpected WiFi issues and/or both. One possible workaround, if analogWrite() usage is a must, is
ESP8266 PWM REVISITED (AND REIMPLEMENTED) The ESP8266 lacks any hardware support for PWM. Any ATtiny, PIC or any ARM Cortex M0 based SoC fares better in this regard, although the smallest SoCs may have only one or two channels.
When the duty cycle > 90% is needed, just use analogWrite(pin,100) or even digitalWrite(pin, 1) |
Hi,
I try to create a "glow" at certain intervals. Current design is that I loop through my sinus-table with 60 ms and do a analogWrite(), stop at the end of the sinus-loop, then wait some time and then rinse repeat. Looping the 60 ms does not work soooo well, as you already wrote.
To run more smoothly I thought of using the ESP8266Timer to switch the 60 ms-pwm change. For demonstration the restart is done in a simple ticker-loop().
All I get is nothing near a glow but watchdog resets. "analogWrite();" seems to be the offender.
Any ideas? What did I miss?
The text was updated successfully, but these errors were encountered: