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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • IntelliJ IDEA 2021.5 x64 激活码[在线序列号]

    IntelliJ IDEA 2021.5 x64 激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    45
  • matlab 查看函数,如何查看MATLAB函数的源代码 | 学步园「建议收藏」

    matlab 查看函数,如何查看MATLAB函数的源代码 | 学步园「建议收藏」如何查看MATLAB函数的源代码大家都知道MATLAB是开源的,所有的函数源代码都是可以查看的。但是,对于初学者来说,可能还不知道如何查看MATLAB函数的源代码。函数之  type假设需要查看function_name的源代码,在命令窗口中键入 type  function_name即:>>typeimreadfunction[X,map,alpha]=imread(v…

    2025年11月11日
    4
  • Java开发谈:2021Java高级面试题及答案

    Java开发谈:2021Java高级面试题及答案前言最近一段时间发现经常看到很多人,对Spring源码比较感兴趣,日常开发中,无论你做什么什么项目,大部分都离不开Spring生态的那一套东西,所以很多人对Spring底层源码实现很感兴趣,但是有些从来没有接触过源码的开发者,在看Spring源码的过程中确实及其难受的,为什么,大部分人看源码基本都是debug一点一点去看的,最后发现,越追越离谱,越追越深,到最后都追到JDK源码了,也没有明白是什么意思!对于学习源码,我的看法是,先去完全的熟悉它的用法,想一下如果让你来实现,你会怎么实现!有了这些想法之后

    2022年7月18日
    17
  • 防暴力激活成功教程密码的脚本「建议收藏」

    防暴力激活成功教程密码的脚本「建议收藏」前几天,突然发现日志文件/var/log/auth.log(ubuntu)或者/var/log/secure(centos),存在好多尝试激活成功教程用户密码的现象,如下脚本通过获取到日志文件的IP地址,加入到/etc/hosts.deny文件中,拒绝该IP地址的尝试登陆服务器。#1.定义变量,获取日志中刷选的IP地址DIR_file=/var/log/auth.logIP_list=`awk’/…

    2022年8月22日
    5
  • python 元类编程_python抽象基类

    python 元类编程_python抽象基类前言通常我们创建类都是使用class类名,但是小伙伴们有没有想过,类是由谁来创建的呢,python中常说的万物皆对象,对象是由类创建的,那类本身也可以看做是对象,类可以由元类type创建type

    2022年8月7日
    7
  • NoSQL之Redis数据库初探

    一、NoSQL的风生水起1.1后Web2.0时代的发展要求随着互联网Web2.0网站的兴起,传统的关系数据库在应付Web2.0网站,特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经

    2021年12月28日
    60

发表回复

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

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