I have recently used the timing function of the timer. I learned that the timer function of stm32 is very powerful and has a lot of things. The datasheet also talks about it in a long paragraph. I will not go into details about it. Here I will only explain how to configure the simplest timing function configuration.
Environment description: MCU: stm32F4 series, using the general timer TIM3
The first thing to know is that TIM3 is mounted on the APB1 bus. For details, see STM32F417xx_datasheet-P.19. There is a block diagram called Device overview. Forget it, let's take a screenshot.
What is the clock of APB1? It depends on how you configure it. Generally, it is in the SetSysClock() function. Here, I divide the system clock by 4. The system clock is 168MHz, so the clock on APB1 is 42MHz. Don't think that the clock of TIM3 is 42MHz, because APB1 to TIM3 has to go through a multiplier. This multiplier works only when the division coefficient of the previous system clock to APB1 is not 1. As we said before, APB1 is obtained by dividing the system clock by 4, so the multiplier is effective here, so the clock of TIM3 is the multiplication of APB1, that is, 84MHz.
OK, now that we have the input clock for the timer, we only need two parameters to configure the simple function of the timer: the prescaler and the auto-reload value.
Tout = ((arr+1)*(psc+1))/Tclk;
Where arr is the auto-reload value, psc is the pre-division value, and Tclk is the timer clock.
For example, if I want a 1s timer, I can set: arr = 9999; psc = 8399;
Here we should pay attention to the value range of arr and psc, the former is u32 and the latter is u16.
NVIC_InitTypeDef NVIC_InitStructure;
// TIM3 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// Enable the TIM3 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//Upward counting mode is to count from 0 to arr. When counting to arr, a TIM_IT_Update interrupt is generated. There are other interrupts to choose from.
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* TIM Interrupts enable */
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
In this way, the timer starts working, and then write the interrupt processing function.
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//your code
}
}
At this point, the timing function of TIM3 is completed. According to the psc and arr values, we can set the desired timing time.
Of course, this is just a very simple function of timing. There are many cool things. If there are any mistakes, please correct me!
Previous article:Summary of the general timer interrupt experiment of STM32F107
Next article:STM32 learning general timer interrupt
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- How to add c files in esp32 idf components to compile
- Will the push-pull circuit be straight through?
- Award-winning live broadcast: Third-generation TI C2000 new features resource update
- EEWORLD University - Industrial Internet of Things using CC1310 sub-1 GHz wireless MCU
- A simple way to distinguish between broadband FM and narrowband FM
- Wireless Connectivity Technology Selection Guide
- Learning FPGA Embedded System Design-1
- Qorvo 5 GHz iFEM Helps Accelerate Wi-Fi 6 Home Mesh Networking
- Automotive/Industrial mmWave Radar Sensors
- Is it true that soft-switching EMI is smaller than hard-switching EMI?