cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 and TIM2 of STM32F103 both are working same APB frequency?

Niks
Associate II
Posted on August 04, 2016 at 12:52

Hi,

I have configured the TIM1 and TIM2  in following way.

void timer1Init(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

timerStruct.TIM_Prescaler  = 71;

timerStruct.TIM_CounterMode  = TIM_CounterMode_Up;

timerStruct.TIM_Period  = 60000;

timerStruct.TIM_ClockDivision  = TIM_CKD_DIV1;

  TIM_TimeBaseInit(TIM1, &timerStruct);

  TIM_ClearFlag(TIM1, TIM_FLAG_Update);

  TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);

  TIM_Cmd(TIM1, ENABLE);

}

void timer2Init(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

timerStruct.TIM_Prescaler  = 71;

timerStruct.TIM_CounterMode  = TIM_CounterMode_Up;

timerStruct.TIM_Period  = 60000;

timerStruct.TIM_ClockDivision  = TIM_CKD_DIV1;

  TIM_TimeBaseInit(TIM2, &timerStruct);

  TIM_ClearFlag(TIM2, TIM_FLAG_Update);

  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

  TIM_Cmd(TIM2, ENABLE);

}

void TIM2_IRQHandler()

{

   if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

   {

     TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

 status = !status;

 GPIO_WriteBit(GPIOF,GPIO_Pin_7,status);

 }

}

void TIM1_UP_IRQHandler()

{

   if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)

   {

     TIM_ClearITPendingBit(TIM1, TIM_IT_Update);

 status = !status;

 GPIO_WriteBit(GPIOF,GPIO_Pin_7,status);

 }

}

in main loop i am calling only one timerInit at a time. As it is mentioned in the datasheet that TIM1 is working on 72MHz and TIM2 is working on 36MHz. 

I have set the same value for prescalar and period member of structure in both timer1Init() and timer2Init() functions, it is showing the same pulse period for both the timers. for TIM1 period should be 60ms and for TIM2 it should be 30ms

why its showing the same period for both.

Hoping for quick response...

thanks 

1 REPLY 1
Posted on August 04, 2016 at 14:45

Please review the Clock Tree diagram in the Reference Manual.

TIMCLK is typically 2x the APB clock except in the DIV1 case.

So if APB1 is DIV2, and ABP2 is DIV1, both will supply a TIMCLK that is the same to the timers on those buses.

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