2016-01-10 05:32 AM
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?
2016-01-10 07:06 AM
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?2016-01-10 09:14 AM
2016-01-10 10:53 AM
Try using TIM_OCStructInit() to make sure all the fields of the structure are set up.
Shouldn't need TIM_CtrlPWMOutputs() for TIM32016-01-11 10:44 AM
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 -2016-01-11 11:04 AM