Classification of STM32 timers:
1. Advanced timer TIME1,TIME8
2. General timer TIME2~TIME5
3. Basic timer TIME6,TIME7
This chapter explains the general timer, refer to Chapters 13, 14, and 15 of the Development Guide and Chapter 14 of the Chinese Reference Manual.
1. Counter Counting Mode
1. Up counting mode
2. Down counting mode
In down mode, the counter starts from the automatically loaded value (the value of the TIMx_ARR counter) and counts down to 0, then restarts from the automatically loaded value and generates a counter underflow event.
3. Center alignment mode (up/down)
In this experiment, we use the down counting mode
2. STM32 general purpose timer
The STM32's general purpose TIMx (TIM2, TIM3, TIM4 and TIM5) timer functions include:
1) 16-bit up, down, up/down auto-load counter (TIMx_CNT).
2) 16-bit programmable (can be modified in real time) prescaler (TIMx_PSC), the division coefficient of the counter clock frequency is any value between 1 and 65535.
3) 4 independent channels (TIMx_CH1~4), which can be used as:
A. Input Capture
B. Output Comparison
C. PWM Generation (Edge or Center Aligned Mode)
D. Single pulse mode output
4) External signals (TIMx_ETR) can be used to control the synchronization circuit of the timer and the timer interconnection (one timer can be used to control another timer).
5) Interrupt/DMA is generated when the following events occur:
A. Update: counter overflow/underflow, counter initialization (by software or internal/external trigger)
B. Trigger event (counter starts, stops, initializes, or counts by internal/external trigger)
C. Input Capture
D. Output Comparison
E. Support incremental (quadrature) encoder and Hall sensor circuits for positioning
F. Trigger input as external clock or cycle-by-cycle current management
3. Use a timer to perform a timed step of a settable length
The overflow time of the counter is (the initial value of the counter + 1) * the clock of the counter
How to set the counting frequency of the counter:
1. Timer clock source
1) Internal clock (CK_INT)
2) External clock mode 1: external input pin (TIx)
3) External clock mode 2: external trigger input (ETR)
4) Internal trigger input (ITRx): Use timer A as the prescaler for timer B (A provides the clock for B).
This chapter explains the internal clock (CK_INT)
The clock comes from the APB1 clock and then passes through a multiplier. The multiplication factor can be 1 or 2, which is determined by the APB1 predivider.
If its prescaler factor is 1, the multiplier is 2.
If its pre-scaling factor is not 1, the multiplication factor is 1.
Up counting mode:
Example:
After initializing the system with the SystemInit() function, set the system clock to 72MHz
The overflow time calculation formula is as follows:
Tout= ((arr+1)*(psc+1))/Tclk
in:
psc: prescaler value
arr: Automatic reload value.
Tclk: The input clock frequency of TIM3 (in Mhz).
Tout: TIM3 overflow time (in us).
The value of (psc+1)/Tclk is the time of one cycle of the timer clock.
4. Some related registers
1. DMA/Interrupt Enable Register (TIMx_DIER)
The 0th bit is mainly used, which is the update interrupt enable bit. This chapter uses the timer update interrupt, so this bit should be set to 1 to allow interrupts caused by update events.
2. Prescaler register (TIMx_PSC)
This register is used to divide the clock frequency and then provide it to the counter as the counter clock.
3. Auto-reload register (TIMx_ARR)
4. Status Register (TIMx_SR)
This register is used to mark whether various events/interrupts related to the timer have occurred.
Getting and clearing the status flag:
FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT);
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT);
5. Control register 1 (TIMx_CR1)
Used to enable the timer, mainly used for bit 4 and bit 0.
6. Counter current value register CNT
5. Steps to use the timer
1) TIM3 clock enable.
TIM3 is mounted under APB1, so we enable TIM3 through the enable function under APB1 bus. The called function is:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Clock enable
2) Initialize timer parameters, set auto-reload value, division factor, counting method, etc.
In the library function, the timer initialization parameters are implemented through the initialization function TIM_TimeBaseInit:
voidTIM_TimeBaseInit(TIM_TypeDef*TIMx,
TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
Definition of the structure:
typedef struct
{
uint16_t TIM_Prescaler;
uint16_t TIM_CounterMode;
uint16_t TIM_Period;
uint16_t TIM_ClockDivision;
uint8_t TIM_RepetitionCounter;
} TIM_TimeBaseInitTypeDef;
The first parameter TIM_Prescaler is used to set the frequency division coefficient
The second parameter TIM_CounterMode is used to set the counting mode
The third parameter TIM_Period is to set the automatic reload count period value
The fourth parameter TIM_ClockDivision is used to set the clock division factor
For general timers, only the first four parameters are useful, and the last parameter TIM_RepetitionCounter is useful only for advanced timers.
example:
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 4999;
TIM_TimeBaseStructure.TIM_Prescaler =7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
3) Set TIM3_DIER to enable update interrupt.
void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);
4) TIM3 interrupt priority setting.
5) Allow TIM3 to work, that is, enable TIM3.
To start the timer after configuration, set the CEN bit in TIM3_CR1.
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
6) Write the interrupt service function.
6. Update: counter overflow/underflow, counter initialization (by software or internal/external trigger)
Previous article:15. Window watchdog experiment
Next article:17. Definition of all interrupt functions
Recommended ReadingLatest update time:2024-11-16 12:44
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EE Mobile Station Development Board Introduction: DA14580DEVKT-B Evaluation Board
- No doubt! Real-time measurement can also be done with low power consumption
- Read the good book "Electronic Engineer Self-study Handbook" + Advanced Edition
- 【Based on Gizwits AIoT development platform + Gokit】+ X-ray dose monitor
- Providing a 1.5V reference voltage to a thermocouple can expand the entire measurement temperature range? What is the principle?
- See the circuit and eat the circuit to verify the circuit
- How to multithread?
- Software and hardware design of Mitsubishi PLC based on STM32
- Analysis of the Principle of Micro-control Circuit for Plasma Color TV
- What is the highest sampling rate that the McASP of TMS320C6748 can support?