-->
Page 1 of 2

Problem: analogWrite() disable timer1 interrupt

PostPosted: Tue Jun 04, 2019 12:54 am
by Rofl92
Hi.
The timer1 works fine until i use the function analogWrite(pin, state).
What could be the problem?
If analogWrite() uses a timer1, how do I work with the analogWrite() and the timer1 at the same time? The Ticker library is not suitable for solving my problems, I need a hardware timer.

Code: Select all#define PIN_LED 2
bool b = false;
void ICACHE_RAM_ATTR timer_callback(void){
  b=!b;
  Serial.print(b);
  wdt_reset();
}
void setup() {
  pinMode(PIN_LED, OUTPUT);
  Serial.begin(115200);
  timer1_disable();
  timer1_attachInterrupt(timer_callback);
  timer1_isr_init();
  timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
  timer1_write(100000);

  delay(1000);
  analogWrite(PIN_LED, 512);
  Serial.print("analogWrite enabled");

//Set up ESP watchdog
  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);
}
void loop() {

}


DEBUG:
Code: Select all10101010101010101010101010101010101010101010101010analogWrite enabled

Platform
  • Hardware: [ESP-12E]
  • Core Version: [latest]
  • Development Env: [Arduino IDE]
  • Operating System: [Windows]
Settings in IDE
  • Module: [Nodemcu]
  • Flash Size: [4MB/3MB]
  • lwip Variant: [v2 Lower Memory]
  • Reset Method: [nodemcu]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [SERIAL]
  • Upload Speed: [921600]

Re: Problem: analogWrite() disable timer1 interrupt

PostPosted: Tue Jun 04, 2019 3:02 pm
by schufti
from what I read here I would say that timer1 is "not free" at least.
Try using timer0 as this seems to be free unless you use servo lib.
Looking at core_esp8266_wiring_pwm.c and core_esp8266_waveform.c confirms the suspicion.

Re: Problem: analogWrite() disable timer1 interrupt

PostPosted: Tue Jun 04, 2019 3:56 pm
by rudy
The ESP8266 is a fantastic bargain. It is not rich in hardware as a more general microcontroller. You learn how to live with the restrictions, or you add additional capabilities, like a co-processor.

Re: Problem: analogWrite() disable timer1 interrupt

PostPosted: Wed Sep 11, 2019 4:16 am
by theenggprojects
You should work on ESP8266 as it's quite easy to control and analogwrite is exactly the same as for Arduino UNO. I have designed projects on it, worked pretty neat.