Stm32 timer time calculation

How does the STM32 timer work? The STM32 microcontroller can perform timing functions thanks to internal counting pulses, which are generated by an oscillator, such as a crystal. The basic formula for calculating time is T = 1/F, where F is the frequency of the clock signal. For example, let's take the 51-series microcontroller as an example. If it uses a 12MHz crystal oscillator, and each machine cycle consists of 12 clock cycles, then the minimum timing unit would be: 12 MHz / 12 = 1 MHz T = 1 / 1 MHz = 1 μs So the smallest measurable time interval is 1 microsecond. For the 51-series timer, different modes offer different maximum timing intervals: - Mode 0: 13-bit timer, max time = 8.192 ms - Mode 1: 16-bit timer, max time = 65.536 ms - Mode 2: 8-bit timer, max time = 256 μs To achieve precise timing with a timer, two key factors must be considered: 1. **Prescaler (clock division ratio)** 2. **Timer counter value** In STM32, the timer operates based on the CK_CNT frequency, which is derived from the timer clock divided by the prescaler value (TIMx_PSC + 1). So the timer clock frequency is calculated as: CK_CNT = Timer Clock / (TIMx_PSC + 1) The time per clock cycle is then T = 1 / CK_CNT. For example, if the timer clock is 72 MHz and the prescaler is set to 7199, the effective frequency becomes: 72,000,000 / (7199 + 1) = 10,000 Hz T = 1 / 10,000 = 100 μs To get a 1-second delay, the auto-reload register (ARR) should be set to 10,000: 100 μs × 10,000 = 1 second Important notes when configuring the STM32 timer: - By default, TIMx (from 1 to 8) uses a 72 MHz clock. - The repetition counter (TIM_TimeBaseStructure.TIM_RepetitionCounter) is used to determine how many times the timer overflows before generating an interrupt. It’s crucial to configure this value properly; otherwise, the interrupt may not occur at the expected time. STM32 timer clock sources vary depending on the timer: - TIM1 is connected to PCLK2 (72 MHz) - TIM2–TIM7 are connected to PCLK1 The main steps in configuring a timer for a 1-second interrupt include setting the prescaler and the auto-reload value. Here’s an example code snippet using the standard peripheral library: ```c TIM_TimeBaseStructure.TIM_Prescaler = 7199; // Prescaler value TIM_TimeBaseStructure.TIM_Period = 9999; // Auto-reload value TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_Cmd(TIM2, ENABLE); ``` Another configuration could be: ```c TIM_TimeBaseStructure.TIM_Prescaler = 35999; // Divides 72 MHz by 36,000 → 2000 Hz TIM_TimeBaseStructure.TIM_Period = 2000; // 2000 counts ``` This results in: (35999 + 1) / 72,000,000 * (2000 + 1) = 1 second Understanding these configurations helps in accurately controlling the timing behavior of the STM32 timer for various applications like delays, PWM generation, or event triggering.

Capacitive Touch Film

PCAP film,capacitive touch foil,multi touch film

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

This entry was posted in on