Try it without HAL, it's not complicated.

void start_TIM2() {
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  TIM2->CR1 |= TIM_CR1_EN;
}

uint16_t read_TIM2() {
  return TIM2->CNT;
}
Answer from followed Monica to Codidact on Stack Overflow
🌐
DeepBlue
deepbluembedded.com › home › blog › stm32 counter mode example – frequency counter | timer in counter mode
STM32 Counter Mode Example - Frequency Counter With Timer Module
January 20, 2024 - Where the timer gets clocked from an external source (input pin) and it counts the number of pulses. However, STM32 timer modules do have multiple modes for the counting mode itself.
🌐
DigiKey
digikey.com › en › maker › projects › getting-started-with-stm32-timers-and-timer-interrupts › d08e6493cefa486fb1e79c43c0b08cc6
Getting Started with STM32 - Timers and Timer Interrupts
I’ll write “80 - 1” to show ... a PSC value of “0” means to use a prescaler (clock divider) of 1. Set the Counter Period to 65535....
Discussions

c - Stm32 Timer Counter With Registers - Stack Overflow
I try to use timer1 on STM32f103c8t6 (bluepill). I was read the datasheet and internet response/questions. However I can't understand "how can I adjust period value with registers". I was using tim... More on stackoverflow.com
🌐 stackoverflow.com
Correct way of setting a 1μs timer on a STM32 MCU with HAL
The problem with your approach is that it will fail once other stuff is going on in the background. I'm assuming 84MHz is your CPU clock. So if you're not checking the flag for 84 CPU cycles, you miss one beat. 84 CPU cycles is not much. An ISR servicing USB could easily take more than that. So unless you are disabling interrupts before calling microDelay() your delay will not be precise. The approach of making an actual 1MHz counter and using its count is the correct approach and you should use it. As for the issue of calling delay_us with a value higher than the counter period, usually you would set up the counter to count from 0..1 million to match the MHz frequency, so you cannot pass a value that's too high because the argument is only a uint16_t. I must add that I'm not familiar with __HAL_TIM_SET_COUNTER/__HAL_TIM_GET_COUNTER. I'm just assuming that the value range here would be 0..1 million. More on reddit.com
🌐 r/embedded
9
7
December 12, 2023
microcontroller - Prescaler vs Counter Period (STM32) - Electrical Engineering Stack Exchange
For timers on the stm32, there is the option of setting prescaler and counter period. I understood that the prescaler is dividing the frequency before using it for the counter. However, it seems to... More on electronics.stackexchange.com
🌐 electronics.stackexchange.com
August 28, 2022
STM32 timers: the most efficient settings for prescalers vs counter periods?
Pretty much all CMOS logic uses power when changing states and almost none the rest of the time- so in general, the more gates performing clock transitions per second, the more power your chip will draw. Almost all power reduction techniques in embedded come down to reducing clock speeds, gating off areas in the chip to experience no transitions, or reducing core voltages to reduce the power dissipation per transition. Your intuition about the prescaler is correct because it results in more ‘downstream’ gates transitioning at a lower frequency, therefore less power. More on reddit.com
🌐 r/embedded
8
22
November 28, 2021
People also ask

