Syncronize interrupt with pulses generated with LEDC

matthew798
Posts: 16
Joined: Mon Jun 01, 2020 2:31 am

Syncronize interrupt with pulses generated with LEDC

Postby matthew798 » Mon Jun 01, 2020 2:42 am

Hi all,

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;
}
So I guess it's pretty straightforward: How can I syncronize triggering an interrupt with every n pulses of a PWM signal.

Who is online

Users browsing this forum: No registered users and 92 guests