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:50

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:47

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/TIM1%20and%20TIM2%20of%20STM32F103%20both%20are%20working%20same%20APB%20frequency%203981&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=2]Dupe

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