利用数码管和单片机编写1-2-3-4每秒加一的循环程序
程序与硬件密切相关:
我起初买的简易学习板上汇编程序如下:
其中P 0口接数码管段
P2.0-P2.3是数码管的位控信号
CLR P2.3 就是开通第1个数码管
SETB P2.3 就是关闭第1个数码管
TABLE为共阳数码管的七段码
ORG 0000H
LJMP MAIN;转初始化程序
ORG 000BH;定位中断子程序地址
LJMP INTT0
ORG 0020H
MAIN:MOV A,#0C0H
MOV 30H,A;先清0,30H-33H分别存个、十、百、千位的七段码
MOV 31H,A
MOV 32H,A
MOV 33H,A ;*******10
MOV TH0,#0FH;设置定时器0FH=15,定时50ms
MOV TL0,#00H
MOV TMOD,#01H
SETB ET0;开中断
SETB EA;开总中断
MOV A,#0FFH;先关闭显示
MOV P0,A
MOV R3,#00H
SETB TR0;启动定时器
LOOP:MOV A,30H;*******20
MOV P0,A
CLR P2.3
MOV R2,#0E6H;延时1MS,E6H=230
LP1:NOP
NOP
DJNZ R2,LP1
MOV A,31H
MOV P0,A
SETB P2.3
CLR P2.2 ;*******30
MOV R2,#0E6H;延时1MS,E6H=230
LP2:NOP
NOP
DJNZ R2,LP2
MOV A,32H
MOV P0,A
SETB P2.2
CLR P2.1
MOV R2,#0E6H;延时1MS,E6H=230
LP3:NOP;*******40
NOP
DJNZ R2,LP3
MOV A,33H
MOV P0,A
SETB P2.1
CLR P2.0
MOV R2,#0E6H;延时1MS,E6H=230
LP4:NOP
NOP
DJNZ R2,LP4 ;*******50
SETB P2.0
MOV DPTR,#TABLE
MOV A,R4
MOVC A,@A+DPTR
MOV 30H,A
MOV A,R5
MOVC A,@A+DPTR
MOV 31H,A
MOV A,R6
MOVC A,@A+DPTR
MOV 32H,A
MOV A,R7;*******60
MOVC A,@A+DPTR
MOV 33H,A
CJNE R3,#14H,LOOP
MOV R3,#00H
INC R4
CJNE R4,#0AH,LOOP
MOV R4,#00H
INC R5
CJNE R5,#0AH,LOOP
MOV R5,#00H;*******70
INC R6
CJNE R6,#0AH,LOOP
MOV R6,#00H
INC R7
LJMP LOOP
TABLE:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,88H,83H,0C6H,0A1H,86H,84H
INTT0:MOV TH0,#0FH;0FH=15,重装计数初值
MOV TL0,#00H
INC R3
RETI
END