cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Najam
Associate II
1 ACCEPTED SOLUTION

Accepted Solutions
Kamil Duljas
Senior III

__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

View solution in original post

9 REPLIES 9
gbm
Lead III

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.

Najam
Associate II

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

But not able to understand.

Kamil Duljas
Senior III

__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
Associate II

Thanks Kamil

Do you understand the differences ​?

Dudo
Najam
Associate II

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.

raptorhal2
Lead

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.

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

Dudo
Najam
Associate II

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