stm32 uses systick to accurately delay

Publisher:dong125612Latest update time:2019-01-31 Source: eefocusKeywords:stm32  systick Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

To achieve precise delay using the system tick timer, the following five steps are required:
1. Set the tick timer clock by setting its control register and selecting the external clock, which is one eighth of the system clock. If the system clock is 72M, the tick timer clock is 9M. That is, it takes 9M clock cycles to time one second.
2. Set the value of the tick timer reload register, which is the number of clock cycles required for delay. For example, if a delay of 20 microseconds is required, the reload value is 20*9. If a delay of 20 milliseconds is required, the reload value is set to 20*9000.
3. Clear the value of the tick timer current value register to zero, so that it can count down from the set value when counting is enabled.
4. Set the tick timer control register to start countdown.
5. Query the status bit of the tick timer. When the timing time is up, turn off the timer and clear the current value of the timer.

void delay_init(u8 SYSCLK)
{
   SysTick->CTRL&=0xfffffffb;//Control register, select external clock, which is one eighth of the system clock (HCLK/8; 72M/8=9M)
   fac_us=SYSCLK/8; //Define global variables, that is, the number of clock cycles required to delay one microsecond (72/8=9, in microseconds)
   fac_ms=(u16)fac_us*1000; //The number of clock cycles required for one millisecond (9000)
}
void Delay_us(u32 nus)
{
  u32 temp;   
  SysTick->LOAD=nus*fac_us; //Time loading (that is, how many clock cycles to delay)  
  SysTick->VAL=0x00; //Clear counter
  SysTick->CTRL=0x01; //Start countdown
do
{
  temp=SysTick->CTRL;
}
  while(temp&0x01&&!(temp&(1<<16)));//Wait for time to arrive  
  SysTick->CTRL=0x00; //Turn off counter
  SysTick->VAL =0X00; //Clear counter
}
orvoid
Delay_us(u32 Nus)   
{   
SysTick->LOAD=Nus*9; //Time loading 72M main frequency     
SysTick->CTRL|=0x01; //Start countdownwhile      
(!(SysTick->CTRL&(1<<16))); //Wait for time to arrive   
SysTick->CTRL=0X00000000; //Turn off counter   
SysTick->VAL=0X00000000; //Clear counter        

Keywords:stm32  systick Reference address:stm32 uses systick to accurately delay

Previous article:Simulation IIC timing based on stm32 microcontroller
Next article:arm exception handling process

Recommended ReadingLatest update time:2024-11-16 16:37

STM32 bus matrix introduction
The main system consists of a 32-bit multi-layer AHB bus matrix, which can interconnect the following parts: ● Eight master buses: — Cortex™-M4F core I-bus, D-bus and S-bus — DMA1 memory bus — DMA2 memory bus — DMA2 peripheral bus — Ethernet DMA bus — USB OTG HS DMA bus ● Seven controlled buses: — Internal Flash I
[Microcontroller]
Reasons and solutions for STM32 RTC crystal oscillator not oscillating
It is a common "industry consensus" that the RTC crystal oscillator of STM32 often fails to oscillate. Many people ask for help on various electronic forums with questions like "Ask an expert for advice! What should I do if the RTC crystal oscillator does not oscillate?", and the answer can basically be summarized as
[Microcontroller]
Reasons and solutions for STM32 RTC crystal oscillator not oscillating
STM32 conversion mode
1. Main features of ADC: 12-bit resolution Modes: single and continuous conversion modes, scan mode, discontinuous mode Channel: rule group, injection group Power supply requirements: 2.4V~3.6V Input range: Vref- = Vin = Vref+ 16 analog input channels (0..15)   The temperature sensor is connected to channe
[Microcontroller]
STM32 conversion mode
PWM breathing light based on stm32
The hardware involved in this section of the program is relatively simple, mainly to examine the application of the stm32 clock I will not say much about the principles. The reference books are:  "STM32 Library Development Practical Guide" by Liu Huoliang and Yang Sen  "STM32 Development Guide (Library Function Versio
[Microcontroller]
PWM breathing light based on stm32
Understanding the STM32 startup file
        In the process of embedded application development, due to the use of C language programming, the execution process of the underlying registers of the machine is rarely involved. Generally, the code is written directly in the main function. It seems that main has become the starting point for granted, although
[Microcontroller]
stm32 HardFault_Handler debugging and problem finding method
stm32 HardFault_Handler debugging and problem finding method  There are two main reasons why HardFault_Handler failure occurs in STM32: 1. Memory overflow or out-of-bounds access. This requires standardizing the code when writing the program yourself, and you need to slowly troubleshoot when you encounter it. 2. Stack
[Microcontroller]
stm32 HardFault_Handler debugging and problem finding method
STM32 uCOS_II practice message queue
First of all, the message queue is similar to a counting semaphore and can save asynchronous events, but the counting semaphore saves the state quantity. It only accumulates the number of state triggers in a variable, while the message queue can save the sent data. At the same time, the message queue has the same prope
[Microcontroller]
STM32 study notes: Building FWlib 3.0 project under IAR
Development tool: JLink V7.0 Development environment: IAR5.3 Step 1: Download the library Download the STM32 V3.0 firmware library from the ST official website (http://www.st.com/stonline/products/support/micro/files/um0427.zip). Step 2: Copy the library file After decompressing the download
[Microcontroller]
STM32 study notes: Building FWlib 3.0 project under IAR
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号