Stm32 timer time calculation

How does the STM32 timer work? The ability of STM32 to perform timing comes from the internal counter that uses a clock signal, usually derived from an external crystal oscillator. The basic principle is based on the formula T = 1/F, where F is the frequency of the clock source. For example, let's take the 51 microcontroller as a reference. Suppose it has a 12MHz crystal oscillator. Since the 51 microcontroller divides the clock by 12 (meaning each machine cycle consists of 12 clock cycles), the minimum timing resolution would be: 12 MHz / 12 = 1 MHz T = 1 / 1 MHz = 1 µs So the smallest time unit for the 51 microcontroller is 1 microsecond. The 51 microcontroller timer has different modes with varying maximum timing intervals: - Mode 0: 13-bit timer, maximum interval of 8.192ms - Mode 1: 16-bit timer, maximum interval of 65.536ms - Mode 2: 8-bit timer, maximum interval of 256µs To achieve accurate timing, two main factors must be considered: 1. The prescaler (clock division ratio) 2. The value of the timer counter In STM32, the timer operates using the following formula: CK_CNT = Timer Clock / (TIMx_PSC + 1) Then, the timer’s clock cycle is calculated as T = 1 / CK_CNT For example, if the timer clock is 72MHz and the prescaler is set to 7199, then: CK_CNT = 72,000,000 / (7199 + 1) = 10,000 Hz T = 1 / 10,000 = 100 µs To achieve a 1-second delay, the auto-reload register (ARR) should be set to 10,000. This means the timer will count up to 10,000 before generating an update event or interrupt. Important notes when configuring the timer: - By default, TIMx (from 1 to 8) runs at 72MHz. - TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; This sets the number of times the timer overflows before triggering an interrupt. If not configured properly, the interrupt may occur after an unpredictable number of overflows, leading to inaccurate timing. Another key point is the system clock configuration. For instance, TIM1 uses PCLK2 (which can be 72MHz), while TIM2–TIM7 use PCLK1. The correct configuration of the prescaler and period values is crucial for accurate timing. Basic timer setup steps include: 1. Setting the prescaler: `TIM_TimeBaseStructure.TIM_Prescaler = 7199;` This divides the clock frequency to get the desired timing resolution. 2. Setting the period: `TIM_TimeBaseStructure.TIM_Period = 9999;` This defines how many counts are needed to trigger an interrupt. 3. Configuring the counter mode: `TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;` 4. Setting the clock division: `TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;` 5. Initializing the timer: `TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);` 6. Enabling the update interrupt: `TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);` 7. Starting the timer: `TIM_Cmd(TIM2, ENABLE);` Alternatively, if you want a 1-second interrupt with a prescaler of 35999 and a period of 2000: ((1 + 35999) / 72M) * (1 + 2000) = 1 second This shows how the combination of prescaler and period values determines the final timing interval. Proper configuration ensures precise control over timing functions in STM32-based applications.

Infrared Touch Frame

open frame touch screen,multi touch frame,touch screen overlay kit,multi touch overlay kit

Guangdong ZhiPing Touch Technology Co., Ltd. , https://www.zhipingtouch.com

This entry was posted in on