cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate PWM on TIM3 - CH3 on STM32F103 device

Bogdan
Senior
Posted on January 10, 2016 at 14:32

Hello all, i have some mouses around my house and need a pwm driver for a mouse trap but i have some dificulties generating a pwm signal on PB0 ( Timer 3 channel 3 output)

RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0; // Timer3 channel 3 on PB0
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
/**** Timer3 time base setup ****/
TIM_TimeBaseInitStructure.TIM_Prescaler=30-1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseInitStructure.TIM_Period = 10000;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable ;
TIM_OCInitStructure.TIM_Pulse = 5000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //PB0 - ouptut pwm on channel 3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM3->CCR3=15; // just some duty cycle value 

with the code above i am unable to have any type of pwm signal on PB0, I am doing something wrong?
5 REPLIES 5
Posted on January 10, 2016 at 16:06

Seems reasonable enough, make sure you are not remapping TIM3 in some of your other code.

Which specific F103? Does that have TIM3, does the clock enable, can you see output on other pins, or get Update interrupt?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Bogdan
Senior
Posted on January 10, 2016 at 18:14

Hello Clive, the device is a stm32f103c8t6 , i had tryied the same code on another c8t6 device, and the same is there.

The above code is just in a simple main(void) {   }

The device is alive because i had some toggles with another GPIOA pin

Posted on January 10, 2016 at 19:53

Try using TIM_OCStructInit() to make sure all the fields of the structure are set up.

Shouldn't need TIM_CtrlPWMOutputs() for TIM3

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Walid FTITI_O
Senior II
Posted on January 11, 2016 at 19:44

Hi anton.bogdan,

You used the wrong APBx peripheral clock enabling function for TIM3 which is APB1 peripheral. TIM3 is not well clocked in your code:

RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB2Periph_GPIOB , ENABLE);

 

 

Should be replaced by:

/* TIM3 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

/* GPIOB clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

- Hannibal -
Bogdan
Senior
Posted on January 11, 2016 at 20:04

Hi, thanks for the remark, just noticed that this morning,

changed to    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

and it works, now i  can catch mouses with high voltage :D