当前位置:首页 > 单片机 > 单片机
[导读] 1,开发环境 1,适用芯片:STM32F4全部芯片 2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0 3,IDE:MDK5172,驱动源码 Delay.h文件/*************************************************************

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

}

}



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

北京——2024年10月29日 亚马逊云科技日前宣布,通过与光环新网与西云数据的紧密合作,在亚马逊云科技(北京)区域和(宁夏)区域推出Amazon Lambda SnapStart功能,将基于Java Lambda函数的...

关键字: 函数 数据处理

北京2024年10月29日 /美通社/ -- 亚马逊云科技日前宣布,通过与光环新网与西云数据的紧密合作,在亚马逊云科技(北京)区域和(宁夏)区域推出Amazon Lambda SnapStart功能,将基于Java La...

关键字: START 亚马逊 PS 函数

关于数据分析与可视化,以下介绍Pandas读取数据以及保存数据的方法,毕竟我们很多时候需要读取各种形式的数据,以及将我们需要将所做的统计分析保存成特定的格式。

关键字: 函数 Panda

深圳2024年8月15日 /美通社/ -- 近日,知名量化交易平台水母量化正式推出了突破性的轮动交易策略回测功能,目前已进入公测阶段。该功能实现了从策略设计、历史回测到实盘自动交易一体化流程,帮用户解决了策略验证难、交易...

关键字: 编程 仿真 函数 开源

Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。 Python提供了高效的高级数据结构,还能简单有效地面向对象编程。

关键字: python 函数 对象编程

测试数据综合分析的绝佳工具,深受工程师和研究员欢迎

关键字: 后处理分析软件 向导 函数

由上图中可以知道进程地址空间中最顶部的段是栈,代码中调用函数、定义局部变量(但不包含static修饰的变量)或声明的类的实例等等都要使用栈空间,当函数执行完(也就是程序执行超过了这个函数的作用范围的时候),操作系统会把该...

关键字: 进程地址 局部变量 函数

星标/置顶 公众号,硬核文章第一时间送达!链接| https://zhuanlan.zhihu.com/p/274473971题很多,先上题后上答案,便于大家思考问题点:1、C和C的特点与区别?2、C的多态3、虚函数实现...

关键字: 腾讯 函数 进程 AI

程序接口是操作系统为用户提供的两类接口之一,编程人员在程序中通过程序接口来请求操作系统提供服务。面向过程语言最基本的单元是过程和函数。

关键字: 程序接口 过程 函数

星标「嵌入式大杂烩」,一起进步!链接:https://www.cnblogs.com/jozochen/p/8541714.html一、问题复现稳定复现问题才能正确的对问题进行定位、解决以及验证。一般来说,越容易复现的问...

关键字: 嵌入式开发 函数 代码 寄存器
关闭