9 years, 2 months ago.

PwmOutput and interrupt (LPC1114)

I have PWM output and I have to do stuff when pwm go high. Can I somehow make interrupt when pwm pulse go high?

I mean something like this:

#include "mbed.h"
 
PwmOut pwm(dp24);

void PWM_IRQHandler(void)
{
//Do things
    return;
}
 
int main() {
    //Init PWM
    pwm.period_ms(100);
    pwm.pulsewidth_ms(10);

    while(1){

    }
}

2 Answers

9 years ago.

I realize this is pretty late, but I was curious myself :)

Taking Erik's suggestion, I tried setting up a PWM output and an interrupt on the same pin. Here's my code:

#include "mbed.h"

InterruptIn pwm_int(dp1);
PwmOut pwm(dp1);
DigitalOut go(dp2, 0);
 
void PWM_IRQHandler(void)
{
    go = 1;
    go = 0;
    return;
}
 
int main() {
    //Init PWM
    pwm_int.rise(&PWM_IRQHandler);
    pwm.period_ms(20);
    pwm.pulsewidth_ms(4);
 
    while(1){
 
    }
}

I hooked up my logic analyzer and first checked to make sure the interrupts were occurring: /media/uploads/bridadan/pwm_int_consistent.png

Success!

Now checking for the interrupt delay: /media/uploads/bridadan/pwm_int_timing.png

4.8us, not too shabby! I'm sure if I used the timing interrupt instead of the pin interrupt it would be faster, but this seemed satisfactory for most needs.

This was tested on the LPC1114. The timing will definitely vary depending on the platform.

Accepted Answer
9 years, 2 months ago.

Depending on your frequency you can either use an InterruptIn connected to the PWM (might even be possible on the same pin, not 100% sure InterruptIn can do the same pin, WakeInterruptIn library (you can search for it) can do it for sure, if the pin is supported for that. That is the easy solution.

Alternatively you can also search which timer is used by for dp24 pwm, and enable interrupt. It can do that, but you need to dive into the reference manual.