I am generating a PWM signal using the LEDC functions. I need to trigger an interrupt every n pulses, tms after the rising edge.
I tried simply by starting the PWM, then delaying n and starting a timer with an interrupt, but this seems to be unpredictable and unstable. I think this is because I can't determine the exact delay between starting the PWM and actually starting the timer with the interrupt. I can't seem to start the timer at exactly the right moment.
I could trigger an interrupt on every pulse and just check if it is one I want, but I feel this generates a lot of interrupting for nothing. Is there a way to syncronize a separate timer with the timer used to by LEDC to generate the PWM?
This is what I have so far:
Code: Select all
void IRAM_ATTR ControlPilot::read_cp(){
adcStart(CP_READ);
}
void ControlPilot::BeginPulse(){
//Start the timer and begin pulsing the CP line
ledcAttachPin(CP_PWM, 0);
ledcWrite(0, Configuration::GetCpPwmDutyCycle());
delayMicroseconds(30);
intTimer = timerBegin(0, 80, true);
//Timer to read CP voltage every 10ms
timerAttachInterrupt(intTimer, &read_cp, true);
timerAlarmWrite(intTimer, 10000, true);
timerAlarmEnable(intTimer);
pulsing = true;
}