单片机带应急的交通灯控制实验程序_单片机交通灯控制程序
单片机带应急的交通灯控制实验程序由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“单片机交通灯控制程序”。
单片机带应急的交通灯控制实验(汇编)
org 00h ajmp main org 0003h ajmp int_
red_we equ p1.1 yel_we equ p1.2 gre_we equ p1.3 red_sn equ p1.5 yel_sn equ p1.6 gre_sn equ p1.7 org 0030h main: mov sp ,#65h call init_int clr red_we clr red_sn call delay3 loop: clr gre_we setb red_we call delay5
setb gre_we clr yel_we mov r4,#10 sight:call delay_200ms cpl yel_we djnz r4,sight setb yel_we
call delay_200ms clr gre_sn setb red_sn clr red_we call delay5 setb gre_sn
clr yel_sn mov r4,#10 sight1:
call delay_200ms cpl yel_sn djnz r4,sight1 setb yel_sn
clr red_sn clr red_we call delay_200ms sjmp loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;中断服务程序;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;int_: push psw push 04h setb gre_sn setb yel_sn setb gre_we setb yel_we clr red_sn clr red_we call delay5 call init_int pop 04h pop psw reti;;;;;;;;;;;;;;;;;;;;;;;;;;外部中断初始化
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;init_int: setb ex0 setb it0 setb ea ret
;;;;;;;;;;;;;;;;;;;;;;;5秒延时
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;delay5: mov r4,#5 delay5_ : call delay_1s djnz r4,delay5_ ret;;;;;;;;;;;;;;;;;;;;;;;3秒延时
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;delay3: mov r4,#3 delay3_: call delay_1s djnz r4,delay3_ ret
;;;;;;;;;;;;;;;;;;;;;;
delay_200ms: mov r5,#20 s: call delay_10ms djnz r5,s ret
0.2秒延时
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
非中断精确1MS定时程序
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;delay_1ms: push 07h MOV R7 ,#249 signed:
;循环部分4机器周期 nop
nop djnz R7 ,signed pop 07h ret
;返回指令2机器周期
;2+249*4+2=1000us 可以精确定时1MS,假设外部晶振是12M
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;非中断精确10MS定时程序
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;delay_10ms: push 06h mov r6,#9
;2个机器周期用2us
delay_10ms_sined:;9次循环共用9(1ms+4us)=9036us acall delay_1ms djnz r6,delay_10ms_sined
MOV r6 ,#240
;2个机器中期用2us signed_10ms :
;循环部分4机器周期 共240次 nop
nop djnz r6 ,signed_10ms pop 06h ret
;返回指令要2us;2us+9036us+240*4us+2us = 10ms 即可精确定时10ms;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
非中断精确定时1s
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay_1s: push 05h mov r5,#99
;两个机器周期2us
delay_1s_signed:
;循环指令周期为4us,加上延时10ms
;(10ms+4us)*99 = 990.396ms acall delay_10ms djnz r5,delay_1s_signed mov r5 ,#9
;两个机器周期2us signed_1s:
;循环指令周期为4us,加上延时1ms
;(1ms+4us)*9 = 9ms+36us acall delay_1ms djnz r5 ,signed_1s
mov r5 ,# 140
;机器周期2us signed_1s_:
;一次循环4us共有140次。140us*4 = 560us nop nop djnz r5,signed_1s_ pop 05h ret
;2us
;2us+990ms+396us+2us+9ms+36us+2us+560us+2us = 999ms+1000us = 1s
end