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