Is interrupt handling required for cascaded counters?
No. The counting happens entirely in hardware. Interrupts are only needed if you want to process or read overflow events in real time.
🌐
controllerstech.com
controllerstech.com › home › stm32 tutorials › stm32 timers series › stm32 timers (part 8): how to create a 48-bit counter by cascading timers
STM32 Timers (Part 8): How to Create a 48-Bit Counter by Cascading ...
Can advanced-control timers and general-purpose timers be cascaded together?
Yes, as long as the internal trigger (ITR) connections support it on your specific STM32 device. Always verify the trigger mapping in the reference manual.
🌐
controllerstech.com
controllerstech.com › home › stm32 tutorials › stm32 timers series › stm32 timers (part 8): how to create a 48-bit counter by cascading timers
STM32 Timers (Part 8): How to Create a 48-Bit Counter by Cascading ...
Can this method be used for event counting instead of time measurement?
Yes. By using an external clock input on the first timer, you can count external pulses and extend the event counting range using cascaded timers.
🌐
controllerstech.com
controllerstech.com › home › stm32 tutorials › stm32 timers series › stm32 timers (part 8): how to create a 48-bit counter by cascading timers
STM32 Timers (Part 8): How to Create a 48-Bit Counter by Cascading ...
🌐
Depfields
en.depfields.com › home › tutorial of arm mcu › genaral embedded skills › details of each peripheral › timer/counter[stm32 timers details]
Timer/Counter[STM32 Timers Details] | Most Powerful Bible to Become an Embedded Engineer
April 8, 2022 - Purpose:To output 5 kHz pulses in Output compare mode of a general-purpose timer ■ Timer uses only CH4 (PA3) of TIM5 output channels CH1-4 ■ Clock supplied to timer is 36MHz ■ Counter operation is up-counter mode and output comparison mode ...
🌐
Microcontrollers Lab
microcontrollerslab.com › home › stm32 blue pill › stm32 blue pill timer in counter mode with stm32cube ide and hal libraries
STM32 Blue Pill Timer in Counter Mode with STM32Cube IDE
September 30, 2022 - When configuring the STM32 Blue Pill timer module in counter mode, an external source such as the timer input pin clocks the timer module. The timer can count up/down on every rising/falling edge of the timer input pin.
Find elsewhere
🌐
DeepBlue
deepbluembedded.com › home › blog › stm32 timers tutorial | hardware timers explained
STM32 Timers Explained Tutorial - Timer Modes Examples Interrupts pwm
January 20, 2024 - The counter starts counting on the internal clock as long as TI1 is low and stops as soon as TI1 becomes high. The TIF flag in the TIMx_SR register is set both when the counter starts or stops.
🌐
Dipartimento di Matematica e Informatica
dmi.unict.it › santoro › teaching › lsm › slides › TimerSTM32.pdf pdf
The Timers of the STM32 Microcontrollers Corrado Santoro
The Timers of the STM32 Microcontrollers · What is a “timer”? A TIMER is a circuit that enables the software to have the · “knowledge of time” · It is basically a global variable (timer counter) that · increments (or decrements) on the basis of a ·
🌐
ControllersTech®
controllerstech.com › home › stm32 tutorials › stm32 timers series › stm32 timers (part 8): how to create a 48-bit counter by cascading timers
STM32 Timers (Part 8): How to Create a 48-Bit Counter by Cascading Timers
March 23, 2026 - You need to cascade 3 16bit timers with one timer clocking the second, and second clocking the third, TIM1->TIM2->TIM3. With this configuration, the formula to calculate the frequency is shown below · This video explains how to create a 48-bit counter by cascading multiple STM32 timers.
🌐
EmbeTronicX
embetronicx.com › tutorials › microcontrollers › stm32 › simple-stm32-timer-tutorial-bare-metal-with-registers
STM32 Timer Tutorial Part 1 - Bare Metal Part 3 ⋆ EmbeTronicX
Synchronization circuit to control the timer with external signals and to interconnect several timers. ... Update: counter overflow/underflow, counter initialization (by software or internal/external trigger) Trigger event (counter start, stop, initialization or count by internal/external trigger) ... Supports incremental (quadrature) encoder and hall-sensor circuitry for positioning purposes. Trigger input for external clock or cycle-by-cycle current management. In this post, we have used three microcontrollers (STM32F1, STM32F4, STM32F7) for demonstration.
Published   November 13, 2024
🌐
Microcontrollers Lab
microcontrollerslab.com › home › stm32 nucleo › stm32 nucleo timer in counter mode with stm32cubeide and hal libraries
STM32 Nucleo Timer in Counter Mode with STM32CubeIDE
September 16, 2024 - When configuring the STM32 Nucleo timer module in counter mode, an external source such as the timer input pin clocks the timer module. The timer can count up/down on every rising/falling edge of the timer input pin.
🌐
EMCU
emcu.eu › timer-counter-for-example-for-reading-an-input-button
TIMER Counter – for example for reading an Input button | EMCU
STM32 and STM8 Embedded Software Solutions · If you like it, share it · – · Counting Modes · Normally a basic timer counts from zero to a given Period value. General purpose and advanced timers can count in other different ways, as reported in Table below. The Figure below shows the three main counting modes. In this example we show you how to use the Timer as a Counter to detect an input after certain number of events.
🌐
Steppeschool
steppeschool.com › blog › stm32-timer-stm32cubemx
STM32 Timer explanation and configuring on STM32CubeMx
2. A prescaler is required to adjust the operating frequency of the timer: Operating frequency = Clock frequency/ prescaler+1 · 3. The counter of the timer is a value that tracks the ticks of the Timer.
🌐
STMicroelectronics
st.com › resource › en › application_note › an4776-generalpurpose-timer-cookbook-for-stm32-microcontrollers-stmicroelectronics.pdf pdf
How to use general-purpose timer peripheral on STM32 ...
Basic operating modes of STM32 general-purpose timers ... Figure 1. TIM1 timer-peripheral block diagram ... TI1FP1 and TI2FP2 input signals. The master/slave controller unit handles the inter-timers synchronization. This unit can be · configured to output a synchronization signal (TRGO signal) next to a certain timer internal · event. It can be configured as well to control the time-base counter in function of external
🌐
Reddit
reddit.com › r/embedded › correct way of setting a 1μs timer on a stm32 mcu with hal
r/embedded on Reddit: Correct way of setting a 1μs timer on a STM32 MCU with HAL
December 12, 2023 -

