讨论贴——求大神跟我讲解一下系统自带延时程序的原理
最近在写程序的时候,有一个很大的疑惑:atmega8自带的_delay_ms()函数与我自己写的Delay_ms()函数有什么区别?我自己用T0计时器写了一个延时程序如下,T0的计数频率为1Mhz:
void Delay_ms(uint16_t w)
{
uint16_t akt;
akt = SetDelay(w);
while(!CheckDelay(akt));
}
char CheckDelay (uint16_t t)
{
return(((t - CountMilliseconds) & 0x8000) >> 8);
}
uint16_t SetDelay(uint16_t t)
{
return(CountMilliseconds + t - 1);
}
//T0计时器溢出中断程序
SIGNAL(SIG_OVERFLOW0)
{
static uint8_t cnt = 0;
//每溢出4次为1ms
if(!cnt--)
{
cnt = 3;
CountMilliseconds += 1;
}
}
页:
[1]