MBed, STM32Nucleo and complimentary outputs.

After a few days of trying to get the complimentary outputs on the STM32F103 Nucleo going I’ve finally succeeded. It was surprisingly simple in the end but there is a general lack of documentation and I wonder at the completeness of the PWM API. Anyway, here is a platform specific working example. It produces a two channels of complimentary output signals (i.e. 4 outputs in total) running at 1kHz with 80% duty and an deadtime of about 13.5uS.

#include "mbed.h"
DigitalOut  my_led(LED1);

/*
Pin mappings for Timer 1 (the advanced timer with deadtime)
From: https://developer.mbed.org/users/mbed_official/code/mbed-src/file/a11c0372f0ba/targets/hal/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/PeripheralPins.c
    {PA_8,  PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, 0)}, // TIM1_CH1 - Default
    {PA_9,  PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, 0)}, // TIM1_CH2 - Default
    {PA_10, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, 0)}, // TIM1_CH3 - Default
    {PB_13, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, 0)}, // TIM1_CH1N - Default
    {PB_14, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, 0)}, // TIM1_CH2N - Default
    {PB_15, PWM_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, 0)}, // TIM1_CH3N - Default
 
*/

int main()
{

    PwmOut      PhaATop(PA_8);    
    PwmOut      PhaABtm(PB_13);  // This should be the complement of PA_8    
    PwmOut      PhaBTop(PA_9);    
    PwmOut      PhaBBtm(PB_14);  // This should be the complement of PA_9   
    PhaATop.period_ms(1);
    PhaATop = 0.8;       
    PhaBTop.period_ms(1);
    PhaBTop = 0.8;             
    TIM1->CCER |= 4; //enable ch1 complimentary output
    TIM1->BDTR |= 0xff; // specify the maximum amount of deadtime required approx 13.5us - see page 356 of reference manual
    TIM1->CCER |= 64; //enable ch2 complimentary output
    TIM1->BDTR |= 0xff; // specify the maximum amount of deadtime required approx 13.5us - see page 356 of reference manual
    while (1) {
        my_led = !my_led;
        wait(0.2); // 500 ms
    }
}

8 thoughts on “MBed, STM32Nucleo and complimentary outputs.

  1. Rupesh Kumar Jha September 7, 2020 / 8:58 am

    Hi,
    Could you please help me with the phase shift in PWM.

    Like

    • fduignan September 7, 2020 / 12:31 pm

      Do you mean for 3 phase?

      Like

      • Rupesh Kymar Jha September 7, 2020 / 1:12 pm

        Thank you so much for your reply. Actually I am developing Single phase Inverter for which I need PWM for four switches. For every leg I need two complementary PWM with a certain dead time and for another leg the PWM should have a phase shift. I tried mbed and CUBEMX to get phase shifted PWM but could not find. I saw your blog which worked very well for dead time but I could not configure it with phase shift. I am new at this stuff so if you could help me out and suggest me something. I will really appreciate that.

        Like

      • Rupesh Kumar Jha September 7, 2020 / 1:46 pm

        Thank you so much for your reply. Actually I want PWM for single phase Inverter where PWMs for the switches for the same leg should be complementary with a dead time and PWMs for the another leg should have phase shift w.r.t. the first one. Basically I want two PWMs with phase shift along with other two PWMs which are complementary in nature and also having a dead time. I used your code and it worked very well for dead time but I could nor configure it for phase shift.

        Like

      • fduignan September 7, 2020 / 6:44 pm

        Could you do this in the manner of a phase locked loop? If the second waveform is arriving too late then subtract 1 from its reload value. This will slightly increase its frequency allowing it to catch up. If it is early then add 1 to its reload value. Allow a dead band around the desired value maybe to stop continuous hunting? You could achieve the same result by writing to the counter value but doing so may cause you to miss some events.

        Like

      • Rupesh Kumar Jha September 8, 2020 / 11:21 am

        I have no idea how to configure your suggestion. Anyway, thank you for your comments.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s