倒计时汇编程序:
ORG 0000H
JMP BEGIN
ORG 000BH
LJMP T0INT
ORG 001BH
LJMP T1INT
LEDD EQU 30H
LEDC EQU 36H
SECC EQU 37H
MINC EQU 38H
HOUR EQU 39H
TCNT EQU 3AH
ORG 0030H
;---------------------------------------
TABLE: ; 共阴极数码管显示代码表
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
; DB 3FH,06H,5BH,4FH,66H ;12345
; DB 6DH,7DH,07H,7FH, ;6789
;---------------------------------------
DELAY:
MOV R6,#25
DLY01:
MOV R7,#100
DJNZ R7,$
DJNZ R6,DLY01
RET
;---------------------------------------
BEGIN:
MOV SP,#5FH
MOV TMOD,#11H
MOV TH0,#3CH
MOV TL0,#0B0H
MOV TH1,#0F4H
MOV TL1,#48H
MOV LEDC,#6
MOV R0,#LEDD
CLR A
INIT:
MOV @R0,A
INC R0
DJNZ LEDC,INIT
SETB ET1
SETB TR1
MOV P2,#0
MOV TCNT,#0
MOV HOUR,#0
MOV MINC,#0
MOV SECC,#0
SETB ET0
SETB EA
LOOP:
JB P1.0,LOOP1
LCALL DELAY
JB P1.0,LOOP1
JNB P1.0,$
INC HOUR
MOV A,HOUR
CJNE A,#24,LOOPH
LOOPH:
JC LOOPH1
MOV HOUR,#0
LOOPH1:
LCALL DISHOUR
SJMP LOOP
LOOP1:
JB P1.1,LOOP2
LCALL DELAY
JB P1.1,LOOP2
JNB P1.1,$
MOV A,HOUR
JNZ LOOPH2
MOV HOUR,#23
SJMP LOOPH3
LOOPH2:
DEC HOUR
LOOPH3:
LCALL DISHOUR
SJMP LOOP
LOOP2:
JB P1.2,LOOP3
LCALL DELAY
JB P1.2,LOOP3
JNB P1.2,$
INC MINC
MOV A,MINC
CJNE A,#60,LOOPM
LOOPM:
JC LOOPM1
MOV MINC,#0
LOOPM1:
LCALL DISMIN
SJMP LOOP
LOOP3:
JB P1.3,LOOP4
LCALL DELAY
JB P1.3,LOOP4
JNB P1.3,$
MOV A,MINC
JNZ LOOPM2
MOV MINC,#59
SJMP LOOPM3
LOOPM2:
DEC MINC
LOOPM3:
LCALL DISMIN
SJMP LOOP
LOOP4:
JB P1.4,LOOP5
LCALL DELAY
JB P1.4,LOOP5
JNB P1.4,$
INC SECC
MOV A,SECC
CJNE A,#60,LOOPS
LOOPS:
JC LOOPS1
MOV SECC,#0
LOOPS1:
LCALL DISSEC
LJMP LOOP
LOOP5:
JB P1.5,LOOP6
LCALL DELAY
JB P1.5,LOOP6
JNB P1.5,$
MOV A,SECC
JNZ LOOPS2
MOV SECC,#59
SJMP LOOPS3
LOOPS2:
DEC SECC
LOOPS3:
LCALL DISSEC
LJMP LOOP
LOOP6:
JB P3.1,LOOP7
LCALL DELAY
JB P3.1,LOOP7
JNB P3.1,$
CPL TR0
LOOP7:
LJMP LOOP
;---------------------------------------
T1INT:
PUSH ACC
CLR TR1
MOV TH1,#0F4H
MOV TL1,#48H
SETB TR1
MOV DPTR,#TABLE
MOV R0,#LEDD
MOV A,LEDC
T1INT0:
JNZ T1INT1
MOV P2,#01H
MOV A,@R0
MOVC A,@A+DPTR
CPL A
MOV P0,A
SJMP T1INTD
T1INT1:
DEC A
JNZ T1INT2
MOV P2,#02H
INC R0
MOV A,@R0
MOVC A,@A+DPTR
CPL A
MOV P0,A
SJMP T1INTD
T1INT2:
DEC A
JNZ T1INT3
MOV P2,#04H
INC R0
INC R0
MOV A,@R0
MOVC A,@A+DPTR
CPL A
MOV P0,A
SJMP T1INTD
T1INT3:
DEC A
JNZ T1INT4
MOV P2,#08H
INC R0
INC R0
INC R0
MOV A,@R0
MOVC A,@A+DPTR
CPL A
MOV P0,A
SJMP T1INTD
T1INT4:
DEC A
JNZ T1INT5
MOV P2,#10H
INC R0
INC R0
INC R0
INC R0
MOV A,@R0
MOVC A,@A+DPTR
CPL A
MOV P0,A
SJMP T1INTD
T1INT5:
DEC A
JNZ T1INTD
MOV P2,#20H
INC R0
INC R0
INC R0
INC R0
INC R0
MOV A,@R0
MOVC A,@A+DPTR
CPL A
MOV P0,A
T1INTD:
INC LEDC
MOV A,LEDC
CJNE A,#6,T1INT6
T1INT6:
JC T1INTE
MOV LEDC,#0
T1INTE:
POP ACC
RETI
;---------------------------------------
HBCD:
MOV B,#10
DIV AB
SWAP A
ORL A,B
RET
;---------------------------------------
T0INT:
PUSH ACC
CLR TR0
MOV TH0,#3CH
MOV TL0,#0B0H
SETB TR0
INC TCNT
MOV A,TCNT
CJNE A,#20,T0INT1
T0INT1:
JC T0INTE
MOV TCNT,#0
MOV A,SECC
ORL A,MINC
ORL A,HOUR
JNZ T0INT2
SETB P3.0
CLR TR0
POP ACC
RETI
T0INT2:
MOV A,MINC
ORL A,HOUR
JNZ T0INT3
MOV A,SECC
CJNE A,#6,T0INT21
T0INT21:
JNC T0INT22
CPL P3.0
T0INT22:
DEC SECC
LCALL DISSEC
SJMP T0INTE
T0INT3:
MOV A,SECC
JZ T0INT31
DEC SECC
LCALL DISSEC
SJMP T0INTE
T0INT31:
MOV SECC,#59
LCALL DISSEC
MOV A,MINC
JZ T0INT32
DEC MINC
LCALL DISMIN
SJMP T0INTE
T0INT32:
MOV MINC,#59
LCALL DISMIN
DEC HOUR
LCALL DISHOUR
T0INTE:
POP ACC
RETI
;---------------------------------------
DISSEC:
MOV A,SECC
LCALL HBCD
PUSH ACC
ANL A,#0FH
MOV 30H,A
POP ACC
SWAP A
ANL A,#0FH
MOV 31H,A
RET
;---------------------------------------
DISMIN:
MOV A,MINC
LCALL HBCD
PUSH ACC
ANL A,#0FH
MOV 32H,A
POP ACC
SWAP A
ANL A,#0FH
MOV 33H,A
RET
;---------------------------------------
DISHOUR:
MOV A,HOUR
LCALL HBCD
PUSH ACC
ANL A,#0FH
MOV 34H,A
POP ACC
SWAP A
ANL A,#0FH
MOV 35H,A
RET
;---------------------------------------
END
;-----------------------------------
; 交通灯控制器实例
;功能:使用6只LED模拟交通灯控制器
;-----------------------------------
SOUTH_RED EQU P1.0
SOUTH_YELLOW EQU P1.1
SOUTH_GREEN EQU P1.2
EAST_RED EQU P1.3
EAST_YELLOW EQU P1.4
EAST_GREEN EQU P1.5
SECOND_FLAG BIT 00H
TIME_COUNT0 DATA 30H
TIME_COUNT1 DATA 31H
STATUS_FLAG DATA 32H
SECOND_COUNT DATA 33H
ORG 0000H ;伪指令,指定程序从0000H开始存放
LJMP MAIN ;跳转指令,程序跳转到MAIN处
ORG 000BH ;伪指令,指定程序从T0入口地址000BH开始存放
LJMP T0_INT ;跳转指令,程序跳转到T0_INT处
ORG 0100H
MAIN:
MOV SP,#60H ;给堆栈指针赋初值
LCALL INIT ;调用状态初始化子程序
LCALL T0_INIT ;调用定时器中断初始化子程序
LOOP:
JNB SECOND_FLAG,LOOP ;根据SECOND_FLAG值判断0.5秒是否到
LCALL STATUS_CHANGE ;每0.5秒调用交通灯状态转换子程序
CLR SECOND_FLAG ;清零SECOND_FLAG
SJMP LOOP ;跳转,程序继续
STATUS_CHANGE:
;---------------------------------
; 交通灯状态转换子程序
;---------------------------------
MOV A,STATUS_FLAG ;根据STATUS_FLAG值进行散转
ADD A,STATUS_FLAG
MOV DPTR,#STATUS_PROC_TABLE
JMP @A+DPTR
STATUS_PROC_TABLE:
AJMP STATUS1
AJMP STATUS2
AJMP STATUS3
AJMP STATUS4
AJMP STATUS5
AJMP STATUS6
STATUS1:
;---------------------------------
; 散转子程序1
;状态1:南北绿灯,东西红灯,持续20秒
;然后转到状态2
;---------------------------------
DJNZ SECOND_COUNT,STATUS1_EXIT
MOV SECOND_COUNT,#6 ;20秒定时到,转换到状态2
MOV STATUS_FLAG,#01H
STATUS1_EXIT:
RET
STATUS2:
;---------------------------------
; 散转子程序2
;状态2:南北绿灯闪,东西红灯,持续3秒
;然后转到状态3
;---------------------------------
DJNZ SECOND_COUNT,STATUS2_EXIT
MOV SECOND_COUNT,#4 ;3秒定时到,转换到状态3
MOV STATUS_FLAG,#02H
SETB SOUTH_RED ;南北红灯灭
CLR SOUTH_YELLOW ;南北黄灯亮
SETB SOUTH_GREEN ;南北绿灯灭
CLR EAST_RED ;东西红灯亮
SETB EAST_YELLOW ;东西黄灯灭
SETB EAST_GREEN ;东西绿灯灭
RET
STATUS2_EXIT:
CPL SOUTH_GREEN ;南北绿灯闪
RET
STATUS3:
;---------------------------------
; 散转子程序3
;状态3:南北黄灯,东西红灯 ,持续2秒
;然后转到状态4
;---------------------------------
DJNZ SECOND_COUNT,STATUS3_EXIT
MOV SECOND_COUNT,#40 ;2秒定时到,转换到状态4
MOV STATUS_FLAG,#03H
CLR SOUTH_RED ;南北红灯亮
SETB SOUTH_YELLOW ;南北黄灯灭
SETB SOUTH_GREEN ;南北绿灯灭
SETB EAST_RED ;东西红灯灭
SETB EAST_YELLOW ;东西黄灯灭
CLR EAST_GREEN ;东西绿灯亮
STATUS3_EXIT:
RET
STATUS4:
;---------------------------------
; 散转子程序4
;状态4:南北红灯,东西绿灯,持续20秒
;然后转到状态5
;---------------------------------
DJNZ SECOND_COUNT,STATUS4_EXIT
MOV SECOND_COUNT,#6 ;20秒定时到,转换到状态5
MOV STATUS_FLAG,#04H
STATUS4_EXIT:
RET
STATUS5:
;---------------------------------
; 散转子程序5
;状态5:南北红灯,东西绿灯闪,持续3秒
;然后转到状态6
;---------------------------------
DJNZ SECOND_COUNT,STATUS5_EXIT
MOV SECOND_COUNT,#4 ;3秒定时到,转换到状态6
MOV STATUS_FLAG,#05H
CLR SOUTH_RED ;南北红灯亮
SETB SOUTH_YELLOW ;南北黄灯灭
SETB SOUTH_GREEN ;南北绿灯灭
SETB EAST_RED ;东西红灯灭
CLR EAST_YELLOW ;东西黄灯亮
SETB EAST_GREEN ;东西绿灯灭
RET
STATUS5_EXIT:
CPL EAST_GREEN ;东西绿灯闪
RET
STATUS6:
;---------------------------------
; 散转子程序6
;状态6:南北红灯,东西黄灯 ,持续2秒
;然后转到状态1
;---------------------------------
DJNZ SECOND_COUNT,STATUS6_EXIT
MOV SECOND_COUNT,#40 ;2秒定时到,转换到状态1
MOV STATUS_FLAG,#00H
SETB SOUTH_RED ;南北红灯灭
SETB SOUTH_YELLOW ;南北黄灯灭
CLR SOUTH_GREEN ;南北绿灯亮
CLR EAST_RED ;东西红灯亮
SETB EAST_YELLOW ;东西黄灯灭
SETB EAST_GREEN ;东西绿灯灭
STATUS6_EXIT:
RET
INIT:
;---------------------------------
; 状态初始化子程序
;---------------------------------
SETB SOUTH_RED ;南北红灯灭
SETB SOUTH_YELLOW ;南北黄灯灭
CLR SOUTH_GREEN ;南北绿灯亮
CLR EAST_RED ;东西红灯亮
SETB EAST_YELLOW ;东西黄灯灭
SETB EAST_GREEN ;东西绿灯灭
MOV TIME_COUNT0,#250 ;变量TIME_COUNT0赋初值250
MOV TIME_COUNT1,#8 ;变量TIME_COUNT1赋初值8
MOV SECOND_COUNT,#40 ;变量SECOND_COUNT赋初值40
MOV STATUS_FLAG,#00H ;变量STATUS_FLAG赋初值00H
CLR SECOND_FLAG ;清零位变量SECOND_FLAG
RET
T0_INT:
;---------------------------------
; T0中断服务程序
;每0.5秒置一SECOND_FLAG
;---------------------------------
DJNZ TIME_COUNT0,T0_INT_EXIT
MOV TIME_COUNT0,#250
DJNZ TIME_COUNT1,T0_INT_EXIT
MOV TIME_COUNT1,#8
SETB SECOND_FLAG ;定时0.5秒到,置一SECOND_FLAG
T0_INT_EXIT:
RETI
T0_INIT:
;---------------------------------
; 定时器T0的初始化
;---------------------------------
MOV TMOD,#00000010B ;定时器T0工作在方式2
MOV TL0,#06H ;设定定时器T0的初始值
MOV TH0,#06H ;设定定时器T0的自动重装入值
MOV TCON,#00010000B ;定时器T0使能
SETB EA ;中断允许总控制位使能
SETB ET0 ;T0中断使能
RET
END
试了,是正确的!
数码管倒计时的汇编程序:
NUM DATA 10H
ORG 0000H
START:
MOV NUM,#9H
MOV R1,#255H
MOV R2,#250H
LOOP:
MOV A,NUM
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P1,A
LCALL DELAY
DJNZ NUM,LOOP
LJMP START
DELAY:
DJNZ R1,DELAY
DELAY_1:
MOV R1,#255H
DELAY_2:
MOV R3,#25H
DJNZ R3,$
DJNZ R2,DELAY_2
DJNZ R1,DELAY_2
RET
TABLE:
DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH
END
只有一位,不过你可以加几位,也不难。
急求,谢谢~ 问题补充:程序跟仿真图要对上 这个是我以前单片机课程设计上我做的程序,希望对当时我们采用的只是二极管p7~p0 东西交通灯我采用
邮箱贴出来,给你发过去,不过两个都是C程序,要的话发给你。