#include<reg51.h>
#define u8 unsigned int
#define u16 unsigned char
u8 all,tin,u,b;
void Timer1Init()
{
TMOD|=0X10;//選擇為定時器1模式,工作方式1,僅用TR1打開啟動。
TH1 = 0xFF;
TL1 = 0xff; //1us
ET1=1;//打開定時器1中斷允許
EA=1;//打開總中斷
TR1=1;//打開定時器
}
void main()
{
Timer1Init();
while(1)
{
if(tin>100)
{
tin=0;
if(b)
{
u++;
}
else
{
u--;
}
}
if(u==1000)
{
b=0;
}
if(u==0)
{
b=1;
}
if(all>1000)
{
all=0;
}
if(u<all)
{
P1=0xff;
}
else
{
P1=0x00;
}
}
}
void Time1() interrupt 3
{
TH1 = 0xFF;
TL1 = 0xff; //1us
all++;
tin++;
} |