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
    }
}

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s