《51单片机常见汇编程序实验代码.docx》由会员分享,可在线阅读,更多相关《51单片机常见汇编程序实验代码.docx(25页珍藏版)》请在三一办公上搜索。
1、51单片机常见汇编程序实验代码1. 将片外8000H-80FFH单元写入数据AB 1 2. 将片内RAM20H单元中数据在数码管上显示出来 1 3. 将片内ARM30H-40H单元清零 2 4. 将六位数显示在数码管上 2 5. 8255并口芯片的应用:交通灯控制系统的设计 3 6. 将交通灯点亮 4 7. AD转换实验 4 8. DA转换实验 5 9. 定时器的应用 6 10. 开关控制LED的亮灭及速度 6 11. 计数器实验 6 12. 串并转换实验 7 13. 直流电机速度检测 8 14. 8255PB外接8个开关,编程将开关状态显示在数码管上 9 15. P3.4接开关K,编程将开关
2、拨动次数,通过串并转换的方式进行显示 10 16. 比较片内RAM30H、31H两个单元值的大小,将较大的数显示在数码管上 10 17. 单片机P1口接8个开关,编程将开关状态显示在数码管上 11 18. 将片内50H单元的值显示在数码管上12 19. 开关K1接P1.0,K2接P1.1,编程实现当按下K1时在数码管上显示50H单元的值,按下K2在数码管上显示51H单元的值 12 1. 将片外8000H-80FFH单元写入数据AB ORG 0000H ;程序从0000H开始执行 AJMP MAIN ;跳转到主程序 ORG 0030H ;以免覆盖中断地址 MAIN: MOV SP,#60H; 避
3、免堆栈和工作寄存器区冲突 MOV DPTR,#8000H MOV R0,#0 LOOP: MOV A,#0ABH MOVX DPTR,A INC DPTR INC R0 CJNE R0,#0,LOOP ; 判断 AJMP $; 等待 END ;调试-视图-M存储器(输入地址X:8000H) 2. 将片内RAM20H单元中数据在数码管上显示出来 ORG 0000H ;程序从0000H开始执行 AJMP MAIN ;跳转到主程序 ORG 0030H ;以免覆盖中断地1 址 MAIN: MOV SP,#60H ;避免堆栈和工作MOV DPTR,#0E100H ;指向命MOV A,#03H ;PA、P
4、B口输出 ;所有并口寄存器区冲突 令口地址 MOVX DPTR,A DJNZ 0FH,P164_1 MOV 0FH,#8 RET MOV DPTR,#0E101H MOV A,R2 MOVX DPTR,A RL A MOV R2,A PBIT: 显示程序先进行8155初始化 LOOP: 的地址送R0 管开始显示 CHAI: DISPLAY: P164: P164_1: MOV 20H,#34H MOV A,20H ACALL CHAI MOV R0,#10H ;第一个显示数MOV R1,#2 ;显示2个数 MOV R2,#1 ;从倒数第一个数码ACALL DISPLAY LJMP LOOP ;
5、判断 MOV B,#10H DIV AB MOV 10H,B MOV 11H,A RET MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPTR,A INC R0 DJNZ R1,DISPLAY RET MOV DPTR,#TAB MOVC A,A+DPTR RRC A MOV R3,A MOV ACC.0,C ANL A,#0FDH MOV DPTR,#0E102H MOVX DPTR,A ORL A,#0FEH MOVX DPTR,A MOV A,R3 RET DELAY: MOV R6,#4 DEL
6、: MOV R7,#250 DJNZ R7,$ DJNZ R6,DEL RET TAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 3. 将片内ARM30H-40H单元清零 ORG 0000H ;程序从0000H开始执行 AJMP MAIN ;跳转到主程序 ORG 0030H ;以免覆盖中断地址 MAIN: MOV SP,#60H ;避免堆栈和工作寄存器区冲突 MOV R0,#30H MOV A,#0 LOOP: MOV R0,A INC R0 CJNE R0,#41H,
7、LOOP ;判断 AJMP $ ;等待 END ;地址D:30H 4. 将六位数显示在数码管上 ORG 0000H ;程序从0000H开始执行 AJMP MAIN ;跳转到主程序 ORG 0030H ;以免覆盖中断地址 MAIN: MOV SP,#60H ;避免堆栈和工作2 寄存器区冲突 MOV DPTR,#0E100H ;指向命令口地址 MOV A,#03H ;PA、PB口输出 MOVX DPTR,A ;所有并口显示程序先进行8155初始化 LOOP: MOV R0,#10H ;第一个显示数的地址送R0 MOV 10H,#0H MOV 11H,#3H MOV 12H,#1H MOV 13H,
8、#2H MOV 14H,#1H MOV 15H,#1H MOV R1,#6 ;显示6个数 MOV R2,#1 ;从倒数第一个数码管开始显示 ACALL DISPLAY LJMP LOOP DISPLAY: MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPTR,A INC R0 DJNZ R1,DISPLAY RET P164: MOV DPTR,#TAB MOVC A,A+DPTR P164_1: RRC A MOV R3,A MOV ACC.0,C ANL A,#0FDH MOV DPTR,#0E10
9、2H MOVX DPTR,A ORL A,#0FEH MOVX DPTR,A MOV A,R3 DJNZ 0FH,P164_1 MOV 0FH,#8 RET PBIT: MOV DPTR,#0E101H MOV A,R2 MOVX DPTR,A RL A MOV R2,A RET DELAY: MOV R6,#4 DEL: MOV R7,#250 DJNZ R7,$ DJNZ R6,DEL RET TAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 5. 8255并口芯片
10、的应用:交通灯控制系统的设计 ORG 0000H AJMP START ORG 0030H START: MOV DPTR,#0AC03H MOV A,#80H MOVX DPTR,A /8155初始化(所有并口显示中都要先8155初始化) MOV R5,#0F0H ACALL DENG MOV R1,#20 ACALL DELAY MOV R0,#1 LOOP01: MOV R5,#5AH ACALL DENG MOV R1,#100 ACALL DELAY DJNZ R0,LOOP01 MOV R0,#3 / LED_2闪烁次数 LOOP02: MOV R5, #0FAH ACALL DE
11、NG MOV R1,#10 ACALL DELAY MOV R5,#50H ACALL DENG MOV R1,#10 ACALL DELAY 3 DJNZ R0,LOOP02 MOV R5,#0A5H ACALL DENG MOV R1,#100 ACALL DELAY MOV R0,#3 LOOP03: MOV R5, #0F5H ACALL DENG MOV R1,#10 ACALL DELAY MOV R5,#0A0H ACALL DENG MOV R1,#10 ACALL DELAY DJNZ R0,LOOP03 AJMP LOOP01 DENG: MOV DPTR,#0AC00H
12、/8155 PA口输出 MOV A,R5 MOVX DPTR,A /8155 PA口送出显示参数 RET /100ms延时子程序 DELAY: MOV R7,#200 DEL: MOV R6,#229 DJNZ R6,$ DJNZ R7,DEL DJNZ R1,DELAY RET END 6. 将交通灯点亮 ORG 0000H AJMP START ORG 0030H START: MOV DPTR,#0AC03H MOV A,#80H MOVX DPTR,A MOV DPTR,#0AC00H MOV A,#05AH MOVX DPTR,A AJMP $ END 7. AD转换实验 ORG 0
13、000 AJMP START ORG 000BH AJMP INT_0 ORG 0030H START: MOV SP,#60H MOV TMOD,#01H MOV TH0,#4BH MOV TL0,#0FDH MOV R4,#2 SETB EA SETB ET0 SETB TR0 MOV DPTR,#0E100H MOV A,#3 MOVX DPTR,A MOV 22H,#9 MOV 23H,#0 MOV 24H,#8 MOV 25H,#0 LOOP: MOV R0,#20H MOV R1,#6 MOV R2,#1 ACALL DISPLAY SJMP LOOP INT_0: MOV TH0
14、,#4BH MOV TL0,#0FDH DJNZ R4,EXIT PUSH ACC PUSH DPH PUSH DPL MOV DPTR,#0A000H MOVX A,DPTR MOV B,#10H DIV AB MOV 21H,A MOV 20H,B MOVX DPTR,A MOV R4,#2 POP DPL POP DPH POP ACC 4 EXIT: RETI DISPLAY: MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPTR,A INC R0 DJNZ R1,DISPLAY RET P16
15、4: MOV DPTR,#TAB MOVC A,A+DPTR P164_1: RRC A MOV R3,A MOV ACC.0,C ANL A,#0FDH MOV DPTR,#0E102H MOVX DPTR,A ORL A,#0FEH MOVX DPTR,A MOV A,R3 DJNZ 0FH,P164_1 MOV 0FH,#8 RET PBIT: MOV DPTR,#0E101H MOV A,R2 MOVX DPTR,A RL A MOV R2,A RET DELAY: MOV R6,#4 DEL: MOV R7,#250 DJNZ R7,$ DJNZ R6,DEL RET TAB: DB
16、 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 8. DA转换实验 ORG 0000 SJMP START ORG 0030H START: MOV SP,#60H MOV A,#0 LOOP1: MOV DPTR,#0B000H MOVX DPTR,A ACALL XS ACALL DELAY INC A CJNE A,#0FFH,LOOP1 LOOP2: MOV DPTR,#0B000H MOVX DPTR,A ACALL XS ACALL DELAY DEC A CJNE A
17、,#0,LOOP2 SJMP LOOP1 XS: PUSH ACC MOV DPTR,#TAB MOV B,#51 DIV AB MOV R1,A XCH A,B MOV B,#5 DIV AB MOVC A,A+DPTR MOV R4,#8 ACALL S164 MOV A,R1 MOVC A,A+DPTR MOV R4,#8 ACALL S164 MOV A,#0 CLR C MOV R4,#16 ACALL S164 POP ACC RET S164: RRC A MOV P1.0,C 5 CLR P1.1 SETB P1.1 DJNZ R4,S164 RET DELAY: MOV R7
18、,#200 DEl: MOV R6,#229 DJNZ R6,$ DJNZ R7,DEL RET TAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 9. 定时器的应用 ORG 0000H AJMP START ORG 00BH AJMP INT_0 ORG 0030H START: MOV TMOD,#01H MOV TH0,#4BH MOV TL0,#0FDH MOV R0,#20 MOV R1,#0 SETB ET0 SETB TR0 SETB EA AJMP $
19、INT_0: MOV TH0,#4BH MOV TL0,#0FDH DJNZ R0,EXIT MOV R0,#20 MOV A,R1 MOV DPTR,#TAB MOVC A,A+DPTR MOV P1,A INC R1 CJNE R1,#8,EXIT MOV R1,#0 EXIT: RETI TAB: DB 0FFH,0FAH,0F5H,0AFH,05FH,0AAH,55H,0H,0FFH END 10. 开关控制LED的亮灭及速度 ORG 0000H ;程序从0000H开始执行 AJMP START ;跳转到主程序 ORG 0030H ;以免覆盖中断地址 START: MOV R4,#1
20、;立即数传送到累加器 LOOP: CLR P3.3 ;进位标志位清零 LOOP1: JNB P3.5,LOOP ;直接寻址位为0则转移 JB P3.4,LOOP2 ;直接寻址位为1则转移 JNB P3.4,$ INC R4 ;寄存器增1 LOOP2: CPL P3.3 ;累加器求反 MOV A,R4 ;寄存器内容传送到累加器A MOV R5,A ;累加器内容传送到累加器 ACALL DELAY ;调用延时函数 AJMP LOOP1 ;循环 DELAY: MOV R6,#200 DEL: MOV R7,#229 DJNZ R7,$ DJNZ R6,DEL ;寄存器减1,不为0则转移 DJNZ R
21、5,DELAY RET ;子程序返回 END 11. 计数器实验 ORG 0000 SJMP START ORG 001BH LJMP INT_1 START: MOV SP,#60H MOV R5,#20 MOV TMOD,#15H 6 MOV TH1,#4BH MOV TL1,#0FDH MOV TH0,#0 MOV TL0,#0 SETB ET1 SETB EA SETB TR1 SETB TR0 MOV DPTR,#0E100H MOV A,#3 MOVX DPTR,A LOOP: MOV R0,#50H; MOV R1,#4 MOV R2,#1 LCALL DISPLAY SJMP
22、LOOP INT_1: MOV TH1,#4BH MOV TL1,#0FDH DJNZ R5,CON MOV R5,#20 PUSH ACC PUSH B MOV A,TL0 MOV B,#10H DIV AB MOV 50H,B MOV 51H,A MOV A,TH0 MOV B,#10H DIV AB MOV 52H,B MOV 53H,A MOV TH0,#0 MOV TL0,#0 POP B POP ACC CON: RETI DISPLAY: MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPT
23、R,A INC R0 DJNZ R1,DISPLAY RET P164: MOV DPTR,#TAB MOVC A,A+DPTR P164_1: RRC A MOV R3,A MOV ACC.0,C ANL A,#0FDH MOV DPTR,#0E102H MOVX DPTR,A ORL A,#0FEH MOVX DPTR,A MOV A,R3 DJNZ 0FH,P164_1 MOV 0FH,#8 RET PBIT: MOV DPTR,#0E101H MOV A,R2 MOVX DPTR,A RL A MOV R2,A RET DELAY: MOV R6,#4 DEL: MOV R7,#250
24、 DJNZ R7,$ DJNZ R6,DEL RET TAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 12. 串并转换实验 ORG 0000 SJMP START ORG 0030H START: MOV SP,#60H MOV R1,#0 MOV DPTR,#TAB LOOP: MOV A,#0 CLR C 7 MOV R4,#24 ACALL S164 MOV A,R1 MOVC A,A+DPTR MOV R4,#8 ACALL S164 SETB EA SETB
25、 TR0 SETB TR1 MOV R2,#00H UP: MOV A,R2 MOV DPTR,#0A000H /注意片选接YC2, INC R1 MOV R5,#10 ACALL DELAY CJNE R1,#10,EXIT MOV R1,#0 EXIT: SJMP LOOP S164: RRC A MOV P1.0,C CLR P1.1 SETB P1.1 DJNZ R4,S164 RET DELAY: MOV R6,#200 DEL: MOV R7,#230 DJNZ R7,$ DJNZ R6,DEL DJNZ R5,DELAY RET TAB: DB 0FCH,60H,0DAH,0F2
26、H,66H,0B6H,0BEH,0E0H, 0FEH,0F6H END 13. 直流电机速度检测 ORG 0000H AJMP MAIN ORG 001BH AJMP INT_1 ORG 0030H MAIN: MOV SP,#60H MOV TMOD,#15H MOV TH1,#4BH MOV TL1,#0FDH MOV R0,#20 MOV TH0,#0 MOV TL0,#0 SETB ET1 非YC3 MOVX DPTR,A MOV R5,#1 ACALL DELAY ;100ms INC R2 CJNE R2,#0FFH,UP DOWN:MOV A,R2 MOV DPTR,#0A000
27、H MOVX DPTR,A MOV R5,#1 ACALL DELAY ;100ms DEC R2 CJNE R2,#00H,DOWN AJMP UP INT_1: MOV TH1,#4BH MOV TL1,#0FDH DJNZ R0,EXIT MOV R0,#20 PUSH ACC PUSH DPH PUSH DPL MOV A,TL0 ACALL XS MOV TL0,#0 MOV A,TH0 ACALL XS MOV TH0,#0 POP DPL POP DPH POP ACC EXIT: RETI XS: MOV DPTR,#TAB ;MOV A,R2 MOV B,#10 ;显示10进
28、制数 DIV AB XCH A,B ;MOV DPTR,#TAB MOVC A,A+DPTR 8 ACALL FS XCH A,B ;MOV DPTR,#TAB MOVC A,A+DPTR ACALL FS RET FS: MOV R1,#8 YW:RRC A MOV P1.0,C CLR P1.1 SETB P1.1 DJNZ R1,YW RET DELAY:MOV R6,#200 DEL:MOV R7,#230 DJNZ R7,$ DJNZ R6,DEL DJNZ R5,DELAY RET TAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0F
29、EH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 14. 8255PB外接8个开关,编程将开关状态显示在数码管上 ORG 0000 AJMP START ORG 0030H START: MOV DPTR,#0AC03H MOV A, #82H MOVX DPTR, A MOV DPTR,#0E100H MOV A,#3 MOVX DPTR,A LOOP: MOV DPTR,#0AC01H MOVX A,DPTR MOV B,#10H DIV AB MOV 10H,B MOV 11H,A MOV R0,#10H MOV R1,#2 MOV R2,#1 ACALL DIS
30、PLAY SJMP LOOP DISPLAY:MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPTR,A INC R0 DJNZ R1,DISPLAY RET P164: MOV DPTR,#TAB MOVC A,A+DPTR P164_1: RRC A MOV R3,A MOV ACC.0,C ANL A,#0FDH MOV DPTR,#0E102H MOVX DPTR,A ORL A,#0FEH MOVX DPTR,A MOV A,R3 DJNZ 0FH,P164_1 MOV 0FH,#8 RET P
31、BIT: MOV DPTR,#0E101H MOV A,R2 MOVX DPTR,A RL A MOV R2,A RET DELAY: MOV R6,#4 DEL: MOV R7,#250 DJNZ R7,$ DJNZ R6,DEL RET TAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH 9 END 15. P3.4接开关K,编程将开关拨动次数,通过串并转换的方式进行显示 ORG 0000 SJMP START ORG 0030H START: MOV SP,#60H MOV
32、R5,#0 LOOP: JB P3.4,$ ACALL DELAY JB P3.4,LOOP INC R5 ACALL XS LOOP1: JNB P3.4,$ ACALL DELAY JNB P3.4,LOOP1 INC R5 ACALL XS SJMP LOOP DELAY: MOV R7,#200 DEL: MOV R6,#229 DJNZ R6,$ DJNZ R7,DEL RET XS: MOV DPTR,#TAB MOV A,R5 MOV B,#10H DIV AB MOV R1,A MOV A,B MOVC A,A+DPTR MOV R4,#8 ACALL S164 MOV A,R
33、1 MOVC A,A+DPTR MOV R4,#8 ACALL S164 MOV A,#0 CLR C MOV R4,#16 ACALL S164 RET S164: RRC A MOV P1.0,C CLR P1.1 SETB P1.1 DJNZ R4,S164 RET TAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH END 16. 比较片内RAM30H、31H两个单元值的大小,将较大的数显示在数码管上 ORG 0000 SJMP START ORG 0030H START
34、: MOV SP,#60H MOV DPTR,#0E100H MOV A,#3 MOVX DPTR,A MOV 30H,#06H ;随便赋两个值,显示较大的 MOV 31H,#11H MOV A,30H SUBB A,31H JC SMALL MOV B,#10H MOV A,30H DIV AB MOV 30H,B MOV 31H,A BIG: MOV R0,#30H MOV R1,#2 MOV R2,#1 ACALL DISPLAY SJMP BIG SMALL: MOV B,#10H MOV A,31H DIV AB MOV 30H,B 10 MOV 31H,A MOV R1,#2 MO
35、V R2,#1 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH 17. 单片机P1口接8个开关,编程将开关状态显 END SMALL1:MOV R0,#30H ACALL DISPLAY SJMP SMALL1 DISPLAY:MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPTR,A INC R0 DJNZ R1,DISPLAY RET P164: MOV DPTR,#TAB MOVC A,A+DPTR P164_1: RRC A MOV R3,A MOV ACC.0,C ANL
36、A,#0FDH MOV DPTR,#0E102H MOVX DPTR,A ORL A,#0FEH MOVX DPTR,A MOV A,R3 DJNZ 0FH,P164_1 MOV 0FH,#8 RET PBIT: MOV DPTR,#0E101H MOV A,R2 MOVX DPTR,A RL A MOV R2,A RET DELAY: MOV R6,#4 DEL: MOV R7,#250 DJNZ R7,$ DJNZ R6,DEL RET TAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 示在数码管上 ORG 0000H SJMP START O
37、RG 0030H START: MOV SP,#60H MOV DPTR,#0E100H MOV A,#3 MOVX DPTR,A LOOP: MOV A,P1 MOV B,#10H DIV AB MOV 20H,B MOV 21H,A MOV R0,#20H MOV R1,#2 MOV R2,#1 ACALL DISPLAY AJMP LOOP DISPLAY:MOV A,R0 MOV 0FH,#8 ACALL P164 ACALL PBIT ACALL DELAY MOV A,#0 MOVX DPTR,A INC R0 DJNZ R1,DISPLAY RET P164: MOV DPTR,#TAB MOVC A,A+DPTR P164_1: RRC A MOV R3,A MOV ACC.0,C ANL A,#0FDH MOV DPTR,#0E102H MOVX DPTR,A ORL A,#0FEH