这两天在用89C51做一个按键选择的汇编程序,P2.0-P2.7分别接8个继电器,初始状态设为低电平,P3.3和P3.4分别接两个按键,初始为高电平。P3.3用于选择,程序一开始是选定P2.0 ,按一下P3.3,选择P2.1 ,再按一下P3.3选择P2.2,一直选到P2.7再回到P2.0开始继续选择。P3.4用来确定当前的选择,假如P3.3选到P2.5时,按下P3.4,那么P2.5所接的继电器导通,反之则关闭。下面是我写的程序,接触单片机时间不长,这是我写的第一个东西,不太会用中断,只能用这种笨方法来写,总觉得哪不对,又找不出来。请大家帮我看看,水平有限,见笑了。
ORG 0000H LJMP START
ORG 0100H
START: MOV SP, #5FH MOV P2, #00H MOV 70H, #01H K1: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #01H, K2 ;相等不转移 CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K12 AJMP K1
K12: CPL P2.0 ;取反 MOV 70H, P3.4
K2: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #0H, K3
CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K22 AJMP K2
K22: CPL P2.1 MOV 70H, P3.4
K3: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #1H, K4
CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K32 AJMP K3
K32: CPL P2.2 MOV 70H, P3.4
K4: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #0H, K5
CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K42 AJMP K4
K42: CPL P2.3 MOV 70H, P3.4
K5: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #1H, K6
CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K52 AJMP K5
K52: CPL P2.4 MOV 70H, P3.4
K6: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #0H, K7
CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K62 AJMP K6
K62: CPL P2.5 MOV 70H, P3.4
K7: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #1H, K8
CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K72 AJMP K7
K72: CPL P2.6 MOV 70H, P3.4
K8: CLR A MOV C, P3.3 MOV ACC.0, C CJNE A, #0H, TURN1 ACALL K81 TURN1: LJMP K1
K81: CLR A MOV C, P3.4 MOV ACC.0, C CJNE A, 70H, K82 AJMP K8
K82: CPL P2.7 MOV 70H, P3.4 LJMP K1
END
|