I am barely new to the embedded world and for a new project I require a 1μs timer. I am using the discovery board STM32F407VG.

These are the settings on how I have configured the timer:

  • Basic timer 6 at a clock frequency of 84 MHz

  • Following this formula I set the Prescaler to 0 and the Counter period to 83 to obtain a period of 1μs:

    • Counter Period = ( Time base required (in seconds) × ( Timer clock source / (Prescaler + 1) ) ) - 1

    • Counter Period = ( 0.000001 × ( 84000000 / (0 + 1) ) ) - 1 = 83

Then, I use this function with the flag TIM_FLAG_UPDATE:

static void microDelay (uint32_t delay){

    while (delay){
      if(__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE)){
    	  __HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE);
    	  delay--;
      }
    }
}

I haven't had any issues so far. I tested it with a logic analyzer by toggling a pin, and I didn't encounter any problems. My concern is that in a few tutorials, I saw that they simply matched the prescaler value to the timer clock frequency to obtain a Timer clock source of 1MHz and then set the Counter period to its maximum value. Then, they used the following function:

void delay_us (uint16_t us)
{
	__HAL_TIM_SET_COUNTER(&htim1,0);  
	while (__HAL_TIM_GET_COUNTER(&htim1) < us);  
}

Maybe I am being too fussy, but with this solution, if you enter a value bigger than the counter period the while loop will loop forever... and I don't see that problem in my solution.

However, do you think my solution could lead a further problems? Maybe, I am missing something that could make the timer fail in the future.

Thanks

Top answer
1 of 3
13
The problem with your approach is that it will fail once other stuff is going on in the background. I'm assuming 84MHz is your CPU clock. So if you're not checking the flag for 84 CPU cycles, you miss one beat. 84 CPU cycles is not much. An ISR servicing USB could easily take more than that. So unless you are disabling interrupts before calling microDelay() your delay will not be precise. The approach of making an actual 1MHz counter and using its count is the correct approach and you should use it. As for the issue of calling delay_us with a value higher than the counter period, usually you would set up the counter to count from 0..1 million to match the MHz frequency, so you cannot pass a value that's too high because the argument is only a uint16_t. I must add that I'm not familiar with __HAL_TIM_SET_COUNTER/__HAL_TIM_GET_COUNTER. I'm just assuming that the value range here would be 0..1 million.
2 of 3
6
A 1uS delay is pretty small. What is the purpose? I would set timer 6 with base clock of 1MHz, now you have a 1uS base. Grab value, wait for it to increment to the desired value. Don't bother setting the counter, just let it run. static void MX_TIM6_Init(void) { TIM_MasterConfigTypeDef sMasterConfig = {0}; htim6.Instance = TIM6; htim6.Init.Prescaler = 84; //Setup a 1uS Tick assuming 84MHz clock htim6.Init.CounterMode = TIM_COUNTERMODE_UP; htim6.Init.Period = 65535; htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 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(); } HAL_TIM_Base_Start(&htim); } void Micro_Delay(uint16_t Delay) { uint16_t tickstart = __HAL_TIM_GET_COUNTER(&htim6); uint16_t wait = Delay; while((__HAL_TIM_GET_COUNTER(&htim6) - tickstart) < wait){ } }
🌐
STMicroelectronics
st.com › resource › en › application_note › an4013-introduction-to-timers-for-stm32-mcus-stmicroelectronics.pdf pdf
AN4013 Application note - Introduction to timers for STM32 ...
February 1, 2026 - The timer's counter is incremented or decremented for each transition on both inputs TI1 ... Figure 7. Position at X4 resolution ... TI1 or TI2. Figure 8. Position at X2 resolution ... In case of X2 resolution, the counter can also be incremented on the TI1 edge. In STM32 timer encoder interface mode, the encoder mode3 corresponds to the X4
🌐
YouTube
youtube.com › watch
12 STM32 Timer Interrupt Counter with STM32Cube IDE and Nucleo Board - YouTube
About Video: Here I explained how to develop timer interrupt driver for 1 ms counter using stm32cube ide. #voidlooprobotech #stm32 #timerDownload Code: https...
Published   September 7, 2024
Views   181