cancel
Showing results for 
Search instead for 
Did you mean: 

Timer not working as expected

SSchu.4
Associate II

Hi,

I want to measure time between two events. The events come in as ext- interrupts.

I set up a timer like this:

TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM6_Init 1 */
 
  /* USER CODE END TIM6_Init 1 */
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 49;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 1;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

Starting the timer

 HAL_TIM_Base_Start(&htim6);

Trying to read its value in the ext-it gives always 1 or 0.

I dont really understand what I am doing wrong.

// diff is allways 0 or 1 (even when different times went by between the calls)
	uint32_t diff = __HAL_TIM_GET_COUNTER(&htim6);
	__HAL_TIM_SET_COUNTER(&htim6,0);

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

Of course the timer is working as expected, HAL is not working as you expect it to work. It's not your fault. Lacking proper documentation that defines basic concepts, one can only make assumptions that are either true or not.

Read the first few sections (up to Time-base unit) of the timer chapter in the reference manual. Step through the HAL functions and note which registers they access. Replace the HAL function calls with the register writes that they do. Then you'll be in a position to know what it exactly does, instead of guessing.

> I want to measure time between two events. The events come in as ext- interrupts.

Note that HAL interrupt handling functions can have several μs latency. To get a more accurate reading, you can use the timer input capture feature if you connect the external signal to a timer channel (use another timer as TIM6 and TIM7 have no capture channels). Read the Capture/compare channels, Input capture mode and maybe the PWM input mode sections. The last one is optional, it describes an advanced concept based on timer capture and slave mode. Each section has some examples in pseudocode that you can directly translate to register writes.

View solution in original post

3 REPLIES 3
TDK
Guru

> htim6.Init.Period = 1;

Your counter will start at 0, increment to 1, then reset to 0 and repeat. If you want it to count up to 100 instead, use 100 here. Or 0xFFFF if you want the maximum period for a 16-bit timer.

If you feel a post has answered your question, please click "Accept as Solution".
SSchu.4
Associate II

Thank you! I did not understand the purpose of "period" correctly. Works now.

berendi
Principal

Of course the timer is working as expected, HAL is not working as you expect it to work. It's not your fault. Lacking proper documentation that defines basic concepts, one can only make assumptions that are either true or not.

Read the first few sections (up to Time-base unit) of the timer chapter in the reference manual. Step through the HAL functions and note which registers they access. Replace the HAL function calls with the register writes that they do. Then you'll be in a position to know what it exactly does, instead of guessing.

> I want to measure time between two events. The events come in as ext- interrupts.

Note that HAL interrupt handling functions can have several μs latency. To get a more accurate reading, you can use the timer input capture feature if you connect the external signal to a timer channel (use another timer as TIM6 and TIM7 have no capture channels). Read the Capture/compare channels, Input capture mode and maybe the PWM input mode sections. The last one is optional, it describes an advanced concept based on timer capture and slave mode. Each section has some examples in pseudocode that you can directly translate to register writes.