cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F205 TIM5 PWM frequency calculation?

mail2
Associate II
Posted on October 10, 2013 at 08:20

Hi,

I will output a PWM Signal on a STM21F205 Pin A3 and use the following code:

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct; 
/* Clock für GPIOA einschalten */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// PA3 kommt auf timer5 für PWM
// Clock für Timer 5 ein
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_StructInit(&GPIO_InitStructure); // Reset init structure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure );
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM5);
TIM_TimeBaseStructInit( &TIM_TimeBaseInitStruct );
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4;
TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1; // 0..999
TIM_TimeBaseInitStruct.TIM_Prescaler = 240 - 1; // Div 240
TIM_TimeBaseInit( TIM5, &TIM_TimeBaseInitStruct );
TIM_OCStructInit( &TIM_OCInitStruct );
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_Pulse = 500;
TIM_OC4Init( TIM5, &TIM_OCInitStruct );
TIM_Cmd( TIM5, ENABLE );

I can measure a PWM with 250 Hz. But why? Thanks Martin
4 REPLIES 4
Posted on October 10, 2013 at 14:02

I can measure a PWM with 250 Hz. But why?

Probably because your APB1 clock is 30 MHz and TIM5CLK is 60 MHz (check system_stm32f2xx.c)

60,000,000 / (240 * 1,000) = 250

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mail2
Associate II
Posted on October 10, 2013 at 16:14

Hello Clive,

That makes sense to me ;)

But what about 

TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4;?

Martin

Posted on October 10, 2013 at 17:57

Pretty sure that has zero impact on the time base, if I recall it relates to the clock used for dead-time generation and filtering elements.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mail2
Associate II
Posted on October 10, 2013 at 18:28

I found it now in application note 4013. Thanks for your help.

Martin