cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 Tim1 PWM

kent gu
Associate III
Posted on January 22, 2018 at 07:48

hi,

I am using SMT32F103VE, try to use TM1_CH1 ouput PWM at GPIOA_8, it can output correctly in debug mode with J-link, but can't output in normal mode(Power up after flash).

The below is code. 

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure); 

//Motor enable signal

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE); 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

GPIO_Init(GPIOD, &GPIO_InitStructure); 

TIM_Cmd(TIM1, DISABLE);

TIM_TimeBaseStructure.TIM_Period = 1000; 

TIM_TimeBaseStructure.TIM_Prescaler = 72-1; 

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 

TIM_TimeBaseStructure.TIM_RepetitionCounter=0; 

TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); 

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 

TIM_OCInitStructure.TIM_Pulse = 0;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; 

TIM_OC1Init(TIM1, &TIM_OCInitStructure)

TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM1,ENABLE);

TIM_Cmd(TIM1, ENABLE);

TIM_CtrlPWMOutputs(TIM1, ENABLE);

Best Regards,

Kent

#tim-pwm
10 REPLIES 10
Posted on January 26, 2018 at 14:33

Structures allocated on the stack contain junk values, these values can get misinterpreted even if you don't use that functionality.

Use the form

TIM_OCInitTypeDef TIM_OCInitStructure = { 0 };