11 years ago.

PwmOut, HighPWM, and FastPWM not working

I'm writing a fairly complex program (by my standards at least), and I need to use a PWM output to provide clocking to a device. The clock has to be 12MHz so I have to use the HighPWM or FastPWM libraries to get that kind of frequency. The issue I'm having is that the PWM works in a blank program, but doesn't work in mine.

Here are my subsystem delcarations:

DigitalOut led1(LED1);
HighPWM spi_clk(p26);

//Timer t;

SPI spi(p5, p6, p7);
DigitalOut cs(p8);
 
DigitalIn i_d(p15);
DigitalIn i_clk(p16);
//InterruptIn i_int_clk(p16);

DigitalOut o_d(p17); 
DigitalOut o_clk(p18);
//InterruptIn o_int_clk(p18);

USBKeyboard keyboard;

As you can see, I've tried enabling and disabling declarations, changing the order, so on and so forth, but that hasn't changed anything. Except, when I put the PWM declaration first, the pin goes low. If I put it in the middle or last, the PWM doesn't activate at all and the voltage stays around 2.3V.

In my main method, I have:

int main() {

    spi_clk.period_ticks(8);
    spi_clk.pulsewidth_ticks(4);
    
    while(1) {
    }
}

I'm at a loss here. If I just use the PWM in a blank program, it runs smoothly but it refuses to run in my code. I didn't put the entire code here because it's about 600 lines and nobody wants to read through all that. Is there a clash of subsystems somewhere that I don't know about? I thought that maybe the SPI clock was causing issues, but I tried all 6 PWM channels and got the exact same result. I also tried the other PWM libraries but all had the same result.

EDIT: I've found out that it's the USBKeyboard library that's causing the issue. I can't seem to find a way for them to hold hands and be friendly. Do you guys have any suggestions?

Does the rest of your code run? Because USBKeyboard should have no influence on the PWM peripheral, unless it never gets to instantiation of the PWM object.

posted by Erik - 17 Nov 2013

I didn't get to test any other code but I assume that it should run fine. I did some feng shui and put the USBKeyboard declaration in the main() function and put a short wait before it. That seemed to do the trick, but obviously that means that I can't use the USBKeyboard in any functions. It won't work if I do it in reverse order (instantiate keyboard first then wait, then instantiate PWM). It's a curious issue indeed but I don't think I'll need to use the keyboard in any functions. It will make my code messier, but the end user won't be looking at the code.

posted by Ankit Patel 17 Nov 2013

All classes do all initialisations BEFORE main starts. The USBKeyboard may hang in its init when it is not connected to a USB host and does not complete enumerations. That probably means your main will not start and will not call spi_clk.period_ticks(8) and spi_clk.pulsewidth_ticks(4), which explains why the PWM does not start.

posted by Wim Huiskamp 17 Nov 2013

That makes sense! I guess I'll just stick with putting the USBKeyboard instantiation in the main function.

posted by Ankit Patel 17 Nov 2013
Be the first to answer this question.