huangning 发表于 2010-2-1 16:45:07

V0.41 电调定时器程序中,延时是怎么回事?请教高人解救。。。

V0.41 电调定时器程序中,延时是怎么回事?请教高人解救。。。



#include "main.h"
volatile unsigned int CountMilliseconds = 0;//毫秒计数
volatile unsigned char Timer0Overflow;

enum {
STOP             = 0,
CK               = 1,
CK8            = 2,
CK64             = 3,
CK256            = 4,
CK1024         = 5,
T0_FALLING_EDGE= 6,
T0_RISING_EDGE   = 7
};


SIGNAL(SIG_OVERFLOW0)
{
static unsigned char cnt;
Timer0Overflow++;
if(!cnt--)
{
   cnt = 3;
   CountMilliseconds += 1;
   if(I2C_Timeout) I2C_Timeout--;
   if(PPM_Timeout) PPM_Timeout--; else anz_ppm_werte = 0;
   if(SIO_Timeout) SIO_Timeout--;
}
}
void Timer0_Init(void)
{
TCCR0= TIMER_TEILER;
// TCNT0 = -TIMER_RELOAD_VALUE;// reload
TIM0_START;
TIMER2_INT_ENABLE;
}


unsigned int SetDelay(unsigned int t)
{
return(CountMilliseconds + t - 1);                                             
}

char CheckDelay (unsigned int t)
{
return(((t - CountMilliseconds) & 0x8000) >> 8);          这部分起到什么作用?
}

void Delay_ms(unsigned int w)
{
unsigned int akt;
akt = SetDelay(w);
while (!CheckDelay(akt));                            这又是说明了什么呢?
}

bjf98 发表于 2010-3-19 10:10:01

同问,希望高手能指点

rei1984 发表于 2010-3-19 11:29:22

char CheckDelay (unsigned int t)
{
return(((t - CountMilliseconds) & 0x8000) >> 8);          这部分起到什么作用?
}

//这里最高为变成1的时候,就说明时间到了。正数的最高为为0,复苏最高位为1.


void Delay_ms(unsigned int w)
{
unsigned int akt;
akt = SetDelay(w);
while (!CheckDelay(akt));                            这又是说明了什么呢?
}

//也简单就是 卡 while, 等在 时间的过去!
页: [1]
查看完整版本: V0.41 电调定时器程序中,延时是怎么回事?请教高人解救。。。