Skip to main content
kent gu
Associate III
January 22, 2018
Question

STM32F103 Tim1 PWM

  • January 22, 2018
  • 3 replies
  • 2789 views
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
This topic has been closed for replies.

3 replies

kent gu
kent guAuthor
Associate III
January 22, 2018
Posted on January 22, 2018 at 09:16

Also I use STM32 ST-link Utility to reflash and SW reset, GPIOA_8 can output PWM signal. But after I re-power up, GPIOA_8 can't output PWM signal, other function work well.

kent gu
kent guAuthor
Associate III
January 24, 2018
Posted on January 24, 2018 at 02:51

I did another trial, short reset# pin to GND.

  • If just rest#pin touch GND in quick speed, GPIOA_8 can't output PWM.
  • but i

     just rest#pin touch GND for about 1s(maybe), GPIOA_8 can output PWM

Best Regards,

Kent

Tesla DeLorean
Guru
January 24, 2018
Posted on January 24, 2018 at 04:19

Perhaps it has nothing to do with TIM1/PWM. Make sure BOOT0 is pulled low, and perhaps output info via a USART indicating the code is running.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
kent gu
kent guAuthor
Associate III
January 24, 2018
Posted on January 24, 2018 at 10:24

Hi Clive,

Thanks a lot for your reply. Boot0 is pull low. And even disable UART, still can't work.  

BTW, for PA8, there are UART1_CK/TM1_CH/MCO, how to select one function of them?

Best Regards,

Kent

Tesla DeLorean
Guru
January 24, 2018
Posted on January 24, 2018 at 18:10

The F1 has peripheral level remapping, if you have both peripherals enabled you'll need to remap one to a different group/block of pins. Absent a few errata if the other peripherals aren't enabled the one functioning takes ownership of the pins in AF mode.

The Data Sheet and Reference Manual should provide the details for pin groups, and remap options.

It is a 10 year old design, ST has improved the pin level mapping in newer families, and many of us have moved on. Consider if a newer model STM32 would be more suitable for a product designed today.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..