;仿照例7.11
;TC=M-T/T计数=9E58H(其中T=25ms)
ORG 0000H
LJMP MAIN
ORG 000BH
AJMP BRT0
ORG 0030H
MAIN:
CLR P1
MOV TMOD,#01H;T0为方式1
SETB EA
SETB ET0
SETB TR0
MOV TH0,#9EH
MOV TL0,#58H
MOV R0,#04H
MOV A,#01H
LOOP:SJMP $;等待中断
ORG 0080H
BRT0:
DJNZ R0,NEXT
MOV P1,A
RL A
MOV R0,#04H
NEXT:MOV TH0,#9EH
MOV TL0,#58H
RETI
END
初始化T0定时50MS,设个计数变量,每定时到了进中断一次+1,加到20次时就是1S,然后更换指示灯点亮,下1S 依次循环点亮。
void main(void)
{
TMOD=0x01; //16位定时模式
TH0=(65536-50000) >> 8;
TL0=(65536-50000) & 255;
TR0=1;
ET0=1;
EA=1;
while(1) ;
}
void timer0() interrupt 1
{
static unsigned char t ,Light = 1;
TF0=0;
if(++t==20)
{
t=0;
//这里加入流水灯程序
P0 = Light;
if(Light > 0x80)Light = 1;
else Light << 1;
}
//重装定时器
TH0=(65536-50000) >> 8;
TL0=(65536-50000) & 255;
}