3 delay functions in STM32

Publisher:红尘清梦Latest update time:2018-09-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The first delay function:


void delay(u16 num)

{

  u16 i,j;

  for(i=0;i

    for(j=0;j<0x800;j++);

}

eg: delay(50);

The second delay function:


void Delayms(u16 ms)

{

  u16 i,j;

  u8k;

  for(i=0;i

    for(j=0;j<0x0500;j++) k++;

}

The third delay function:


static u8 fac_us=0;//us delay multiplier

static u16 fac_ms=0;//ms delay multiplier

//Initialize delay function

//SYSTICK clock is fixed to 1/8 of the HCLK clock

//SYSCLK: system clock

void delay_init(u8 SYSCLK)

{

SysTick->CTRL&=0xfffffffb;//Clear bit2, select external clock HCLK/8

fac_us=SYSCLK/8;    

fac_ms=(u16)fac_us*1000;

}    

//Delay nms

//Pay attention to the range of nms

//SysTick->LOAD is a 24-bit register, so the maximum delay is:

//nms<=0xffffff*8*1000/SYSCLK

//SYSCLK unit is Hz, nms unit is ms

//Under 72M conditions, nms<=1864 

void delay_ms(u16 nms)

{    

u32 temp;   

SysTick->LOAD=(u32)nms*fac_ms;//Time loading (SysTick->LOAD is 24bit)

SysTick->VAL =0x00; //Clear the counter

SysTick->CTRL=0x01; //Start the countdown  

do

{

temp=SysTick->CTRL;

}

while(temp&0x01&&!(temp&(1<<16))); //Wait for time to arrive   

SysTick->CTRL=0x00; //Close counter

SysTick->VAL =0X00; //Clear the counter      

}   

//Delay nus

//nus is the number of us to be delayed.       

void delay_us(u32 nus)

{

u32 temp;      

SysTick->LOAD=nus*fac_us; //Time loading    

SysTick->VAL=0x00; //Clear the counter

SysTick->CTRL=0x01; //Start the countdown  

do

{

temp=SysTick->CTRL;

}

while(temp&0x01&&!(temp&(1<<16))); //Wait for time to arrive   

SysTick->CTRL=0x00; //Close counter

SysTick->VAL =0X00; //Clear the counter  

}


Keywords:STM32 Reference address:3 delay functions in STM32

Previous article:STM32 system tick timer (systick) application
Next article:STM32 about BOOT0 and BOOT1 settings

Recommended ReadingLatest update time:2024-11-16 19:39

STM32 RTC Summary
RTC can be used to set the real-time clock and generate a second interrupt or an alarm interrupt. When the chip is powered, the clock timing can still be maintained after the system loses power. RTC settings involve rewriting the power supply (PWR), backup area (BKP), and some RCC registers. The crystal oscillator
[Microcontroller]
STM32 ISP burning process
 STM32 embeds a boot program in the chip production process, whose function is to download the program to Flash through the serial port, which provides great convenience for future software updates. Users do not need to use the simulation port to download the program, which greatly improves work efficiency. After ST
[Microcontroller]
STM32 USB DFU firmware upgrade must read
Upgrade code engineering instructions After the project is created, the following steps need to be performed: 1. First, click on the compiler's "Target Option" , then select the Target option, set IROM1 under on-chip in the "Read/Only Memory Areas" field, and modify the download start address and size. Here we use the
[Microcontroller]
STM32 USB DFU firmware upgrade must read
STM32 re-learning - Integrated Development Environment IDE
An embedded system is a computer system that combines software and hardware. The software code is written, compiled, assembled, and linked to generate an executable file, which is then loaded into the ROM or flash of the embedded system, and the MCU of the embedded system is used to control and detect external devices.
[Microcontroller]
STM32 re-learning - Integrated Development Environment IDE
Design of automatic range voltmeter based on STM32
  The whole system in the scheme can be powered by a 9V battery, achieving low power consumption and portability. AC measurement is measured by converting AC signals into DC voltage using the AD637 true RMS conversion chip; input voltage conversion is performed using a reverse amplifier with clamp protection, achievin
[Microcontroller]
Design of automatic range voltmeter based on STM32
STM32 internal FLASH read and write interface function
Because the internal FLASH is used instead of the external EEPROM, the parameters are placed at 0x08000000+320K of the STM32, of which 20K is the bootloader and 300K is the application. Principle: First move the contents of the entire FLASH page to RAM, then modify it in RAM, then erase the entire FLASH page, and th
[Microcontroller]
STM32 USB virtual serial port
Serial port debugging is used more and more in projects, and the shortage of serial port resources has become particularly prominent. Many people who use notebooks have a deep understanding that they cannot develop without a USB to serial port tool. This chapter briefly outlines the transplantation of USB virtual seri
[Microcontroller]
STM32 USB virtual serial port
STM32 development board development notes (4) - startup settings
stm32 has a boot pin Boot0 and Boot1, which are used to set the startup position of the code As shown below Boot0 is located at pin 94, which is a dedicated Boot pin. Boot1 is located at pin 37 and is multiplexed with PB2. Boot1 Boot0 x 0: boot0 is 0, boot1 is any value, the code starts from the internal
[Microcontroller]
STM32 development board development notes (4) - startup settings
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号