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


相关推荐

  • 蚂蚁矿机linux系统文件,蚂蚁显卡矿机G2系统一键还原教程[通俗易懂]

    蚂蚁矿机linux系统文件,蚂蚁显卡矿机G2系统一键还原教程[通俗易懂]一、还原准备工作[ol]一台办公电脑(建议win7或以上系统,配置不限)一个可以格式化的U盘(最少8G,速度不限)下载UltraISO刻录软件《uiso9_cn.exe》至桌面下载《clonezilla-live-2117-11-29-03-img.iso》文件至桌面下载《checksum64.exe》至桌面双击打开checksum64.exe,把下载下来的ISO文件移动至软件中,点击选项-&gt…

    2022年9月28日
    3
  • springboot源码解析详细版

    springboot源码解析详细版springboot源码解析(转)SpringBoot的入口类@SpringBootApplicationpublicclassStartupApplication{publicstaticvoidmain(String[]args){SpringApplication.run(StartupApplication.class,args)…

    2022年5月1日
    55
  • SSL探03

    SSL探03

    2022年1月11日
    62
  • 说说JSON和JSONP,也许你会豁然开朗

    转载地址:说说JSON和JSONP,也许你会豁然开朗 前言   由于Senc 前言   由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现。   当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯和本地数据库功能,又或者通过HTML5的WebSocket也可以实现与服务器的通讯和服务端推功能,但

    2022年2月25日
    46
  • git log 查看 当前分支的 提交历史[通俗易懂]

    git log 查看 当前分支的 提交历史[通俗易懂]gitlog查看当前分支的提交历史在提交了若干更新之后,想回顾下提交历史,可以使用gitlog命令查看默认不用任何参数的话,gitlog会按提交时间列出所有的更新,最近的更新排在最上面。看到了吗,每次更新都有一个SHA-1校验和、作者的名字和电子邮件地址、提交时间,最后缩进一个段落显示提交说明。gitlog有许多选项可以帮助你搜寻感兴趣的提交,接下来我们…

    2022年8月22日
    12
  • 大数据的使用方法,主要有哪些?「建议收藏」

    大数据的使用方法,主要有哪些?「建议收藏」我们正处于福雷斯特研究公司所描述的“用户时代”,这个时代中驱动业务决策的不再是公司,而是用户。基于这个原因,深度理解用户的重要性已经远胜以往,因此许多机构开始使用大数据技术来挖掘用户信息。在这个时代,企图收获成功(甚至是求生存)的在线业务必须切实的理解顾客的体验和行为,因此海量数据的收集及挖掘能力成了这些机构的必备手段。当下,有许多机构的分析仍处于数据的收集上,组织能力的缺乏和技术的限制让这些收…

    2022年5月5日
    39

发表回复

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

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