Timer selection:
The stm8 timer is slightly different from the stm32 timer. The stm32 timer is 24 bits - 16,777,216. According to the 72M clock divided by 8, the period is about 1.86s, while the stm8 timer is 16/8 - 65535/255, and its own clock is 16M. Even with the 16-division period, it is much less than 1s.
Currently, the TIM2 timer is selected, with a division ratio of 1 and a maximum period of 65536/16, which is approximately equal to 4096us.
initialization
void TIM_Delay_Init()
{
/* The auto-reload value range is 0~255 for TIM4 and 0~65535 for TIM2*/
TIM2_TimeBaseInit(TIM2_PRESCALER_1,65535); //65535*(1/16)≈4095us
TIM2_Cmd(DISABLE); //Disable the timer
}
Delay function
It is best to use register operations in delay functions to reduce the amount of calculations and reduce errors.
void TIM_Delay_us(uint16_t nus)
{
uint16_t value = nus*16;
TIM2->CNTRH = 0; //calculated value cleared
TIM2->CNTRL = 0;
TIM2->CR1 |= (uint8_t)TIM2_CR1_CEN; //Start timing
while(TIM2_GetCounter() < value)
{
/*if(TIM2_GetFlagStatus(TIM2_FLAG_UPDATE)) //The maximum delay time for a single us is reached
{
TIM2_ClearFlag(TIM2_FLAG_UPDATE);
break;
}*/
};
TIM2->CR1 &= (uint8_t)(~TIM2_CR1_CEN); //Stop timing
}
Conclusion
When an 8-bit timer (TIM4) is selected, the one-division period is 16us, the two-division period is 32us, and in the case of ms delay, 5/10/20us can be used as the delay period to implement functions such as Delay_10us() and Delay_N_10us().
Previous article:stm8s timer 1 delay_STM8S_library function_timer automatically loads TIM1
Next article:STM8-TIMER implements system tick timer
- 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
- Japan Kenwood Power Supply Repair
- 【Development related】Detection principles and advantages of laser gas sensors
- IAR STM8 software has been updated to a new version, and the following prompt appears. Does anyone know how to solve it?
- [Bluesight AB32VG1 RISC-V board "meets" RTT] Audio starts
- Why Python is the programming language of the future
- [SAMR21 new gameplay] 7. Use webusb
- Qorvo's BAW technology greatly improves the detection speed of new crown test kits
- Seeking long-term cooperation engineers to do part-time work: STM32 51 8S FPGA image processing
- Disassembled 5 surveillance cameras, 3 brands, to check the quality
- How does the Zigbee module connect to third-party gateways such as Philips Hue, Amazon alexa, etc. What is the standard protocol?