Ⅰ. Write in front
In some specific situations, precise delay (us level) is required, especially for the underlying driver. If software delay is used, the delay will change with the change of system clock and various factors. Therefore, TIM precise delay is required.
Blocking delay: From the start to the end of the delay, the program is blocked there and will not jump to other places (except interrupts) to execute the program. Friends who don’t understand can search for the answer online.
There are many types and functions of TIM. This article is about the basic knowledge and is relatively simple. For more powerful and practical functions of timers, please pay attention to my later articles.
For your convenience, the content of this article has been organized into a PDF file:
http://pan.baidu.com/s/1i5uWhJR
II. Basic knowledge of TIM
There are three types of timers in STM8S: basic timer, general timer and advanced timer. The basic timer is an 8-bit counting timer, and the general and advanced timers are 16-bit counting timers.
Different types of timers have different functions and complexities, and are suitable for different occasions. This article uses the most basic and simple 8-bit basic timer to explain the delay of TIM.
Emphasize one point: 8-bit counting timer, the maximum count value is 256.
TIM4 basic timer functions:
Ø 8-bit up-counting (UP-COUNTER) automatic reload counter;
Ø 3-bit programmable prescaler (modifiable during operation) providing 8 frequency division ratios: 1, 2, 4, 8, 16, 32, 64 and 128.
Ø Interrupt generation: If interrupts are enabled, an interrupt is generated when the counter is updated (counter overflows). This article does not enable interrupts.
III. Software Engineering Source Code
1. About the project
The engineering code provided in this article is based on the previous "STM8S_Demo" and modified by adding TIM timer. Beginners can refer to my previous corresponding basic articles, which are more detailed.
Software engineering source code implementation function: Change the LED on and off state through blocking delay (500ms) to observe the delay size. If you want to measure the accuracy of the delay, you can change TIMDelay_Nms(500) to TIMDelay_N10us(10) (delay 100us), and measure the frequency of the LED pin through an oscilloscope to be 5KHz (period 200us).
2. Software Overview
This article provides a simple introduction to the content included in software engineering:
System Initialization: System_Initializes
v BSP_Initializes: clock initialization CLK_Configuration and GPIO_Configuration initialization;
v TIMER_Initializes: Timer initialization, the focus of this article.
Function implementation: while(1)
3. Code Analysis Description
The contents of BSP_Initializes will not be described in detail here, please refer to the previous article: STM8S_001_GPIO Basics
This article focuses on the contents of the bsp_timer.c file:
A.TIMER_InitializesTimer initialization
void TIMER_Initializes(void)
{
TIM4_TimeBaseInit(TIM4_PRESCALER_2, 79);
TIM4_ClearFlag(TIM4_FLAG_UPDATE);
}
The software engineering we provide is to achieve a 10us delay, and the implementation formula is: 16MHz / 2 / (79+1) = 0.1MHz (100KHz).
The first parameter TIM4_PRESCALER_2: 2-frequency division, this parameter is as follows:
typedef enum
{
TIM4_PRESCALER_1 = ((uint8_t)0x00),
TIM4_PRESCALER_2 = ((uint8_t)0x01),
TIM4_PRESCALER_4 = ((uint8_t)0x02),
TIM4_PRESCALER_8 = ((uint8_t)0x03),
TIM4_PRESCALER_16 = ((uint8_t)0x04),
TIM4_PRESCALER_32 = ((uint8_t)0x05),
TIM4_PRESCALER_64 = ((uint8_t)0x06),
TIM4_PRESCALER_128 = ((uint8_t)0x07)
} TIM4_Prescaler_TypeDef;
The second parameter 79: The value of this parameter is actually the value of the auto-reload register. As can be seen from the formula, it is the source of the 10us delay.
Many people don’t understand why it is 79 instead of 80?
The reason is that the counting starts from 0, so 0 to 79 means counting 80, so here it is 79.
Statement TIM4_ClearFlag(TIM4_FLAG_UPDATE):
The meaning of this statement is very simple, clear the UPDATE update flag.
B. Delay N 10us: void TIMDelay_N10us(uint16_t Times)
void TIMDelay_N10us(uint16_t Times)
{
TIM4_SetCounter(0); //Count value reset to zero
TIM4_Cmd(ENABLE); //Start the timer
while(Times--)
{
while(RESET == TIM4_GetFlagStatus(TIM4_FLAG_UPDATE));
TIM4_ClearFlag(TIM4_FLAG_UPDATE);
}
TIM4_Cmd(ENABLE); //turn off the timer
}
Why is it N 10us?
From the above timer initialization, we can know that a counting process (delay) is 10us, and the parameter Times represents the number of times the delay of 10us is to be executed.
TIM4_SetCounter(0);
Before starting the timer each time, reset the count value to zero to ensure that the first count (delay) is accurate.
while(RESET == TIM4_GetFlagStatus(TIM4_FLAG_UPDATE));
This statement means that the program keeps reading the update flag TIM4_FLAG_UPDATE (blocked) until the read flag is valid (count is full), then it jumps out of the while loop.
TIM4_ClearFlag(TIM4_FLAG_UPDATE);
Clear the update flag TIM4_FLAG_UPDATE. After the above flag is valid, it needs to be cleared, and then the next counting process is carried out.
I believe that everyone can understand the start and stop timer here, which starts from the execution of the TIMDelay_N10us function to the end of the operation process. Here is a reminder: the counting process is a cyclic process, and try to avoid repeated switching timers (it will take some time). The TIMDelay_Nms I provided is actually not very accurate strictly speaking, and this function is repeated switching.
C. Specific implementation functions
The while in the main function is the specific function implemented by the source code of this article. An LED light (IO) is output alternately in high and low, and a timer is used in the middle to delay more accurately by 500ms to achieve the effect of LED turning on and off.
Code:
while(1)
{
LED_ON; //LED is on
TIMDelay_Nms(500);
LED_OFF; //LED off
TIMDelay_Nms(500);
}
Strictly speaking, there is a certain deviation in the TIMDelay_Nms function. From the above description, I believe you know how to modify it to avoid such errors.
IV. Download
STM8S information:
http://pan.baidu.com/s/1o7Tb9Yq
Software source code project (STM8S-A02_TIM precise delay (blocking)):
http://pan.baidu.com/s/1c2EcRo0
Previous article:The problem of delay function being optimized when debugging stm8 microcontroller with IAR
Next article:STM8L101F3P6 waveforms of different writing methods of millisecond delay function
Recommended ReadingLatest update time:2024-11-16 16:02
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
- Can you give me an example of how to read the development board data in real time via TCP protocol on PC?
- AC7801 chip serial port printing problem
- I would like to ask you about the closed-loop control of the motor encoder
- How to protect privacy using Bluetooth
- Tesla Robot Optimus Prime Conference Uncut 4K HD First Episode
- O-RAN development trends, reference architecture, and interoperability testing
- Qorvo PAC series highly integrated motor control chips and applications
- Five skills required for RF test engineers
- 【GD32L233C-START Review】Display driver for color OLED screen
- Would you choose the popular outdoor power supply?