Skip to main content
Najam
Associate II
May 3, 2023
Solved

I am not able to understand the difference between following two: __HAL_TIM_GetCompare(&htimX, TIM_CHANNEL_X) __HAL_TIM_GetCounter(&htimX) I checked the datasheet for STM32F031K6T6 but not able to understand the difference.

  • May 3, 2023
  • 7 replies
  • 2583 views

..

This topic has been closed for replies.
Best answer by Kamil Duljas

__HAL_TIM_GetCounter(&htimX) allow to read CNT register where is the value that correspond with current "time". As time We mean nummber of clock cycles. When your CNT register equals ARR register, then CNT register reset and count again.

__HAL_TIM_GetCompare(&htimX, TIM_CHANNEL_X) allows read CCR register. The register used to for example in Output compare mode or input capture mode.

In Output compare mode (toggle mode):


_legacyfs_online_stmicro_images_0693W00000bjNPvQAM.pngYou can manipulate signal when registers have been equal. When CNT == CCR then the signal toggles.

Remeber this is just example. CCR register support many various cases and features

7 replies

gbm
Lead III
May 3, 2023

And why don't you check the HAL reference?

I do not use HAl, but I would guess that the first returns the value of CCRx and the second - CNT.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Najam
NajamAuthor
Associate II
May 3, 2023

I checked this UM1785 document which is "Description of STM32F0 HAL and low-layer drivers".

But not able to understand.

Kamil DuljasBest answer
Senior III
May 4, 2023

__HAL_TIM_GetCounter(&htimX) allow to read CNT register where is the value that correspond with current "time". As time We mean nummber of clock cycles. When your CNT register equals ARR register, then CNT register reset and count again.

__HAL_TIM_GetCompare(&htimX, TIM_CHANNEL_X) allows read CCR register. The register used to for example in Output compare mode or input capture mode.

In Output compare mode (toggle mode):


_legacyfs_online_stmicro_images_0693W00000bjNPvQAM.pngYou can manipulate signal when registers have been equal. When CNT == CCR then the signal toggles.

Remeber this is just example. CCR register support many various cases and features

Dudo
Najam
NajamAuthor
Associate II
May 4, 2023

Thanks Kamil

Senior III
May 4, 2023

Do you understand the differences ​?

Dudo
Najam
NajamAuthor
Associate II
May 4, 2023

I am trying to use one of the timers as a counter. So that it can read external clock pulses. What I understand from your explanation is that I have to use the following

__HAL_TIM_GetCompare(&htimX, TIM_CHANNEL_X

to check if the pulse at the input pin is high.

Senior III
May 4, 2023

If you want use timer as counter you don't need compare registers ​

Dudo
raptorhal2
Lead
May 4, 2023

Read the Reference Manual regarding timer capture compares, then read a Timer Capture example included with the HAL Library.

A capture event (e.g, input pulse high) saves the contents of the Timer Counter, and this event can be sensed with a timer interrupt, so a separate and less accurate __HAL_TIM_GetCounter is not required.

Najam
NajamAuthor
Associate II
May 5, 2023

Is this the document you are referring to PM0215 "STM32F0 series Cortex-M0 programming manual"