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

TIM3->SR &=~(TIM_CR1_CEN); // NOT SR

Probably saturating if your code/call-back is doing too much. Try 10 KHz rather than 100 KHz

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

> first they are 14us , then .5us

Do you freeze the timer during debugging, in DBGMCU_APB1_FZ? If not, the timer continues to run while your debugger stopped at the breakpoint.

Also, you should stop only if TIMx_SR.UIF is set, although the Cube code you are calling is probably inefficient enough to hide this problem.

JW

Thankyou so much , the DBGMCU_APB1_FZ debugmode register did the job for me . Although Ive noticed that having exact theoretical timer register values are not neccsessry , could this be a problem in the future ?

> having exact theoretical timer register values are not neccsessry

I don't understand. Not necessary for what? And what do you mean by "exact theoretical timer register values"?

JW

Oahme.1
Associate III

@Community member​  for example my clock speed is 84Mhz , according to the formula if i want a timer count of 1 second i would need values as PSC = 10000 and ARR =8400 , yet when i insert these values i get a timer of 8.4 seconds ? , but then I inserted values ARR = 10000-1 and PSC = 1000 i get 1 second exact .

> when i insert these values i get a timer of 8.4 seconds

That means that TIM1 clock is not 84MHz as you thought. You can verify it by outputting system clock onto the MCO pin and measure using oscilloscope/logic analyzer.

Check RCC registers content, and frequency of the primary clock source. Try with HSI first, its frequency is known.

JW

Oahme.1
Associate III

Yeah I had my doubts , but I scratched it out beacuse how can CubeMx get the clock wrong ? Never really checked clock before using MCO pin so yeah , thats a another problem on another thread . So My clock is basically running on 10Mhz ??

Read out and check RCC registers settings. Check the primary clock (i.e. do you have a 25MHz crystal connected?)

JW

Oahme.1
Associate III

yes, the 25Mhz crystal is connected and I am using HSE as my clock source ,both in pinnout and clock configuration block, otherwise if connection is issue i shouldnt be getting any clock ? . My clock settings are as show below . Also on the top i've shown my actual CubeMx clock configuration ,which matches exactly with the code generated i believe .

RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }