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


相关推荐

  • 一个简单的Spring的AOP例子

    一个简单的Spring的AOP例子

    2021年5月10日
    100
  • windows10搭建nas详细(docker搭建开发环境)

    Windows下ODrive固件开发环境搭建以下内容适用于希望修改ODrive固件的开发人员。因此,它假定您了解诸如如何使用Git,什么是编译器之类的知识。如果这听起来很陌生,以下内容对您来说可能不适合。文章目录Windows下ODrive固件开发环境搭建1准备要用到的开发工具2安装Python3安装ST-Link/V2Drivers4安装GitforWindo…

    2022年4月17日
    212
  • 全网都在"养龙虾",但你知道养它要花多少钱吗?

    全网都在"养龙虾",但你知道养它要花多少钱吗?

    2026年3月13日
    2
  • GridView 控件详细介绍

    GridView 控件详细介绍br GridView 控件详细介绍收藏 br 显示表格数据是软件开发中的一个周期性任务 ASP NET 提供了许多工具来在网格中显示表格数据 例如 GridView 控件 通过使用 GridView 控件 您可以显示 编辑和删除多种不同的数据源 例如数据库 XML 文件和公开数据的业务对象 中的数据 br br 1 GridView 数据绑定基础 br nbsp nbsp nbsp GridView 大部份场合下都是用来绑定数据源 进行数据的显示 一般情况下 可以绑定到 SqlDataSourc 控

    2025年6月15日
    5
  • FIO详解

    FIO详解fio FlexibleIOTe 安装 a 下载地址 http freshmeat sourceforge net projects fio b 安装两个插件 yuminstallli yuminstallzi c 编译安装 tar xvffio 2 1 10 tar gz cdfio 2 1 10 configure mak

    2026年3月19日
    2
  • sigmoid函数解析式_phonetic函数

    sigmoid函数解析式_phonetic函数Sigmoid函数,即f(x)=1/(1+e-x)。是神经元的非线性作用函数。广泛应用在神经网络中。神经网络的学习是基于一组样本进行的,它包括输入和输出(这里用期望输出表示),输入和输出有多少个分量就有多少个输入和输出神经元与之对应。最初神经网络的权值(Weight)和阈值(Threshold)是任意给定的,学习就是逐渐调整权值和阈值使得网络的实际输出和期望输出一致。给定以下的总输

    2025年8月22日
    5

发表回复

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

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