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

STM32F4(用SysTick实现Delay函数)[通俗易懂]STM32F4(用SysTick实现Delay函数)1,开发环境     1,适用芯片:STM32F4全部芯片    2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0     3,IDE:MDK5172,驱动源码     Delay.h文件/**************************************

大家好,又见面了,我是你们的朋友全栈君。

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--;
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139478.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月7日 下午3:00
下一篇 2022年5月7日 下午3:20


相关推荐

  • 值得推荐的Idea十几大优秀插件

    值得推荐的Idea十几大优秀插件最近,闲来无事,为了改变一下枯燥的编程环境,特地搜寻了下有助提升代码功力的插件,够装逼,够狂,拽,屌~绚丽的画面,多彩的跳动,让你区别其他程序猿。产品,测试,开发看到你的界面,眼睛都会发光~算了,我实在是编不下去,自己去体验吧~PS:☆半星★一星主要是以狂拽屌指数来排名12、Stackoverflow这个插件其实是最实用的插件,程序猿遇到的问题…

    2022年6月16日
    130
  • Prometheus TSDB存储原理

    Prometheus TSDB存储原理Python 微信订餐小程序课程视频 https blog csdn net m0 article details Python 实战量化交易理财系统 https blog csdn net m0 article details Prometheus 包含一个存储在本地磁盘的时间序列数据库 同时也支持与远程存储系统集成 比如 grafanacloud 提供的免费云存储 API 只需将 remote write 接口信息填写在 Prome

    2025年7月16日
    7
  • kimi 又好起来了!月之暗面开源 Kimi K2:320 亿参数 MoE 模型登顶 HF 榜首

    kimi 又好起来了!月之暗面开源 Kimi K2:320 亿参数 MoE 模型登顶 HF 榜首

    2026年3月12日
    3
  • 5.20爬虫结——Mu

    5.20爬虫结——Mu最新版墨镜风格模板发布了 有需要的朋友可以进来看看 589 官方手机模板在哪安装 347 手机触摸屏被劫持跳转其它网站 562 终于知道 DZ 官网为什么没落了 535 主题自带的二级导航怎么修改啊 505 以前有农场这些插件能安装 现在没了吗 513 请问这个网站用的是什么应用程序 515 萌新想问一下 能否可以实现现在主流素

    2026年3月26日
    2
  • Oracle ltrim() 函数用法

    Oracle ltrim() 函数用法前面有说到过 LPAD 和 RPAD 这两个函数用法的文章 今天发现与之相反意义的另外两个函数 那就是 LTRIM RTRIM 这次就挑 LTRIM 这一函数来讲讲 具体的语法格式如下 nbsp LTRIM c1 c2 功能 删除左边出现的字符串 参数 C1 字符串 c2 追加字符串 默认为空格 返回 字符型接下来看看几个例子 selectltrim abcdd

    2026年3月18日
    2
  • 腾讯云代理商:如何用腾讯云轻量应用服务器部署 OpenClaw 并和飞书进行集成?

    腾讯云代理商:如何用腾讯云轻量应用服务器部署 OpenClaw 并和飞书进行集成?

    2026年3月13日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号