cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072 Alternate PWM

Posted on January 28, 2015 at 17:22

Hi guys.

I'm having a little issue about using 6 pwm on different timers, on my STM32F072.

Application is:

a board with an adjustable sensor. The adjust phase works with a generation of pwm, changing the duty cycle until a signal on another port change.

This is the actual working code, with the PWM on timer 17

/* Installing pwm and definitions */

&sharpdefine HW_TIM_PWM_OUT_DIS        TIM_CtrlPWMOutputs(TIM17, DISABLE)

&sharpdefine HW_TIM_PWM_DIS            TIM_Cmd(TIM17, DISABLE)

&sharpdefine HW_TIM_PWM_ENA            TIM_Cmd(TIM17, ENABLE)

&sharpdefine HW_TIM_PWM_CLR            TIM_SetCounter(TIM17, 0)

&sharpdefine HW_TIM_PWM_INS(duty) \

{ \

TIM_TimeBaseInitTypeDef loc; \

    TIM_OCInitTypeDef oloc; \

                                                        /* TIM clock enable */ \

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE); \

                                                        /* Time base configuration */ \

    loc.TIM_Period = 480; \

    loc.TIM_Prescaler = (word) (SystemCoreClock  / 4800000) - 1; \

    loc.TIM_ClockDivision = TIM_CKD_DIV1; \

    loc.TIM_CounterMode = TIM_CounterMode_Up; \

    TIM_TimeBaseInit(TIM17, &loc); \

                                                        /* Output compare PWM 1 */ \

    TIM_OCStructInit(&oloc); \

    oloc.TIM_OCMode = TIM_OCMode_PWM1; \

    oloc.TIM_Pulse = ((dword)480 * (dword)duty + 128UL) / 256UL; \

    oloc.TIM_OutputState = TIM_OutputState_Enable; \

    TIM_OC1Init(TIM17, &oloc); \

    TIM_CtrlPWMOutputs(TIM17, ENABLE); \

On calibration phase, this is what I actually do (with success), on a cycle where ''duty_n'' changes as described before

HW_TIM_PWM_INS(duty_n);

HW_TIM_PWM_CLR;

HW_TIM_PWM_ENA;

Hope you understand.

Now, I have to do the exact same thing for another pwm, tied on timer 2.

My issue is: since there's no way to set TIM2 on function

TIM_CtrlPWMOutputs

what can I do as ''workaround''?

Ps. I found the TIM_CCxCmd routine on library, but it's not working. Damn. Help me!

#pwm #stm32 #pwm #timer-synchronization
4 REPLIES 4
Posted on January 28, 2015 at 17:42

Hope you understand.

Not really.

Now, I have to do the exact same thing for another pwm, tied on timer 2.

 

My issue is: since there's no way to set TIM2 on function TIM_CtrlPWMOutputs what can I do as ''workaround''?

Switch the pin between GPIO and AF settings?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 29, 2015 at 08:41

What's the part you didn't understand? Maybe I could attach the whole C project 😉

Anyway, about your suggestion... I'll try and I'll let you know! Thanks

Posted on January 29, 2015 at 14:23

What's the part you didn't understand? Maybe I could attach the whole C project 😉

Describe the unknown unknowns?

Showing me the code tells me what you've done, and isn't working on the other timer, it doesn't show me what you want to achieve.

Draw a diagram of the signalling you want to come out of the pins, and how enabling/disabling the PWM outputs impacts that. Draw a diagram of the signal from the timer that isn't doing what you want, and mark clearly the behaviour you don't want contrasting it to the behaviour you do want.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 29, 2015 at 17:36

Basically, I wanted to have a workaround for the CtrlPWMOutputs routine.

Your suggestion does work, so... problem solved! Thanks for support and patience!