Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of STM32 delay function

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what are the methods of STM32 delay function, I hope you will gain something after reading this article, let's discuss it together!

The delay function is often used in the programming process of single-chip microcomputer, and the most commonly used ones are microsecond delay delay_us () and millisecond delay_ms (). This paper introduces four kinds of delay functions implemented in different ways based on STM32F207.

1. General delay

This kind of delay mode should be the earliest delay function when we come into contact with 51 single-chip microcomputer. This is relatively simple, let the single-chip microcomputer do some unimportant work to pass the time, often realized by loops, in some compilers, the code will be optimized, resulting in low precision, used in general delay, insensitive to precision in the application scene.

/ / microsecond delay void delay_us (uint32_t delay_us) {volatile unsigned int num; volatile unsigned int t; for (num = 0; num)

< delay_us; num++) { t = 11; while (t != 0) { t--; } }}//毫秒级的延时void delay_ms(uint16_t delay_ms){ volatile unsigned int num; for (num = 0; num < delay_ms; num++) { delay_us(1000); }} 上述工程源码仓库:https://github.com/strongercjd/STM32F207VCT6/tree/master/02-Template 2、定时器中断 定时器具有很高的精度,我们可以配置定时器中断,比如配置1ms中断一次,然后间接判断进入中断的次数达到精确延时的目的。这种方式精度可以得到保证,但是系统一直在中断,不利于在其他中断中调用此延时函数,有些高精度的应用场景不适合,比如其他外设正在输出,不允许任何中断打断的情况。 STM32任何定时器都可以实现,下面我们以SysTick 定时器为例介绍: 初始化SysTick 定时器: /* 配置SysTick为1ms */RCC_GetClocksFreq(&RCC_Clocks);SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); 中断服务函数: void SysTick_Handler(void){ TimingDelay_Decrement();}void TimingDelay_Decrement(void){ if (TimingDelay != 0x00) { TimingDelay--; }} 延时函数: void Delay(__IO uint32_t nTime){ TimingDelay = nTime; while(TimingDelay != 0);}3、查询定时器 为了解决定时器频繁中断的问题,我们可以使用定时器,但是不使能中断,使用查询的方式去延时,这样既能解决频繁中断问题,又能保证精度。 STM32任何定时器都可以实现,下面我们以SysTick 定时器为例介绍。 STM32的CM3内核的处理器,内部包含了一个SysTick定时器,SysTick是一个24位的倒计数定时器,当计到0时,将从RELOAD寄存器中自动重装载定时初值。只要不把它在SysTick控制及状态寄存器中的使能位清除,就永不停息。 SYSTICK的时钟固定为HCLK时钟的1/8,在这里我们选用内部时钟源120M,所以SYSTICK的时钟为(120/8)M,即SYSTICK定时器以(120/8)M的频率递减。SysTick 主要包含CTRL、LOAD、VAL、CALIB 等4 个寄存器。 ▼CTRL:控制和状态寄存器 ▼LOAD:自动重装载除值寄存器

▼ CALIB: calibration value register

Can not be used, no more introduction

Sample code

Void delay_us (uint32_t nus) {uint32_t temp; SysTick- > LOAD = RCC_Clocks.HCLK_Frequency/1000000/8*nus; SysTick- > VAL=0X00;// emptying counter SysTick- > CTRL=0X01;// enable, reduce to zero is no action, use external clock source do {temp=SysTick- > CTRL;// to read the current countdown} while ((temp&0x01) & (temp& (1VAL = 0X00) / / emptying counter} void delay_ms (uint16_t nms) {uint32_t temp; SysTick- > LOAD = RCC_Clocks.HCLK_Frequency/1000/8*nms; SysTick- > VAL=0X00;// emptying counter SysTick- > CTRL=0X01;// enable, reducing to zero means no action. External clock source do {temp=SysTick- > CTRL;// is used to read the current countdown value} while ((temp&0x01) & (temp& (1VAL = 0X00umbent / emptying counter)

The source code warehouse of the above project: https://github.com/strongercjd/STM32F207VCT6/tree/master/04-Delay

4. Assembler instruction

If the system hardware resources are tight, or there is no additional timer, and you do not want the common delay of method 1, you can use assembly instructions to delay, which will not be compiled and optimized and the delay is accurate.

STM32F207 in IAR environment

/ *! * @ brief Software delay * @ param ulCount: for every 1 increase in delay clock * @ return none * @ note ulCount, this function increases 3 clocks * / void SysCtlDelay (unsigned long ulCount) {_ _ asm (">" >

These three clocks refer to the CPU clock, which is the system clock. 120MHZ, that is to say, 1s has a clock of 120m, and a clock is 1max 120 us, that is, a cycle of 1max 120 us. Three clocks because three instructions were executed.

Organize the ms and us interfaces in this way, and pass the test in both Keil and IAR environments.

/ * 120Mhz clock, when ulCount is 1, the function takes 3 clocks, delay = 3*1/120us=1/40us*//*SystemCoreClock=120000000us level delay, n microsecond SysCtlDelay (n * (SystemCoreClock/3000000)); ms level delay, n millisecond SysCtlDelay (n * (SystemCoreClock/3000)); m level delay, n second SysCtlDelay (n * (SystemCoreClock/3)) * / # if defined (_ _ CC_ARM) / *! < ARM Compiler * / _ _ asm voidSysCtlDelay (unsigned long ulCount) {subs R0, # 1; bne SysCtlDelay; bx lr } # elif defined (_ _ ICCARM__) / *! < IAR Compiler * / voidSysCtlDelay (unsigned long ulCount) {_ _ asm ("subs R0, # 1\ n"bne.n SysCtlDelay\ n"bx lr") } # elif defined (_ _ GNUC__) / *! < GNU Compiler * / void _ attribute__ ((naked)) SysCtlDelay (unsigned long ulCount) {_ _ asm ("subs R0, # 1\ n"bne SysCtlDelay\ n"bx lr") } # elif defined (_ _ TASKING__) / *! < TASKING Compiler * / / * none * / # endif / * _ CC_ARM * / after reading this article, I believe you have a certain understanding of "what are the methods of STM32 delay function". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report