static u8 fac_us = ;//us与系统滴答的被乘数
static unsigned short fac_ms = ;//ms与系统滴答的被乘数
static unsigned int m_sys_seconds = ;//系统运行秒数
/**
* @description:系统滴答计时系统初始化
* @param 无
* @retval 无
*/
void SysTick_Init(void)
{
//配置系统滴答时钟源
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //选择外部时钟 HCLK/8
//设置预装载值
SysTick->LOAD = 0x895440;//时间加载
//计算ms & us转换因数
fac_us = SystemCoreClock/; //为系统时钟的1/8
fac_ms = (unsigned short)fac_us*; //代表每个ms需要的systick时钟数 //开启中断,使能计时
SysTick->CTRL |= (SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk) ; //开始倒数 } /*-------------------------------------------------------------------------------------------------
*@description: 返回当前系统运行的秒数.(溢出不计)
--------------------------------------------------------------------------------------------------*/
unsigned long sys_seconds(void)
{
return m_sys_seconds;
} /*-------------------------------------------------------------------------------------------------
*@description: 返回当前系统运行的时间,Unit ms.(溢出不计)
--------------------------------------------------------------------------------------------------*/
unsigned long long sys_millis(void)
{
unsigned long long millisecond = (0x895440-SysTick->VAL)/fac_ms + m_sys_seconds*;
//当前系统运行时间ms
return millisecond;
} /*-------------------------------------------------------------------------------------------------
*@description: 返回当前系统运行的时间,Unit us.(溢出不计)
--------------------------------------------------------------------------------------------------*/
unsigned long long sys_micros(void)
{
unsigned long long microseconds = (0x895440-SysTick->VAL)/fac_us + m_sys_seconds*;
return microseconds;
} // couter 减1的时间 等于 1/systick_clk
// 当counter 从 reload 的值减小到0的时候,为一个循环,如果开启了中断则执行中断服务程序,
// 同时 CTRL 的 countflag 位会置1
// 这一个循环的时间为 reload * (1/systick_clk) void SysTick_Delay_Us( unsigned short ns_num)
{
unsigned long long current_us = sys_micros(); if((current_us + ns_num) <= 0x3e7fffb7bbf)//不存在溢出
{
current_us += ns_num;
do
{
;
}while(sys_micros() < current_us);
}
else//存在溢出
{
current_us = (current_us + ns_num) - 0x3e7fffb7bbf;
do
{
;
}while(sys_micros() <= 0x3e7fffb7bbf);
do
{ }while(sys_micros() < current_us);
}
} void SysTick_Delay_Ms( unsigned short ms_num)
{
unsigned long long current_ms = sys_millis(); if((current_ms + ms_num) <= 0xfffffed7)//不存在溢出
{
current_ms += ms_num;
do
{
;
}while(sys_millis() < current_ms);
}
else//存在溢出
{
current_ms = (current_ms + ms_num) - 0xfffffed7;
do
{
;
}while(sys_millis() <= 0xffffed7);
do
{
;
}while(sys_millis() < current_ms);
} } /**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
m_sys_seconds++;
}