11 years, 2 months ago.

Problem changing PWM on the fly

Hi all,

I am currently utilizing a PWM pin to be a "simulator" for some hardware that I am going to build. Basically I am going to use the PWM to drive a stepper driver (from Pololu) and I'm using an opto-interrultor connected to a pin on the mbed and that pin is configured as an interrupt. This way I can count the interrupt time and be certain that the motor is actually moving at the RPM expected.

In the meantime I have the PWM directly driving the interrupt pin. (I'm only counting a "pulse" every 200th interrupt since the stepper takes 200 steps per revolution.)

The problem I am having is that when I go to change the period of the pulses on the PWM, the interrupts stop happening.

Here are is a snippet of my code:

    motorPulse.rise(&revolution); // tet up ISR
    motorSim.period_us(1000); // just set an inital timing for the PWM
    motorSim.pulsewidth_us(2); // motor driver requires only a 2uS pulse to acheive a step
    wait(10); // Let me clear the serial terminal
    char buf[1024];
    int ptr = 0;
    while(1)
    {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
        if(pc.readable())
        {
            buf[ptr] = pc.getc();
            if(buf[ptr] == '\r')
            {
                buf[ptr] = '\0';
                ptr = 0;
                double period = ((double)atof(buf) / 60.0) * 1000.0;
                int period_ms = (period / 200.0) * (double) 1000.0;
                pc.printf("\x1b[3;0fbuf = %s, period = %f, period_ms = %i\n", buf, period, period_ms);
                motorSim.period_ms(period_ms);
            }
            else
            {
                ptr++;
            }
        }
    }

So once I get input on the serial line and convert it into a number I calculate and change the period using period_ms(); but when that happens my interrupt code stops firing. So I am assuming that my change to the period is stopping the PWM output.

Any suggestions as to how I can change the period without killing the PWM?

Thanks in advance!

Did you check what happens when you comment out the line which changes the period of the pwm, but keep the rest the same? Or the other way around, change the period without user input. So for example wait 10 seconds, then call period_us(2000) or something similar.

posted by Erik - 15 Feb 2013

Great debugging suggestion! And if I make the change after 10sec I get the same behavior, so the serial input is not the problem it is just making the change.

posted by Tim Borland 15 Feb 2013

OK, so I even tried switching the PwmOut variable to a point ernd then using the code below:

    motorSim = new PwmOut(p21);
    motorPulse.rise(&revolution);
    motorSim->period_us(1000);
    motorSim->pulsewidth_us(2);
    wait(10);
    char buf[1024];
    int ptr = 0;
    //pc.printf("\r\n\r\n"); //Position to where input will be
    displayUpdater.attach(&updateDisplay, 1.0);
    delete motorSim;
    motorSim = new PwmOut(p21);
                motorSim->period_ms(2000);
                motorSim->pulsewidth_us(2);

Still the same result, after changing the output period, the PWM channel appears to stop working.

I'd really appreciate someone from the mbed team weighing in on thos. I am really beginning to think I found a bug in the code.

posted by Tim Borland 18 Feb 2013
Be the first to answer this question.