cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot Configure TIM3 , Timer count values are drifting ?

Oahme.1
Associate III

I'm using an STM32F401CCU6 microcontroller and am trying to generate a timer interrupt of 10 us with TIMER3.The timer is a update event timer. The formula I'm using is:

Update_event = TIx_CLK/((PSC + 1)*(ARR + 1))

Below, I've given my clock configuration. Secondly, I got my ARR = (840-1) and the Prescaler = (1-1) for a 10 µs timer. Is this incorrect?

Also, I'm using the Keil development software and when I run the code,my first query is the right psc and arr values . Secondly when ever interrupt is generated my timer values is different each time interrupt is generated , the counts are drifting ??.My Code :

void TIM3_Init(void)
{
        __HAL_RCC_TIM3_CLK_ENABLE(); 
	TIM3->SR &=~(TIM_CR1_CEN);
	TIM3->CNT = 0 ; 
	TIM3->ARR  = (839); 
	TIM3->PSC  = (1-1) ;
	TIM3->DIER |=TIM_DIER_UIE; 
        HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(TIM3_IRQn);	
	TIM3->CR1 |= TIM_CR1_CEN ;                  // First BreakPoint 
}

TIM3Interrupt routine :

void TIM3_IRQHandler(void)
{
  HAL_TIM_IRQHandler(&htim3);  // Second Breakpoint
 
}

How I debug is by first placing breakpoint on my TIM3_Init function last line , Hit that breakpoint , Reset my Timer on the Keil IDE as t1 , and run until TIM3 Interrupt is generated and note the value , as I said , the values are drifting , first they are 14us , then .5us , I dont understand how difficult could it be to Initialize a timer , I've done it with stm32f103 series and never had a headache . Please tell me what Im doing wrong , could my RCC Clock be the problem which I doubt , or IDE settings. I've been fighting with this simple initialization for 3 days .

0693W00000Npy4qQAB.png

12 REPLIES 12

Output SYSCLK to MCO and measure.

Read out and check/post RCC and TIM registers content.

JW

Oahme.1
Associate III

So , Ill have to check it on my scope till tomorrow , but , i used __NOP() function to step through and yes, my clock is exactly running at 10Mhz ,0.1us , doing the same with HSI , disabling HSE and configuring for 84Mhz is also giving me 10Mhz ? what is going on , this is totally uncalled for , I checked my voltage which Im giving through stlink only , no usb and its 3.2 volts

Oahme.1
Associate III

I Fixed my problem , it was stupid and careless, I forgot to set my frequency in trace to 84Mhz in KEIL. Probably because I created a seperate project just recently .Now my timer count is according to the calculated values .

Thanks ,