* File Name : systick_delay.h
* Author : shifu
* Version :
* Date : 09/20/2008
* Description :
********************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYSTICK_DELAY_H
#define __SYSTICK_DELAY_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_systick.h"
/* SysTick clock countflag bit */
#define SysTick_CountFlag_Bit (u32)0x00010000
/* SysTick flag */
#define Count_Not_Arrived (u32)0x0
#define Count_Arrived (u32)0x00010000
/* CTRL TICKINT Mask */
#define CTRL_TICKINT_Enable ((u32)0x00000002)
#define CTRL_TICKINT_Disable ((u32)0xFFFFFFFD)
/* Exported functions ------------------------------------------------------- */
void ST_Delay_Ms(u16 ms);
#endif
//***************************************************************************
// Function Name : systick_delay.c
// Description : delay x ms 必须在AHB 设置以后使用
// Input : ms
// Output : None
// Return : None
//***************************************************************************
#include"systick_delay.h"
void ST_Delay_Ms(u16 ms)
{
u32 statusreg = Count_Not_Arrived;
//检查参数是否超过最大值0xffffff/9000=1864,在AHB 72MHz时 最长时间0xffffff/9000
if(ms > 1864) ms = 1864;
//不产生异常中断请求
SysTick->CTRL &= CTRL_TICKINT_Disable;
//时钟选择 HCLK/8
SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
//装入计数值
SysTick->LOAD = (u32)(ms * 9000);
//开始计数
SysTick->CTRL |= SysTick_Counter_Enable;
//检查标志位,等待延迟
while((statusreg & SysTick_CountFlag_Bit) == Count_Not_Arrived)
{
statusreg = SysTick->CTRL;
}
//停止计数
SysTick->CTRL &= SysTick_Counter_Disable;
//清除计数
SysTick->VAL = SysTick_Counter_Clear;
}//END
上一篇:STM32 外部中断配置
下一篇:STM32通用定时器的基本定时器功能实现灯闪烁
推荐阅读最新更新时间:2024-03-16 15:15