Timer and PWM issue
Posted: Sun Oct 08, 2017 7:08 pm
This works perfectly the first time toneOn() is executed, after that the ISR is never called. I tried disabling all the ledc* functions to see if there was a conflict, with the same result. Is it ok to call timerAlarmDisable() from within the ISR? Thanks in advance.
Code: Select all
byte buzzerPin = 12;
hw_timer_t * toneTimer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
/*----------------------------------------------------------------------*
toneOff() ISR
----------------------------------------------------------------------*/
void IRAM_ATTR toneOff() {
timerAlarmDisable(toneTimer);
ledcWriteTone(4, 0);
Serial.println("OFF");
}
void setup() {
Serial.begin(115200);
while (!Serial);
toneTimer = timerBegin(1, 80, true); // initialize timer - timer_id = 1; divider=80; countUp = true;
timerAttachInterrupt(toneTimer, &toneOff, true); // edge = true
ledcAttachPin(buzzerPin, 4);
ledcSetup(4, 2000, 8);
}
void loop() {
toneOn(50, 40, 1000); // for testing
delay(2000);
}
void toneOn(byte volume, int dur, int frequency) {
ledcWriteTone(4, frequency);
ledcWrite(4, volume);
timerAlarmWrite(toneTimer, dur * 1000, true); // 1000 per ms
timerAlarmEnable(toneTimer);
Serial.println("ON");
}