#include <reg52.h>
#define uint unsigned int
#define uchar unsigend char
sbit led0=P2^1;
sbit led1=P2^2;
void delay(uint);
void time0()
{
TMOD|=0x01; //选择定时器0,工作方式1
TH0=0xfc; //赋初值,定时1ms
TL0=0x18;
ET0=1; //打开定时器0中断允许
EA=1; //打开总中断
TR0=1; //打开定时器0
}
void time1()
{
TMOD|=0x10; //选择定时器1,工作方式1
TH1=0xfc; //赋初值,定时1ms
TL1=0x18;
ET1=1; //打开定时器1中断允许
EA=1; //打开总中断
TR1=1; //打开定时器1
}
void same()
{
uint i,j;
delay(500);
if(i==200&&j==200)
{
i=0;
j=0;
}
}
void main()
{
time0();
time1();
while(1)
{
same();
}
}
void timer0()interrupt 1
{
uint i;
TH0=0xfc;
TL0=0x18;
i++;
if(i==200)
{
i=0;
led0=~led0;
}
}
void timer1()interrupt 3
{
uint j;
TH1=0xfc;
TL1=0x18;
j++;
if(j==500) //未定
{
j=0;
led1=~led1;
}
}
void delay(uint a)
{
uint b,c;
for(b=a;b>0;b--)
for(c=110;c>0;c--);
} |