//源檔案
static uint32_t fac_us = 0; // us延時倍乘數
/**
* @brief 初始化延遲函式
* 當使用ucos的時候,此函式會初始化ucos的時鐘節拍
* SYSTICK的時鐘固定為AHB時鐘的1/8
* @param SYSCLK 系統時鐘頻率
*/
void DelayPhyConfig()
{
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); //SysTick頻率為HCLK
fac_us = 216; //不論是否使用OS,fac_us都需要使用
}
/**
* @brief 延時nus
*
* @param nus 要延時的us數.值不要大于1000us
*/
void DelayPhyUs(uint32_t nus)
{
uint32_t ticks;
uint32_t told, tnow, tcnt = 0;
uint32_t reload = SysTick->LOAD; //LOAD的值
ticks = nus * fac_us; //需要的節拍數
told = SysTick->VAL; //剛進入時的計數器值
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
tcnt += told - tnow; //這里注意一下SYSTICK是一個遞減的計數器就可以了.
else
tcnt += reload - tnow + told;
told = tnow;
if (tcnt >= ticks)
break; //時間超過/等于要延遲的時間,則退出.
}
};
}
/**
* @brief 延時nms
*
* @param nms 要延時的ms數
*/
void DelayPhyMs(uint16_t nms)
{
uint32_t i;
for (i = 0; i < nms; i++)
DelayPhyUs(1000);
}
//頭檔案
/*****************************delay************************************/
void DelayPhyConfig(void);
void DelayPhyUs(uint32_t nus);
void DelayPhyMs(uint16_t nms);
/**********************************************************************/
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534047.html
標籤:其他
