Author Topic: Weird timing issue in STM32  (Read 3750 times)

0 Members and 1 Guest are viewing this topic.

Offline petoknmTopic starter

  • Newbie
  • Posts: 7
Weird timing issue in STM32
« on: February 03, 2014, 06:42:53 pm »
Hello
I have STM32F4Discovery board and I am having weird problems with timer clock. I am using separate timer TIM5 to generate interrupt every 1us (CPU freq=168MHz so I think it is possible(but it also happens with 10us interrupts...)) My main source of clock is XTAL 8 MHz that is on the board. that is then multiplied by the internal PLL to generate 168MHz CPU clock and that is than divided by 1(i checked it in all source files and header files) to generate AHB clock so AHB should be also 168MHz that is then divided further by 4 to get APB1 clock... hopefully 42MHz...TIM5 is connected to APB1 bus... but when i set the timer up it counts up twice as fast as i expect it to count... what may be the problem... I am probably overlooking something... but I don't know what... Is the clock source of the timer APB1 clock directly? I included partial page of the datasheet about the clocks... Please give me some feedback so I can know what I'm doing wrong... :D with this code below I can get should get 20us interrupts but I am getting 10us interrupts... WHY??? Thank you

SOLVED:
!!! I see the problem now... in the datasheet it says that if APB1 prescaler is not 1 then the clock to the timer is multiplied by 2...!!!

Timer init function:

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
   NVIC_InitTypeDef NVIC_InitStructure;
   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   /* Enable the TIM5 gloabal Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);

   /* TIM5 clock enable */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
   /* Time base configuration */
   TIM_TimeBaseStructure.TIM_Period = 20-1; // 1 MHz down to 50kHz (20 us)
   TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1; // 42 MHz Clock down to 1 MHz (adjust per your clock)
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
   /* TIM IT enable */
   TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
   /* TIM5 enable counter */
   TIM_Cmd(TIM5, ENABLE);


interrupt handler:

void TIM5_IRQHandler(void) {
   if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) {
      TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
      ticksus += 10;
      if (ticksus % 1000 == 0) {
         ticksms += 1;
         if(ticksms%100==0){
            GPIO_ToggleBits(GPIOD, GPIO_Pin_7);
         }
      }

   }
}
« Last Edit: February 03, 2014, 06:48:56 pm by petoknm »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Weird timing issue in STM32
« Reply #1 on: February 03, 2014, 07:03:02 pm »
It would have been a miracle if your code did what you expected.
================================
https://dannyelectronics.wordpress.com/
 

Offline leppie

  • Frequent Contributor
  • **
  • Posts: 269
  • Country: za
Re: Weird timing issue in STM32
« Reply #2 on: February 05, 2014, 10:36:10 am »
You should be using PWM output from what I can see...
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf