Written at the beginning, why is there a need for a timer for microsecond delay?
1. In the HAL firmware library, only Systick is used as a delay counter, and the millisecond delay HAL_Delay(). In order to increase the precise microsecond delay, the Systick configuration parameters are generally changed, but the HAL firmware library uses the HAL_Delay() function in many places, so it is recommended that you do not modify the Systick parameters automatically configured by the system;
2. Personally, I think that when the operating system is added, Systick will be occupied, and the MCU system's own time base must select other timers. In summary, the changes made to Systick are basically useless;
therefore, the method of using a timer to control subtle delays is more flexible. The user needs to add very little code, which is economical and practical, energy-saving and environmentally friendly- -;
Step 1. Configure the clock.
Note that you must make sure that the crystal frequency marked in the red part is consistent with the actual crystal frequency.
Step 2. Configure the timer .
When configuring the timer, you need to clarify the following points:
1. Timer clock frequency;
2. Timer overflow frequency;
First, let's look at the more important registers, as shown below:
The red line marks the place where we get:
Timer operating frequency = **APBx Timer Clock/PSC register value divided by the internal clock + 1;
for example,
if the timer clock frequency is 84MHz, without using the internal clock division, to make the timer operating frequency 1MHz (1us), as follows:
1MHz=84MHz/(83+1);
Timer overflow frequency = timer operating frequency / arr
Regarding when the arr reload value is loaded, the new value will be loaded only after an overflow update event occurs;
The relevant configuration is as follows:
System clock configuration, see the figure above;
here I use the TIM14 general timer for 1us delay, the configuration is as follows:
Step 3. Write the code
void delay_us(uint16_t us)
{
uint16_t differ = 0xffff-us-5;
/*To prevent interrupts from interrupting the delay and causing counting errors.
For example, the delay is 1us starting from 0xfffE, but it is interrupted by an interrupt
(The counter is still counting at this time), the reason is that the count reaches 0xffff)
The count stops, but because the count value is missed and the arr value is overloaded,
The actual delay is (0xffff+1)us
*/
HAL_TIM_Base_Start(&htim14);
__HAL_TIM_SetCounter(&htim14,differ);
while(differ<0xffff-5)
{
differ=__HAL_TIM_GetCounter(&htim14);
}
HAL_TIM_Base_Stop(&htim14);
}
Because the upward counting method is used, it needs to be converted, but the downward counting method does not need it;
The test code is as follows:
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
delay_us(5);
}
/* USER CODE END 3 */
The actual waveform is as follows:
Previous article:STM32 - System tick timer
Next article:How to generate microsecond (us) delay in STM32 CubeMX
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Issues that should be paid attention to when drawing PCB diagrams from the perspective of welding
- Can anyone help analyze the parameters of this DC-DC transformer?
- In the 5G era, IoT technology empowers smart healthcare
- 【TouchGFX Design】(2) Simple interface design
- Sony HT-Z9F speaker system disassembled with Rockchip RKNanoD chip
- [Rvb2601 Creative Application Development] lvgl Usage 2 - Drawing
- [DWIN Serial Port Screen] Nucleic Acid Sampling and Registration System - Three Links to DWIN Cloud
- Help! Why can the command sent by the microcontroller only display up to the fourth step?
- Future Outlook of IoT
- msp430g2553 hardware IIC