/*************************************
*** 定时器完成
*** 两灯不同频率闪
***************************************/
#include<reg52.h>
#define FOSC (11059200L)
#define WANTTIME (1) //单位MS 进中断的时间
#define VTL ((65536-FOSC*WANTTIME/12000)%256)
#define VTH ((65536-FOSC*WANTTIME/12000)/256)
#define TIMEISUP (1)
#define TIMEISNOTUP (0)
#define TIMERCOUNT (2)
#define TIMER1 (0)
#define TIMER2 (1)
#define LED1DELAYTIME (100)
#define LED2DELAYTIME (300)
sbit Led1 =P0^0;
sbit Led2 =P0^1;
unsigned int Timers[TIMERCOUNT];
void LedEffect();
void Init();
//iTimer是定时器数组的下标
//xTime是要定时的时长,单位ms
unsigned char xDelay(unsigned char iTimer,unsigned int xTime)
{
if(Timers[iTimer]==0)
{
Timers[iTimer]=xTime;
return(TIMEISUP);
}
return(TIMEISNOTUP);
}
void main()
{
Init();
while(1)
{
LedEffect();
}
}
void Init()
{
TMOD=0X01;
TL0=VTL;
TH0=VTH;
ET0=1;
TR0=1;
EA=1;
}
void InitT0() interrupt 1
{
unsigned char i;
TL0=VTL;
TH0=VTH;
for(i=0;i<TIMERCOUNT;i++)
if(Timers[i]>0) Timers[i]--;
}
void LedEffect()
{
if(xDelay(TIMER1,LED1DELAYTIME)==TIMEISUP)
Led1^=1;
if(xDelay(TIMER2,LED2DELAYTIME)==TIMEISUP)
Led2^=1;
}