你的浏览器版本过低,可能导致网站不能正常访问!为了你能正常使用网站功能,请使用这些浏览器。
举报
查看全部评分
Tcreat 发表于 2018-3-10 10:08 用系统滴答也可以 INT16U g_uwDelayTime_ms = 0;
__nop();指令最精确
但是做一个可以改变延时的函数就不精确了。
评分
查看全部评分
评分
查看全部评分
INT16U g_uwDelayTime_ms = 0;
static INT8U g_ubMultiple_us = 0;
static INT16U g_uwMultiple_ms = 0;
void Delay(INT16U mTime)
{
g_uwDelayTime_ms = mTime;
while(g_uwDelayTime_ms != 0);
}
void Delay_Init(INT8U sysclk)
{
SysTick->CTRL &= 0xFFFFFFFB; // Ñ¡ÔñTickʱÖÓÔ´¡ª¡ªHCLK/8
g_ubMultiple_us = sysclk / 8;
g_uwMultiple_ms = (INT16U)sysclk / 8 * 1000;
//SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; // Ñ¡ÔñTickʱÖÓÔ´¡ª¡ªÏµÍ³Ê±ÖÓ
//g_ubMultiple_us = sysclk;
//g_uwMultiple_ms = (INT16U)sysclk * 1000;
}
// ΢Ã붨ʱ
void Delay_us(INT16U time)
{
INT32U ulTemp = 0;
ulTemp = (INT32U)time * g_ubMultiple_us;
SysTick->LOAD = ulTemp - 1;
SysTick->VAL = 0; // ¸³Öµ¿ÉÇå³þSysTick_CTRL_COUNTFLAG_Msk λ
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Æô¶¯¼ÆʱÆ÷
do
{
ulTemp = SysTick->CTRL;
}
while((ulTemp & SysTick_CTRL_COUNTFLAG_Msk) != SysTick_CTRL_COUNTFLAG_Msk); // µÈµ½¼ÆÊýÍê³É
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // Í£Ö¹¼ÆʱÆ÷
SysTick->VAL = 0;
}
// ºÁÃ붨ʱ
void Delay_ms(INT16U time)
{
INT32U ulTemp = 0;
ulTemp = (INT32U)time * g_uwMultiple_ms;
SysTick->LOAD = ulTemp - 1;
SysTick->VAL = 0; // ¸³Öµ¿ÉÇå³þSysTick_CTRL_COUNTFLAG_Msk λ
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Æô¶¯¼ÆʱÆ÷
do
{
ulTemp = SysTick->CTRL;
}
while((ulTemp & SysTick_CTRL_COUNTFLAG_Msk) != SysTick_CTRL_COUNTFLAG_Msk); // µÈµ½¼ÆÊýÍê³É
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // Í£Ö¹¼ÆʱÆ÷
SysTick->VAL = 0;
}
谢了,就是要滴答时钟
评分
查看全部评分
要更精准的话,用定时器查询方式。
评分
查看全部评分