3.4 上机实验——80C51程序设计实验
参考程序
ORG 0AA0H
L5800: MOV R0,#10H
MOV R1,#00H
MOV DPTR,#2000H
L5811: MOVX A,@DPTR
CJNE A,#00H , L5816
INC R1
L5816: INC DPTR
DJNZ R0,L5811
MOV DPTR,#2100H
MOV A,R1
MOVX @DPTR,A
L581E: SJMP L581E
END
4.7.1 多分支程序
参考程序
ORG 0A10H
MEMS: MOV A,20H
MOV DPTR,#KKKK
RL A
JMP @A+DPTR
KKKK: AJMP MEMSP0
AJMP MEMSP1
AJMP MEMSP2
SJMP MEMSP3
MEMSP0: MOV 20H,#0C0H
LJMP SOX0
MEMSP1: MOV 20H,#0F9H
LJMP SOX0
MEMSP2: MOV 20H,#0A4H
LJMP SOX0
MEMSP3: MOV 20H,#0B0H
LJMP SOX0
---------------------------------------------
SOX0: MOV A,20H
MOV R0,#0DCH
MOV R1,#0DDH
MOVX @R0,A
MOV A,#01H
SOX1: MOVX @R1,A
MOV R2,#20H
LCALL DELY
RL A
SJMP SOX1
---------------------------------------------
DELY: PUSH 02H
DEL2: PUSH 02H
DEL3: PUSH 02H
DEL4: DJNZ R2,DEL4
POP 02H
DJNZ R2,DEL3
POP 02H
DJNZ R2,DEL2
POP 02H
DJNZ R2,DELY
RET
---------------------------------------------
END
4.7.2 拆字程序
参考程序
ORG 0660H
SE02: MOV DPTR,#2000H
MOVX A,@DPTR
MOV B,A
SWAP A
ANL A,#0FH
INC DPTR
MOVX @DPTR,A
INC DPTR
MOV A,B
ANL A,#0FH
MOVX @DPTR,A
SJMP $
END
8.6.1 显示接口实验
参考程序
ORG 0000H
DIS: MOV A,#03H
MOV DPTR,#0ff20H
MOVX @DPTR,A
LD2: MOV R0,#78H
MOV R3,#20H
MOV A,R3
LD0: MOV DPTR,#0ff21H
MOVX @DPTR,A
INC DPTR
MOV A,@R0
ADD A,#0EH
MOVC A,@A+PC
MOVX @DPTR,A
ACALL DL1
INC R0
MOV A,R3
JB ACC.0,LD1
RR A
MOV R3,A
AJMP LD0
LD1: AJMP LD2
DB 0c0H,0f9H,0a4H,0b0H,99H,92H,82H,0f8H,80H,90H
DB 88H,83H,0c6H,0a1H,86H,8eH
DL1: MOV R5,#02H
DL0: MOV R7,#02H
DL: MOV R6,#0FFH
DL6: DJNZ R6,DL6
DJNZ R7,DL
DJNZ R5,DL0
RET
END
8.6.2 模/数转换(A/D)实验
程序框图与参考程序
CSEG AT 0000H
LJMP START
CSEG AT 4003H
LJMP NT0
CSEG AT 4100H
PORT EQU H
START: MOV IE,#01H ;开放INT0
MOV IP,#1 ;INT0为高级中断
MOV R4,#0
MOV DPTR,#PORT ;启动通道0
MOVX @DPTR,A
SETB EA
MOV R0,#0FFH
LOOP1: DJNZ R0,LOOP1 ;等待中断
MOV R4,#1
INC DPTR
MOVX @DPTR,A ;启动通道1
SETB EA
MOV R0,#0FFH
LOOP2: DJNZ R0,LOOP2 ;等待中断
MOV R4,#2
MOV R0,#0FFH
INC DPTR
MOVX @DPTR,A ;启动通道2
SETB EA
MOV R0,#0FFH
LOOP21:DJNZ R0,LOOP21 ;等待中断
CLR EA ;关中断
DISP: MOV A,R1 ;从R1、R2、R3中取转
SWAP A ;换结果,分离高四位
ANL A,#H ;和低四位并依次存
MOV 50H,A ;放在50H到55H中
MOV A,R1
ANL A,#H
MOV 51H,A
MOV A,R2
SWAP A
ANL A,#H
MOV 52H,A
MOV A,R2
ANL A,#H
MOV 53H,A
MOV A,R3
SWAP A
ANL A,#H
MOV 54H,A
MOV A,R3
ANL A,#H
MOV 55H,A
LOOP: MOV DPTR,#0CFE9H ;写显示
MOV A,#90H ;RAM命令字
MOVX @DPTR,A
MOV R7,#06H ;06为显示位数
MOV R0,#50H ;存放转换结果地址初值送R0
MOV DPTR,#0CFE8H ;8279数据口地址
DL0: MOV A,@R0
ACALL TABLE ;转换为显码
MOVX @DPTR,A ;送显码输出
INC R0
DJNZ R7,DL0 ;没显示完则继续
SJMP DEL1
TABLE: INC A
MOVC A,@A+PC
RET
DB H, H, H, H, H, H, H, H
DB H, H, H, H, H, H, H, H
DEL1: MOV R6,#200 ;延时一段时间使显示更稳定
DEL2: MOV R5,#126
DEL3: DJNZ R5,DEL3
DJNZ R6,DEL2
SETB EA ;开中断
LJMP START ;循环
NT0: CLR A
CJNE R4,#,AD1 ;通道号为0吗
MOV R0,#0FFH
LOOP3: DJNZ R0,LOOP3
MOVX A,@DPTR
MOV R1,A ;通道号为0,则将结果存入R1
SJMP INI
AD1: CJNE R4,#,AD2 ;通道号为1吗
MOV R0,#0FFH
LOOP4:DJNZ R0,LOOP4
MOVX A,@DPTR
MOV R2,A ;通道号为1,则将结果存入R2
SJMP INI
AD2: MOV R0,#0FFH
LOOP5:DJNZ R0,LOOP5
MOVX A,@DPTR
MOV R3,A ;通道号为2,则将结果存入R3
INI: RETI
END
程序框图如图B-1、图B-2所示。
图B-1 程序框图(1) 图B-2 程序框图(2)
8.6.3 D/A转换器接口实验
参考程序
ORG 0000H
MOV SP,#53H
MOV 7EH,#00H
MOV 7DH,#08H
MOV 7CH,#03H
MOV 7BH,#02H
MOV A,#03H
MOV R1,#20H
MOVX @R1,A
LO20:MOV R6,#00H
LO21:MOV DPTR,#8000H
MOV A,R6
MOVX @DPTR,A
MOV R0,#79H
LCALL PTDS
LCALL DIS
MOV R2,#08H
LCALL DL1
INC R6
CJNE R6,#0FFH,LO21
LO22:MOV DPTR,#8000H
DEC R6
MOV A,R6
MOVX @DPTR,A
MOV R0,#79H
LCALL PTDS
LCALL DIS
MOV R2,#08H
LCALL DL1
CJNE R6,#00H,LO22
SJMP LO20
PTDS:MOV R1,A
ACALL PTDS1
MOV A,R1
SWAP A
PTDS1:ANL A,#0FH
MOV @R0,A
INC R0
RET
DIS:SETB RS1
MOV R5,#05H
DIS1:MOV 30H,#20H
MOV 31H,#7EH
MOV R7,#06H
DIS2:MOV R1,#21H
MOV A,30H
MOVX @R1,A
MOV R0,31H
MOV A,@R0
MOV DPTR,#DDFF
MOVC A,@A+DPTR
MOV R1,#22H
MOVX @R1,A
MOV A,30H
RR A
MOV 30H,A
DEC 31H
MOV A,#0FFH
MOVX @R1,A
DJNZ R7,DIS2
DJNZ R5,DIS1
CLR RS1
RET
DL1:MOV R5,#02H
DL0:MOV R7,#02H
DL: MOV R4,#0FFH
DL6:DJNZ R4,DL6
DJNZ R7,DL
DJNZ R5,DL0
RET
DDFF:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
DB 88H,83H,0C6H,0A1H,86H,0FFH,0CH,89H,0DEH
END
9.6 电脑时钟程序实验
参考程序
ORG 0000H
LJMP CHK0
ORG 000BH
LJMP CLOCK
CHK0:MOV SP,#60H
LCALL LCK0
LCALL PTDS0
MOV TMOD,#01H
ORL IE,#82H
MOV TL0,#0B7H
MOV TH0,#0CH
MOV 23H,#00H
SETB TR0
LOO5:LCALL SSEE
LCALL PTDS0
SJMP LOO5
PTDS0:MOV R0,#79H
MOV A,22H
ACALL PTDS
MOV A,21H
ACALL PTDS
MOV A,20H
ACALL PTDS
RET
PTDS: MOV R1,A
ACALL PTDS1
MOV A,R1
SWAP A
PTDS1:ANL A,#0FH
MOV @R0,A
INC R0
RET
CLOCK:MOV TL0,#0B7H
MOV TH0,#3CH
PUSH PSW
PUSH ACC
SETB 0D3H
INC 23H
MOV A,23H
CJNE A,#0AH,DONE
MOV 23H,#00H
MOV A,22H
INC A
DA A
MOV 22H,A
CJNE A,#60H,DONE
MOV 22H,#00H
MOV A,21H
INC A
DA A
MOV 21H,A
CJNE A,#60H,DONE
MOV 21H,#00H
MOV A,20H
INC A
DA A
MOV 20H,A
CJNE A,#24H,DONE
MOV 20H,#00H
DONE:POP ACC
POP PSW
RETI
ORG 0D50H
SSEE:SETB RS1
MOV R5,#05H
SSE2:MOV 30H,#20H
MOV 31H,#7EH
MOV R7,#06H
SSE1:MOV R1,#21H
MOV A,30H
MOVX @R1,A
MOV R0,31H
MOV A,@R0
MOV DPTR,#DDFF
MOVC A,@A+DPTR
MOV R1,#22H
MOVX @R1,A
MOV A,30H
RR A
MOV 30H,A
DEC 31H
MOV A,#0FFH
MOVX @R1,A
DJNZ R7,SSE1
DJNZ R5,SSE2
CLR RS1
RET
DDFF:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
DB 88H,83H,0C6H,0A1H,86H,8EH,0FFH,0CH,89H,0DEH
ORG 1D00H
X3:MOV R4,A
MOV R0,#59H
MOVX A,@R0
MOV R1,A
MOV A,R4
MOV @R1,A
CLR A
POP DPH
POP DPL
MOVC A,@A+DPTR
INC DPTR
CJNE A,01H,X30
CLR A
MOVC A,@A+DPTR
X31:MOVX @R0,A
INC DPTR
PUSH DPL
PUSH DPH
RET
X30:DEC R1
MOV A,R1
SJMP X31
X2:MOV R6,#50H
X0:ACALL XLE
JNB ACC.5,XX0
DJNZ R6,X0
MOV R6,#20H
MOV R0,#59H
MOVX A,@R0
MOV R0,A
MOV A,@R0
MOV R7,A
MOV A,#10H
MOV @R0,A
X1:ACALL XLE
JNB ACC.5,XX1
DJNZ R6,X1
MOV A,R7
MOV @R0,A
SJMP X2
XX1:MOV R6,A
MOV A,R7
MOV @R0,A
MOV A,R6
XX0:RET
XLE:ACALL DIS
ACALL KEY
MOV R4,A
MOV R1,#48H
MOVX A,@R1
MOV R2,A
INC R1
MOVX A,@R1
MOV R3,A
MOV A,R4
XRL A,R3
MOV R3,04H
MOV R4,02H
JZ X10
MOV R2,#88H
MOV R4,#88H
X10:DEC R4
MOV A,R4
XRL A,#82H
JZ X11
MOV A,R4
XRL A,#0EH
JZ X11
MOV A,R4
ORL A,R4
JZ X12
MOV R4,#20H
DEC R2
SJMP X13
X12:MOV R4,#0FH
x11:MOV R2,04H
MOV R4,03H
X13:MOV R1,#48H
MOV A,R2
MOVX @R1,A
INC R1
MOV A,R3
MOVX @R1,A
MOV A,R4
RET
LS3:DB 07H,04H,08H,05H,09H,06H,0AH
DB 0BH,01H,00H,02H,0FH,03H,0EH
DB 0CH,0DH
DIS:PUSH DPH
PUSH DPL
SETB RS1
MOV R0,#7EH
MOV R2,#20H
MOV R3,#00H
MOV DPTR,#LS0
LS2:MOV A,@R0
MOVC A,@A+DPTR
MOV R1,#22H
MOVX @R1,A
MOV A,R2
DEC R1
MOVX @R1,A
DEC R0
LS1:DJNZ R3,LS1
CLR C
RRC A
MOV R2,A
JNZ LS2
INC R1
MOV A,#0FFH
MOVX @R1,A
CLR RS1
POP DPL
POP DPH
RET
LS0:DB 0C0H,0F9H,0A4H,0B0H,99H,92H
DB 82H,0F8H,80H,90H,88H,83H,0C6H
DB 0A1H,86H,8EH,0FFH,0CH,89H,7FH,0BFH
KEY:SETB RS1
MOV R2,#0FEH
MOV R3,#08H
MOV R0,#00H
LP1:MOV A,R2
MOV R1,#21H
MOVX @R1,A
RL A
MOV R2,A
MOV R1,#23H
MOVX A,@R1
CPL A
ANL A,#0FH
JNZ LP0
INC R0
DJNZ R3,LP1
MOVX A,@R1
JB ACC.4,XP33
MOV A,#19H
SJMP XP3
XP33:MOV A,#20H
XP3:CLR RS1
RET
LP0:CPL A
JB ACC.0,XP0
MOV A,#00H
SJMP LPP
XP0:JB ACC.1,XP1
MOV A,#08H
SJMP LPP
XP1:JB ACC.2,XP2
MOV A,#10H
SJMP LPP
XP2:JB ACC.3,XP33
MOV A,#18H
LPP:ADD A,R0
CLR RS1
CJNE A,#10H,LX0
LX0:JNC XP35
MOV DPTR,#LS3
MOVC A,@A+DPTR
XP35:RET
LCK0:LCALL X1
JNC LCK1
LCALL X3
MOV R1,#7EH
SJMP LCK0
LCK1:CJNE A,#16H,LCK0
MOV A,7AH
SWAP A
ORL A,79H
MOV 22H,A
CJNE A,#60H,LCK2
LCK2:JNC EXIT
MOV A,7CH
SWAP A
ORL A,7BH
MOV 21H,A
CJNE A,#60H,CLK3
CLK3:JNC EXIT
MOV A,7EH
SWAP A
ORL A,7DH
MOV 20H,A
CJNE A,#24H,CLK4
CLK4:JNC EXIT
RET
EXIT:AJMP LCK0
END