cancel
Showing results for 
Search instead for 
Did you mean: 

Pulse Width Modulation in stm32f10x

parisa
Senior
Posted on December 26, 2016 at 12:02

Hello

Here is my

Pulse Width Modulation

function that doesn't work properly.what is my mistake?

Input Freq:72Mhz

Chip: f107

output pin:PA.0

TIM_TimeBaseInitTypeDef TStructure;

TIM_OCInitTypeDef OCStructure;

GPIO_InitTypeDef GPStruc;    

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

TStructure.TIM_Prescaler=7199;

TStructure.TIM_CounterMode=TIM_CounterMode_Up;

TStructure.TIM_Period=9;

TStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TStructure.TIM_RepetitionCounter=0;

TIM_TimeBaseInit(TIM5, &TStructure);

TIM_Cmd(TIM5, ENABLE);    

OCStructure.TIM_OCMode=TIM_OCMode_PWM2;

OCStructure.TIM_OutputState=TIM_OutputState_Enable;

OCStructure.TIM_OCPolarity=TIM_OCPolarity_Low;

OCStructure.TIM_Pulse=3599;

TIM_OC1Init(TIM5, &OCStructure);

TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);    

GPStructure.GPIO_Mode=GPIO_Mode_AF_PP ;

GPStructure.GPIO_Speed=GPIO_Speed_2MHz;

GPStructure.GPIO_Pin=GPIO_Pin_0;

GP_Init(GPIOA,&GPStructure);
1 ACCEPTED SOLUTION

Accepted Solutions
Khouloud GARSI
Lead II
Posted on December 26, 2016 at 15:39

Hello

Mohamadi.Parisa

,

I advise you to rearrange your code.

- configure the GPIO

- configure the TIMER

- Then enable the TIMER

Note: This condition should be respected: TIM_Period > TIM_Pulse

I think you are confusing between the prescaler and the period values.

There is a lot of examples using

https://my.st.com/content/my_st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef1.html

or

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32html

. They will help you to generate the PWM pulses correctly.

If this solves your issue, please click on correct

🙂

.

Regards,

Khouloud

View solution in original post

1 REPLY 1
Khouloud GARSI
Lead II
Posted on December 26, 2016 at 15:39

Hello

Mohamadi.Parisa

,

I advise you to rearrange your code.

- configure the GPIO

- configure the TIMER

- Then enable the TIMER

Note: This condition should be respected: TIM_Period > TIM_Pulse

I think you are confusing between the prescaler and the period values.

There is a lot of examples using

https://my.st.com/content/my_st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef1.html

or

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32html

. They will help you to generate the PWM pulses correctly.

If this solves your issue, please click on correct

🙂

.

Regards,

Khouloud