Nucleo F401RE and PWM basics

10 Jun 2015

Hello Everyone, I just start to study the STM32F401RE Nucleo and I'm working on example programs avaible on mbed compiler. I'm "playing" on "Nucleo_pwm2" exemple. I changed a little bit the code trying to increment the duty cycle by 10% every time occurs an interrupt. Only TIM1 has full modulation capability, so I used the PA8 pin connected to TIM1_CH1, but I have issues to generate a PWM with 0% or 100% duty cycle Can someone help me? Thanks for the answer and sorry for my poor English.

include the mbed library with this snippet

#include "mbed.h"

DigitalOut  my_led(LED1);
InterruptIn my_button(USER_BUTTON);
PwmOut      my_pwm(PA_8);

void pressed()
{
    if (my_pwm.read() < 1.0) {
        my_pwm.write(my_pwm.read() + 0.1);
        if (my_pwm.read() > 1.0) {
            my_pwm.write(1.0);
            printf("pwm set to 100 %\n");
        } else {
            printf("pwm set to %.2f %%\n", my_pwm.read() * 100);
        }
    } else {
        my_pwm.write(0);
        printf("pwm set to 0 %\n");
    }
    return;
}

int main()
{

    // Set PWM
    my_pwm.period_ms(10);                                       
    my_pwm.write(0.1);                                          
    printf("pwm set to %.2f %%\n", my_pwm.read() * 100);

    // Set button
    my_button.rise(&pressed);                                   

    while (1) {
        my_led = !my_led;
        wait(0.5);                                              // 500 ms
    }
}

Regards, Giuseppe