// Initialize the millisecond unit
void Timer_Init()
{
TIM1_PSCRH = (unsigned char) ( ( (F_CPU / 1000) - 1) >> 8); /* 16M system clock is pre-divided by f=fck/(PSCR+1) */
TIM1_PSCRL = (unsigned char) ( (F_CPU / 1000) - 1); /* PSCR=0x3E7F, f=16M/(0x3E7F+1)=1000Hz, each counting cycle is 1ms */
TIM1_ARRH = 0; /* I don't know why these three lines need to be initialized first. If they are not used regularly for the first time, they will not work properly*/
TIM1_ARRL = 1; /* */
TIM1_CR1 = 0x01; /* Enable the counter and start counting */
}
/* In ms, the timer is used for more accurate calculation*/
void Delay( unsigned int ticks )
{
TIM1_CR1 = 0x00; /* Counter off */
TIM1_SR1 = 0; /* Clear the update interrupt flag */
TIM1_CNTRH = 0;
TIM1_CNTRL = 0; /* Clear the counter */
TIM1_ARRH = (ticks >> 8); /* Auto-reload register ARR=0x01F4=500 */
TIM1_ARRL = ticks; /* Generate an interrupt every 500 ticks, i.e. 500ms */
TIM1_CR1 = 0x01; /* Enable the counter and start counting */
while ( !(TIM1_SR1 & 0x01) )
;
TIM1_CR1 = 0x00; /* Counter stops counting */
TIM1_SR1 = 0; /* Clear timer 0 flag */
}
Previous article:STM8S delay function
Next article:Why simple software delay does not work when developing STM8 with IAR
Recommended ReadingLatest update time:2024-11-15 07:48
- 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
- [NXP Rapid IoT Review] Hello Sensor
- What are the advantages of electronic load switches over MOS or transistors as output control? Now many domestic semiconductor...
- Lighter igniter electric shock/ignition principle
- This is the second time I haven't seen any replies in the past few days. The pattern is: there are replies on the 20th floor, but no more on the 21st floor; there are replies on the 40th floor, but no more on the 41st floor...
- Find TI, NXP, ALTERA, XILINX, ST, Microchip factory stock chips
- Record the use of TM1668 driver
- Newbie help, ESP8266 program problem
- GaN Applications in RF and Electronics
- CC2530 Basic Experiment 1 I/O Experiment Code
- Qorvo Online Design Conference - Wi-Fi Architecture and Future Development of Wi-Fi Standards