7 years, 9 months ago.

can i use PWM to generate 2 individual square waves? (Nucleo F411)

I want to generate 2 square waves with 50% duty cycle in the range of 0 - 25kHz. The waves need to have a varying period and both running simultaneously at different frequencies.

The following example shows the period can be set

example

#include "mbed.h"

PwmOut mypwm(PWM_OUT);

DigitalOut myled(LED1);

int main() {
    
    mypwm.period_ms(10);
    mypwm.pulsewidth_ms(1);
  
    printf("pwm set to %.2f %%\n", mypwm.read() * 100);
    
    while(1) {
        myled = !myled;
        wait(1);
    }
}

Can i set this to 2 different PWM outputs? The pinout of the board (link below) suggests this is possible can anybody confirm? https://developer.mbed.org/platforms/ST-Nucleo-F411RE/

I'm assuming that the PWM has its own dedicated timer, is this correct?

Also the pinout uses notation such as PWM1/1, PWM2/1, PWM1/2 and ect. any idea what this means?

1 Answer

7 years, 9 months ago.

You may want to double check this since it varies platform to platform but I think you'll be OK as long as you pick the pins carefully. On some platforms PWM outputs can each have a different duty cycle but must all use a common frequency.

From memory on the F411 all of the PWM1/n outputs use the same timer and so must have the same frequency but the PWM2/n pins will be on a different timer and so can use a different frequency. As long as you use pins that are on different timers you should be OK.

Accepted Answer

I can confirm tested 2 different frequencies and worked fine when checking on the scope.

#include "mbed.h"

PwmOut ch1(D7); //PWM1/1
PwmOut ch2(D6); //PWM2/3


DigitalOut myled(LED1);

int main() {
    
    while(1) {
        
    ch1.period_us(100);
    ch1.write(0.5);
    
    ch2.period_us(50);
    ch2.write(0.5);
     myled = !myled;
        wait(1);
    }
}

posted by Narshil tibbers 21 Jul 2016