求单片机高手!编写一个用定时器中断的秒表程序,成功后请把程序源文件和仿真成功的文件发给我,万分感谢 5
KEYVAL EQU 30H
DAT EQU 33H
SCANLED EQU 39H
CLK EQU 77H
SEC EQU 78H
SEC1 EQU 79H
SEC2 EQU 7AH
DOT BIT 00H
ORG 0000H
LJMP MAIN
ORG 0003H
LJMP EXTINT0
ORG 000BH
LJMP T0ISR ;50ms定时
ORG 001BH
LJMP T1ISR ;扫描显示
ORG 0030H
MAIN:
MOV SP,#5FH
MOV TMOD,#11H
MOV TH0,#03CH
MOV TL0,#0B0H
MOV TH1,#0ECH
MOV TL1,#078H
MOV KEYVAL,#2
MOV 33H,#10H
MOV 34H,#10H
MOV 35H,#10H
MOV 36H,#10H
MOV CLK,#0
SETB IT0
SETB EX0
CLR TR0
SETB ET0
MOV R2,#0
SETB TR1
SETB ET1
SETB EA
LOOP:
MOV A,KEYVAL
LOOP1:
JNZ LOOP2 ;启动
SETB TR0
SJMP LOOP
LOOP2:
DEC A
JNZ LOOP3 ;暂停
CLR TR0
SJMP LOOP
LOOP3:
DEC A
JNZ LOOP ;清零
MOV 36H,#0
MOV 35H,#0
MOV 34H,#0
MOV 33H,#0
SJMP LOOP
EXTINT0:
PUSH ACC
INC KEYVAL
MOV A,KEYVAL
CJNE A,#3,EXT00
EXT00:
JC EXT01
MOV KEYVAL,#0
EXT01:
POP ACC
RETI
T0ISR:
PUSH ACC
CLR TR0
MOV TH0,#3CH
MOV TL0,#0B0H
SETB TR0
INC CLK
MOV A,CLK
CJNE A,#2,T0ISRE
MOV CLK,#0
INC 36H
MOV A,36H
CJNE A,#10,T0ISRE
MOV 36H,#0
INC 35H
MOV A,35H
CJNE A,#10,T0ISRE
MOV 35H,#0
INC 34H
MOV A,34H
CJNE A,#6,T0ISRE
MOV 34H,#0
INC 33H
MOV A,33H
CJNE A,#10,T0ISRE
MOV 33H,#0
T0ISRE:
POP ACC
RETI
T1ISR:
PUSH ACC
CLR TR1
MOV TH1,#0ECH
MOV TL1,#78H
SETB TR1
MOV DPTR,#LEDTAB
T100:
MOV R0,#DAT
MOV A,SCANLED
ADD A,R0
MOV R0,A
MOV A,SCANLED
JNZ T101
MOV P2,#01H
SETB DOT
SJMP T1DIS
T101:
DEC A
JNZ T102
MOV P2,#02H
CLR DOT
SJMP T1DIS
T102:
DEC A
JNZ T103
MOV P2,#04H
SETB DOT
SJMP T1DIS
T103:
MOV P2,#08H
CLR DOT
T1DIS:
MOV A,@R0
MOVC A,@A+DPTR
JNB DOT,T1DIS1
ORL A,#01H
T1DIS1:
CPL A
MOV P0,A
INC SCANLED
MOV A,SCANLED
CJNE A,#4,T1END
MOV SCANLED,#0
T1END:
POP ACC
RETI
LEDTAB: DB 0FCH ;"0" 00H
DB 60H ;"1" 01H
DB 0DAH ;"2" 02H
DB 0F2H ;"3" 03H
DB 66H ;"4" 04H
DB 0B6H ;"5" 05H
DB 0BEH ;"6" 06H
DB 0E0H ;"7" 07H
DB 0FEH ;"8" 08H
DB 0F6H ;"9" 09H
DB 0EEH ;"A" 0AH
DB 3EH ;"B" 0BH
DB 9CH ;"C" 0CH
DB 7AH ;"D" 0DH
DB 9EH ;"E" 0EH
DB 8EH ;"F" 0FH
DB 00H ;" " 10H
END
C语言编程
#include
#define uchar unsigned char
uchar ledtab[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xfe,0x3e,0x9c,0x7a,0x9e,0x8e,0x00};
uchar keyval,dat,scanled,clk;
uchar disdat[4];
void ext0(void) interrupt 0
{
keyval++;
keyval%=3;
}
void t0isr(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
clk++;
if(clk>1)
{
clk=0;
disdat[3]++;
if(disdat[3]>9)
{
disdat[3]=0;
disdat[2]++;
if(disdat[2]>9)
{
disdat[2]=0;
disdat[1]++;
if(disdat[1]>9)
{
disdat[1]=0;
disdat[0]++;
if(disdat[0]>9)disdat[0]=0;
}
}
}
}
}
void t1isr(void) interrupt 3
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
P2=0x01<<scanled;
dat=ledtab[disdat[scanled]];
if((scanled==0)||(scanled==2))dat|=0x01;
P0=~dat;
scanled++;
scanled%=4;
}
main()
{
uchar i;
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
keyval=2;
scanled=0;
clk=0;
IT0=1;
EX0=1;
ET0=1;
TR1=1;
ET1=1;
EA=1;
while(1)
{
switch(keyval)
{
case 0:TR0=1;break;
case 1:TR0=0;break;
case 2:for(i=0;i<4;i++)disdat[i]=0;break;
default:break;
}
}
}