STM32F4(用SysTick实现Delay函数)[通俗易懂]

STM32F4(用SysTick实现Delay函数)[通俗易懂]STM32F4 用 SysTick 实现 Delay 函数 GitHub 仓库 https github com XinLiGitHub STM32F4xx Delay Example PS 博文不再更新 后续更新会在 GitHub 仓库进行 1 开发环境 1 适用芯片 STM32F4 全部芯片 2 固件库 STM32F4xx DSP StdPeriph Lib V1 8 0 3 IDE

STM32F4(用SysTick实现Delay函数)

GitHub仓库:https://github.com/XinLiGitHub/STM32F4xx_Delay_Example

PS:博文不再更新,后续更新会在GitHub仓库进行。

1,开发环境

1,适用芯片:STM32F4全部芯片

2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

3,IDE:MDK517

2,驱动源码

Delay.h文件

/****************************************************************
* Copyright (C) 2016, XinLi, all right reserved.
* File name: Delay.h
* Date: 2016.03.22
* Description: Delay Driver
*****************************************************************/

#ifndef __DELAY_H
#define __DELAY_H

/****************************************************************
* Header include
*****************************************************************/
#include "stm32f4xx.h"

/****************************************************************
* Macro definition
*****************************************************************/


/****************************************************************
* Type definition
*****************************************************************/


/****************************************************************
* Structure definition
*****************************************************************/



#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/****************************************************************
* Variable declaration
*****************************************************************/


/****************************************************************
* Function declaration
*****************************************************************/
void Delay_us(uint64_t nus);
void Delay_ms(uint64_t nms);
void Delay_s(uint64_t ns);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __DELAY_H */

Delay.c文件

/****************************************************************
* Copyright (C) 2016, XinLi, all right reserved.
* File name: Delay.c
* Date: 2016.03.22
* Description: Delay Driver
*****************************************************************/

/****************************************************************
* Header include
*****************************************************************/
#include "Delay.h"

/****************************************************************
* Global variables
*****************************************************************/


/****************************************************************
* Function declaration
*****************************************************************/
static void Delay_Init(void);

/****************************************************************
* Function definition
*****************************************************************/

/****************************************************************
* Function: Delay_Init
* Description: Delay Configuration.
* Input:
* Output:
* Return:
*****************************************************************/
static void Delay_Init(void)
{
static uint8_t first = 0;

if(first == 0)
{
first = 1;

SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; /* Disability SysTick counter */
}
}

/****************************************************************
* Function: Delay_us
* Description: Microsecond delay.
* Input: nus
* Output:
* Return:
*****************************************************************/
void Delay_us(uint64_t nus)
{
uint32_t temp = 0;
uint64_t nms = 0;

Delay_Init();

if(nus == 0)
{
return;
}

nms = nus / 1000;
nus = nus % 1000;

if(nms > 0)
{
Delay_ms(nms);
}

if(nus > 0)
{
SysTick->LOAD = SystemCoreClock / 8000000 * nus; /* Time load (SysTick-> LOAD is 24bit) */
SysTick->VAL = 0x000000; /* Empty counter */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown */

do
{
temp = SysTick->CTRL;
}
while(temp&0x01 && !(temp&(1<<16))); /* Wait time is reached */

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; /* Close Counter */
SysTick->VAL = 0x000000; /* Empty counter */
}
}

/****************************************************************
* Function: Delay_ms
* Description: Millisecond delay.
* Input: nms
* Output:
* Return:
*****************************************************************/
void Delay_ms(uint64_t nms)
{
uint32_t temp = 0;

Delay_Init();

if(nms == 0)
{
return;
}

while(nms > 500)
{
SysTick->LOAD = SystemCoreClock / 8000 * 500; /* Time load (SysTick-> LOAD is 24bit) */
SysTick->VAL = 0x000000; /* Empty counter */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown */

do
{
temp = SysTick->CTRL;
}
while(temp&0x01 && !(temp&(1<<16))); /* Wait time is reached */

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; /* Close Counter */
SysTick->VAL = 0x000000; /* Empty counter */

nms -= 500;
}

SysTick->LOAD = SystemCoreClock / 8000 * nms; /* Time load (SysTick-> LOAD is 24bit) */
SysTick->VAL = 0x000000; /* Empty counter */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown */

do
{
temp = SysTick->CTRL;
}
while(temp&0x01 && !(temp&(1<<16))); /* Wait time is reached */

SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; /* Close Counter */
SysTick->VAL = 0x000000; /* Empty counter */
}

/****************************************************************
* Function: Delay_s
* Description: Second delay.
* Input: ns
* Output:
* Return:
*****************************************************************/
void Delay_s(uint64_t ns)
{
while(ns > 0)
{
Delay_ms(1000);
ns--;
}
}
编程小号
上一篇 2025-01-30 07:30
下一篇 2025-02-06 20:01

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/114645.html