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--;
}
}
上一篇:STM32F4(USART+DMA+静态内存)
下一篇:STM32F4(Flash读保护)
推荐阅读最新更新时间:2024-03-16 16:00
设计资源 培训 开发板 精华推荐
- 有奖直播:新科技、新课堂、新学期~ ADI软件定义无线电ADALM-Pluto入门实战
- 【抢楼有礼】TI TMS320F28377S 入门经验大搜集!
- 有奖直播|罗彻斯特电子半导体全周期解决方案 助您应对供应链中断和元器件停产的挑战
- ADI•世健工业嘉年华——工业趴主题征文及投票活动!
- 4月TI两场EP直播,都挺好:超声气体流量计量创新方案+SimpleLink平台小鲜肉CC13X2/CC26X2专场
- ADI & 世健 新基建系列第一期——工业以太网 答题赢好礼!
- EEWORLD十周年为你而任性——上百块ST开发板团购盛宴开始啦!49元包邮,发帖还返现
- 【团购】EE_FPGA V2.0配套器件