Today we use the basic timer TIM4 to do a delay task, which is implemented in two ways: query and interrupt.
1 The query method is to query whether the UIF bit (update interrupt flag) in the status register is set. The main code is:
void delay_ms(u16 n_ms)
{
/* Init TIMER 4 */
CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE);
/* Init TIMER 4 prescaler: / (2^6) = /64 */
TIM4->PSCR = 6;
/* HSI div by 1 --> Auto-Reload value: 16M / 64 = 1/4M, 1/4M / 1k = 250*/
TIM4->ARR = 250;
/* Counter value: 2, to compensate the initialization of TIMER*/
TIM4->CNTR = 2;
/* clear update flag */
TIM4->SR1 &= ~TIM4_SR1_UIF;
/* Enable Counter */
TIM4->CR1 |= TIM4_CR1_CEN;
while(n_ms--)
{
while((TIM4->SR1 & TIM4_SR1_UIF) == 0) ;
TIM4->SR1 &= ~TIM4_SR1_UIF;
}
/* Disable Counter */
TIM4->CR1 &= ~TIM4_CR1_CEN;
}
Previous article:STM8L Exploration Kit Study Notes - RTC Real-time Clock (VI)
Next article:STM8L Discovery Kit Study Notes-CLK Clock Control (IV)
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Melexis uses coreless technology to reduce the size of current sensing devices
- Commonly used algorithms for drones - Kalman filter (Part 9)
- SIGMATEL's D-MAJOR MP3 Hard Drive Application Notes
- Happy Teacher's Day! Exclusive benefits for moderators and teacher netizens
- Reset time problem
- Understanding MIMO Antennas: Part I
- TM8712 User Manual
- Advantages and disadvantages of 7nm processors
- Measuring the operating current of pyboardCN V2
- CC4019------Four 2 to 1 data selector
- 51 MCU controls the USB host bottom-level driver of SL811HS