功能要求:
一):绿灯25s倒计时,绿灯过度红灯有5s黄灯时间,红灯25s后直接跳绿灯。
二):按键按下模拟闯红灯输入,产生5s蜂鸣器鸣叫。
开发环境:
软件:Keil uVision4
硬件:HOT51增强型单片机开发板
程序代码:
ORG 0000H
AJMP MAIN
ORG 0003H
LJMP INTER
ORG 000BH
LJMP TIMER1
ORG 0030H
;主函数开始
MAIN: MOV R7,#00H
DJNZ R7,$
MOV SP,#60H
MOV TMOD,#01H
MOV TH0,#3CH ;定时器定时时间为50ms
MOV TL0,#0AFH
MOV R6,#20 ;定时器计数20*50ms=1s
SETB ET0
SETB EX0
SETB EA
SETB TR0
;--------------------------------------------------------------;
ST1: MOV R5,#25 ;主道路亮绿灯25s,支道路亮红灯25s
DIS1: MOV A,R5
JNZ NEXT1 ;若5s已经减为0则跳下一状态ST3
AJMP ST2
NEXT1: MOV A,#25
CLR C
SUBB A,R5 ;相减得到已经过去的时间
MOV B,A
MOV A,#07H ;得到查表跳转偏移量
MUL AB
MOV DPTR,#TAB1
ADD A,DPL ;加上偏移量
MOV DPL,A
MOV A,#00H
ADDC A,DPH
MOV DPH,A
MOV A,R4
JZ NOSP1 ;若鸣叫时间不为0则鸣叫(取反p1.2)否则不鸣叫
CPL P1.2
NOSP1: MOV A,#00H ;动态显示,循环点亮数码管
MOV P0,#00H
MOV P2,#0FFH
MOVC A,@A+DPTR
MOV P0,A ;显示支路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F6H
MOVC A,@A+DPTR
MOV P0,A ;显示支路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F5H
MOVC A,@A+DPTR
MOV P0,A ;显示主路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F4H
MOVC A,@A+DPTR
MOV P0,A ;显示主路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F2H
MOVC A,@A+DPTR
MOV P0,A ;以下3个输出显示主支亮灯状态
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F1H
MOVC A,@A+DPTR
MOV P0,A
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F0H
MOVC A,@A+DPTR
MOV P0,A
MOV P0,#00H
MOV P2,#0FFH
AJMP DIS1 ;返回起始循环点亮数码管
TAB1: DB 3FH,4FH,6DH,5BH,08H,00H,01H ;25
DB 6FH,5BH,66H,5BH,08H,00H,01H ;24
DB 7FH,5BH,4FH,5BH,08H,00H,01H ;23
DB 07H,5BH,5BH,5BH,08H,00H,01H ;22
DB 7DH,5BH,06H,5BH,08H,00H,01H ;21
DB 6DH,5BH,3FH,5BH,08H,00H,01H ;20
DB 66H,5BH,6FH,06H,08H,00H,01H ;19
DB 4FH,5BH,7FH,06H,08H,00H,01H ;18
DB 5BH,5BH,07H,06H,08H,00H,01H ;17
DB 06H,5BH,7DH,06H,08H,00H,01H ;16
DB 3FH,5BH,6DH,06H,08H,00H,01H ;15
DB 6FH,06H,66H,06H,08H,00H,01H ;14
DB 7FH,06H,4FH,06H,08H,00H,01H ;13
DB 07H,06H,5BH,06H,08H,00H,01H ;12
DB 7DH,06H,06H,06H,08H,00H,01H ;11
DB 6DH,06H,3FH,06H,08H,00H,01H ;10
DB 66H,06H,6FH,3FH,08H,00H,01H ;09
DB 4FH,06H,7FH,3FH,08H,00H,01H ;08
DB 5BH,06H,07H,3FH,08H,00H,01H ;07
DB 06H,06H,7DH,3FH,08H,00H,01H ;06
DB 3FH,06H,6DH,3FH,08H,00H,01H ;05
DB 6FH,3FH,66H,3FH,08H,00H,01H ;04
DB 7FH,3FH,4FH,3FH,08H,00H,01H ;03
DB 07H,3FH,5BH,3FH,08H,00H,01H ;02
DB 7DH,3FH,06H,3FH,08H,00H,01H ;01
;--------------------------------------------------------------;
ST2: MOV R5,#5 ;主道路亮黄灯5s,支道路亮红灯5s
DIS2: MOV A,R5
JNZ NEXT2 ;若5s已经减为0则跳下一状态ST3
AJMP ST3
NEXT2: MOV A,#5
CLR C
SUBB A,R5 ;相减得到已经过去的时间
MOV B,A
MOV A,#07H ;得到查表跳转偏移量
MUL AB
MOV DPTR,#TAB2
ADD A,DPL ;加上偏移量
MOV DPL,A
MOV A,#00H
ADDC A,DPH
MOV DPH,A
MOV A,R4
JZ NOSP2 ;若鸣叫时间不为0则鸣叫(取反p1.2)否则不鸣叫
CPL P1.2
NOSP2: MOV A,#00H ;动态显示,循环点亮数码管
MOV P0,#00H
MOV P2,#0FFH
MOVC A,@A+DPTR
MOV P0,A ;显示支路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F6H
MOVC A,@A+DPTR
MOV P0,A ;显示支路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F5H
MOVC A,@A+DPTR
MOV P0,A ;显示主路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F4H
MOVC A,@A+DPTR
MOV P0,A ;显示主路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F2H
MOVC A,@A+DPTR
MOV P0,A ;以下3个输出显示主支亮灯状态
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F1H
MOVC A,@A+DPTR
MOV P0,A
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F0H
MOVC A,@A+DPTR
MOV P0,A
MOV P0,#00H
MOV P2,#0FFH
AJMP DIS2 ;返回起始循环点亮数码管
TAB2: DB 6DH,3FH,6DH,3FH,08H,01H,00H ;05
DB 66H,3FH,66H,3FH,08H,01H,00H ;04
DB 4FH,3FH,4FH,3FH,08H,01H,00H ;03
DB 5BH,3FH,5BH,3FH,08H,01H,00H ;02
DB 06H,3FH,06H,3FH,08H,01H,00H ;01
;--------------------------------------------------------------;
ST3: MOV R5,#25 ;支道路亮绿灯25s,主道路亮红灯25s
DIS3: MOV A,R5
JNZ NEXT3 ;若25s已经减为0则跳下一状态ST4
AJMP ST4
NEXT3: MOV A,#25
CLR C
SUBB A,R5 ;相减得到已经过去的时间
MOV B,A
MOV A,#07H ;得到查表跳转偏移量
MUL AB
MOV DPTR,#TAB3
ADD A,DPL ;加上偏移量
MOV DPL,A
MOV A,#00H
ADDC A,DPH
MOV DPH,A
MOV A,R4
JZ NOSP3 ;若鸣叫时间不为0则鸣叫(取反p1.2)否则不鸣叫
CPL P1.2
NOSP3: MOV A,#00H ;动态显示,循环点亮数码管
MOV P0,#00H
MOV P2,#0FFH
MOVC A,@A+DPTR
MOV P0,A ;显示支路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F6H
MOVC A,@A+DPTR
MOV P0,A ;显示支路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F5H
MOVC A,@A+DPTR
MOV P0,A ;显示主路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F4H
MOVC A,@A+DPTR
MOV P0,A ;显示主路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F2H
MOVC A,@A+DPTR
MOV P0,A ;以下3个输出显示主支亮灯状态
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F1H
MOVC A,@A+DPTR
MOV P0,A
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F0H
MOVC A,@A+DPTR
MOV P0,A
MOV P0,#00H
MOV P2,#0FFH
AJMP DIS3 ;返回起始循环点亮数码管
TAB3: DB 6DH,5BH,3FH,4FH,01H,00H,08H ;25
DB 66H,5BH,6FH,5BH,01H,00H,08H ;24
DB 4FH,5BH,7FH,5BH,01H,00H,08H ;23
DB 5BH,5BH,07H,5BH,01H,00H,08H ;22
DB 06H,5BH,7DH,5BH,01H,00H,08H ;21
DB 3FH,5BH,6DH,5BH,01H,00H,08H ;20
DB 6FH,06H,66H,5BH,01H,00H,08H ;19
DB 7FH,06H,4FH,5BH,01H,00H,08H ;18
DB 07H,06H,5BH,5BH,01H,00H,08H ;17
DB 7DH,06H,06H,5BH,01H,00H,08H ;16
DB 6DH,06H,3FH,5BH,01H,00H,08H ;15
DB 66H,06H,6FH,06H,01H,00H,08H ;14
DB 4FH,06H,7FH,06H,01H,00H,08H ;13
DB 5BH,06H,07H,06H,01H,00H,08H ;12
DB 06H,06H,7DH,06H,01H,00H,08H ;11
DB 3FH,06H,6DH,06H,01H,00H,08H ;10
DB 6FH,3FH,66H,06H,01H,00H,08H ;09
DB 7FH,3FH,4FH,06H,01H,00H,08H ;08
DB 07H,3FH,5BH,06H,01H,00H,08H ;07
DB 7DH,3FH,06H,06H,01H,00H,08H ;06
DB 6DH,3FH,3FH,06H,01H,00H,08H ;05
DB 66H,3FH,6FH,3FH,01H,00H,08H ;04
DB 4FH,3FH,7FH,3FH,01H,00H,08H ;03
DB 5BH,3FH,07H,3FH,01H,00H,08H ;02
DB 06H,3FH,7DH,3FH,01H,00H,08H ;01
;--------------------------------------------------------------;
ST4: MOV R5,#5 ;主道路亮黄灯5s,支道路亮红灯5s
DIS4: MOV A,R5
JNZ NEXT4 ;若5s已经减为0则跳下一状态ST3
AJMP ST1
NEXT4: MOV A,#5
CLR C
SUBB A,R5 ;相减得到已经过去的时间
MOV B,A
MOV A,#07H ;得到查表跳转偏移量
MUL AB
MOV DPTR,#TAB4
ADD A,DPL ;加上偏移量
MOV DPL,A
MOV A,#00H
ADDC A,DPH
MOV DPH,A
MOV A,R4
JZ NOSP4 ;若鸣叫时间不为0则鸣叫(取反p1.2)否则不鸣叫
CPL P1.2
NOSP4: MOV A,#00H ;动态显示,循环点亮数码管
MOV P0,#00H
MOV P2,#0FFH
MOVC A,@A+DPTR
MOV P0,A ;显示支路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F6H
MOVC A,@A+DPTR
MOV P0,A ;显示支路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F5H
MOVC A,@A+DPTR
MOV P0,A ;显示主路个位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F4H
MOVC A,@A+DPTR
MOV P0,A ;显示主路十位
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F2H
MOVC A,@A+DPTR
MOV P0,A ;以下3个输出显示主支亮灯状态
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F1H
MOVC A,@A+DPTR
MOV P0,A
INC DPTR
MOV A,#00H
MOV P0,#00H
MOV P2,#0F0H
MOVC A,@A+DPTR
MOV P0,A
MOV P0,#00H
MOV P2,#0FFH
AJMP DIS4 ;返回起始循环点亮数码管
TAB4: DB 6DH,3FH,6DH,3FH,01H,08H,00H ;05
DB 66H,3FH,66H,3FH,01H,08H,00H ;04
DB 4FH,3FH,4FH,3FH,01H,08H,00H ;03
DB 5BH,3FH,5BH,3FH,01H,08H,00H ;02
DB 06H,3FH,06H,3FH,01H,08H,00H ;01
;--------------------------------------------------------------;
;定时器中断服务程序
TIMER1: MOV TH0,#3CH
MOV TL0,#10H
DJNZ R6,BACK
DEC R5
MOV R6,#20
MOV A,R4
JZ BACK
DEC R4
BACK: RETI
;闯红灯外部中断服务程序
INTER: MOV R4,#5 ;外部模拟闯红灯输入,蜂鸣器鸣叫5s
RETI
END