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
kent gu
Associate III
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
Associate III
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

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
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

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 25, 2018 at 11:20

Hi Clive,

You know, I have Tim8 advance timer can output PWM correctly, but Tim1 advance timer can't output PWM correctly? If it is possible use wrong startup file or flash.ld file?

Why do say that, because we didn't procure chip in big spot market, just procure chip in web like ebay. So I doubt if they changer mark on the chip, for example 128k flash change to 256 or 512K for more profit

  

Best Regards,

Kent

Posted on January 25, 2018 at 14:25

I believe TIM1 and TIM8 should be workable. I don't have the time/resources to replicate your scenario and confirm the behaviour on F1 chips.

Make sure the structures are fully initialized, use '={0};' so behaviour is strongly defined.

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 26, 2018 at 06:14

Sorry, what ' use '={0};'  ', what is it?

Posted on January 26, 2018 at 08:21

I solved the problem, just add 'TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset', but I just tim1_ch1,  not use tim1_ch1N. Don't know why?