#include "reg52.h"
sbit LedHuxi1=P1^0;
#define time (65536-1000) // 单次定时1ms
unsigned char T_High, T, ti, dir;
void InitTimer0(void)
{
TMOD=0x01;
TL0 = time; //initial timer1 low byte
TH0 = time >> 8; //initial timer1 high byte
TR0 = 1; //timer1 start running
ET0 = 1; //enable timer1 interrupt
EA = 1; //open global interrupt switch
}
void Delay(void)
{
unsigned char i,j;
for(i=0;i<20;i++)
for(j=0;j<255;j++)
;
}
void main(void)
{
T_High=0; //高电平
T=30; //周期
ti=0;
dir=0;
InitTimer0();
while(1){
Delay();
if((T_High<T)&&(dir==0))
T_High++;
else if(T_High==T) {
dir=1;
T_High--;
}
else if((T_High>0)&&(dir==1))
T_High--;
else
dir=0;
}
}
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1
{
TL0 = time;
TH0 = time >> 8;
if(ti<T_High){
LedHuxi1=1;
ti++;
}else if(ti<T){
LedHuxi1=0;
ti++;
}else{
ti=0;
}
}