单片机常见汇编程序实验代码.docx
《单片机常见汇编程序实验代码.docx》由会员分享,可在线阅读,更多相关《单片机常见汇编程序实验代码.docx(33页珍藏版)》请在冰豆网上搜索。
单片机常见汇编程序实验代码
1.将片外8000H-80FFH单元写入数据AB
ORG0000H;程序从0000H开始执行
AJMPMAIN;跳转到主程序
ORG0030H;以免覆盖中断地址
MAIN:
MOVSP,#60H;避免堆栈和工作寄存器区冲突
MOVDPTR,#8000H
MOVR0,#0
LOOP:
MOVA,#0ABH
MOVX@DPTR,A
INCDPTR
INCR0
CJNER0,#0,LOOP;判断
AJMP$;等待
END;调试-视图-M存储器(输入地址X:
8000H)
2.将片内RAM20H单元中数据在数码管上显示出来
ORG0000H;程序从0000H开始执行
AJMPMAIN;跳转到主程序
ORG0030H;以免覆盖中断地址
MAIN:
MOVSP,#60H;避免堆栈和工作寄存器区冲突
MOVDPTR,#0E100H;指向命令口地址
MOVA,#03H;PA、PB口输出
MOVX@DPTR,A;所有并口显示程序先进行8155初始化
MOV20H,#34H
MOVA,20H
ACALLCHAI
LOOP:
MOVR0,#10H;第一个显示数的地址送R0
MOVR1,#2;显示2个数
MOVR2,#1;从倒数第一个数码管开始显示
ACALLDISPLAY
LJMPLOOP;判断
CHAI:
MOVB,#10H
DIVAB
MOV10H,B
MOV11H,A
RET
DISPLAY:
MOVA,@R0
MOV0FH,#8
ACALLP164
ACALLPBIT
ACALLDELAY
MOVA,#0
MOVX@DPTR,A
INCR0
DJNZR1,DISPLAY
RET
P164:
MOVDPTR,#TAB
MOVCA,@A+DPTR
P164_1:
RRCA
MOVR3,A
MOVACC.0,C
ANLA,#0FDH
MOVDPTR,#0E102H
MOVX@DPTR,A
ORLA,#0FEH
MOVX@DPTR,A
MOVA,R3
DJNZ0FH,P164_1
MOV0FH,#8
RET
PBIT:
MOVDPTR,#0E101H
MOVA,R2
MOVX@DPTR,A
RLA
MOVR2,A
RET
DELAY:
MOVR6,#4
DEL:
MOVR7,#250
DJNZR7,$
DJNZR6,DEL
RET
TAB:
DB0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H
DB0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH
END
3.将片内ARM30H-40H单元清零
ORG0000H;程序从0000H开始执行
AJMPMAIN;跳转到主程序
ORG0030H;以免覆盖中断地址
MAIN:
MOVSP,#60H;避免堆栈和工作寄存器区冲突
MOVR0,#30H
MOVA,#0
LOOP:
MOV@R0,A
INCR0
CJNER0,#41H,LOOP;判断
AJMP$;等待
END;地址D:
30H
4.将六位数显示在数码管上
ORG0000H;程序从0000H开始执行
AJMPMAIN;跳转到主程序
ORG0030H;以免覆盖中断地址
MAIN:
MOVSP,#60H;避免堆栈和工作寄存器区冲突
MOVDPTR,#0E100H;指向命令口地址
MOVA,#03H;PA、PB口输出
MOVX@DPTR,A;所有并口显示程序先进行8155初始化
LOOP:
MOVR0,#10H;第一个显示数的地址送R0
MOV10H,#0H
MOV11H,#3H
MOV12H,#1H
MOV13H,#2H
MOV14H,#1H
MOV15H,#1H
MOVR1,#6;显示6个数
MOVR2,#1;从倒数第一个数码管开始显示
ACALLDISPLAY
LJMPLOOP
DISPLAY:
MOVA,@R0
MOV0FH,#8
ACALLP164
ACALLPBIT
ACALLDELAY
MOVA,#0
MOVX@DPTR,A
INCR0
DJNZR1,DISPLAY
RET
P164:
MOVDPTR,#TAB
MOVCA,@A+DPTR
P164_1:
RRCA
MOVR3,A
MOVACC.0,C
ANLA,#0FDH
MOVDPTR,#0E102H
MOVX@DPTR,A
ORLA,#0FEH
MOVX@DPTR,A
MOVA,R3
DJNZ0FH,P164_1
MOV0FH,#8
RET
PBIT:
MOVDPTR,#0E101H
MOVA,R2
MOVX@DPTR,A
RLA
MOVR2,A
RET
DELAY:
MOVR6,#4
DEL:
MOVR7,#250
DJNZR7,$
DJNZR6,DEL
RET
TAB:
DB0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H
DB0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH
END
5.8255并口芯片的应用:
交通灯控制系统的设计
ORG0000H
AJMPSTART
ORG0030H
START:
MOVDPTR,#0AC03H
MOVA,#80H
MOVX@DPTR,A//8155初始化(所有并口显示中都要先8155初始化)
MOVR5,#0F0H
ACALLDENG
MOVR1,#20
ACALLDELAY
MOVR0,#1
LOOP01:
MOVR5,#5AH
ACALLDENG
MOVR1,#100
ACALLDELAY
DJNZR0,LOOP01
MOVR0,#3//LED_2闪烁次数
LOOP02:
MOVR5,#0FAH
ACALLDENG
MOVR1,#10
ACALLDELAY
MOVR5,#50H
ACALLDENG
MOVR1,#10
ACALLDELAY
DJNZR0,LOOP02
MOVR5,#0A5H
ACALLDENG
MOVR1,#100
ACALLDELAY
MOVR0,#3
LOOP03:
MOVR5,#0F5H
ACALLDENG
MOVR1,#10
ACALLDELAY
MOVR5,#0A0H
ACALLDENG
MOVR1,#10
ACALLDELAY
DJNZR0,LOOP03
AJMPLOOP01
DENG:
MOVDPTR,#0AC00H//8155PA口输出
MOVA,R5
MOVX@DPTR,A//8155PA口送出显示参数
RET//100ms延时子程序
DELAY:
MOVR7,#200
DEL:
MOVR6,#229
DJNZR6,$
DJNZR7,DEL
DJNZR1,DELAY
RET
END
6.将交通灯点亮
ORG0000H
AJMPSTART
ORG0030H
START:
MOVDPTR,#0AC03H
MOVA,#80H
MOVX@DPTR,A
MOVDPTR,#0AC00H
MOVA,#05AH
MOVX@DPTR,A
AJMP$
END
7.AD转换实验
ORG0000
AJMPSTART
ORG000BH
AJMPINT_0
ORG0030H
START:
MOVSP,#60H
MOVTMOD,#01H
MOVTH0,#4BH
MOVTL0,#0FDH
MOVR4,#2
SETBEA
SETBET0
SETBTR0
MOVDPTR,#0E100H
MOVA,#3
MOVX@DPTR,A
MOV22H,#9
MOV23H,#0
MOV24H,#8
MOV25H,#0
LOOP:
MOVR0,#20H
MOVR1,#6
MOVR2,#1
ACALLDISPLAY
SJMPLOOP
INT_0:
MOVTH0,#4BH
MOVTL0,#0FDH
DJNZR4,EXIT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
DJNZR7,DEL
RET
TAB:
DB0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H
DB0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH
END
9.定时器的应用
ORG0000H
AJMPSTART
ORG00BH
AJMPINT_0
ORG0030H
START:
MOVTMOD,#01H
MOVTH0,#4BH
MOVTL0,#0FDH
MOVR0,#20
MOVR1,#0
SETBET0
SETBTR0
SETBEA
AJMP$
INT_0:
MOVTH0,#4BH
MOVTL0,#0FDH
DJNZR0,EXIT
MOVR0,#20
MOVA,R1
MOVDPTR,#TAB
MOVCA,@A+DPTR
MOVP1,A
INCR1
CJNER1,#8,EXIT
MOVR1,#0
EXIT:
RETI
TAB:
DB0FFH,0FAH,0F5H,0AFH,05FH,0AAH,55H,0H,0FFH
END
10.开关控制LED的亮灭及速度
ORG0000H;程序从0000H开始执行
AJMPSTART;跳转到主程序
ORG0030H;以免覆盖中断地址
START:
MOVR4,#1;立即数传送到累加器
LOOP:
CLRP3.3;进位标志位清零
LOOP1:
JNBP3.5,LOOP;直接寻址位为0则转移
JBP3.4,LOOP2;直接寻址位为1则转移
JNBP3.4,$
INCR4;寄存器增1
LOOP2:
CPLP3.3;累加器求反
MOVA,R4;寄存器内容传送到累加器A
MOVR5,A;累加器内容传送到累加器
ACALLDELAY;调用延时函数
AJMPLOOP1;循环
DELAY:
MOVR6,#200
DEL:
MOVR7,#229
DJNZR7,$
DJNZR6,DEL;寄存器减1,不为0则转移
DJNZR5,DELAY
RET;子程序返回
END
11.计数器实验
ORG0000
SJMPSTART
ORG001BH
LJMPINT_1
START:
MOVSP,#60H
MOVR5,#20
MOVTMOD,#15H
MOVTH1,#4BH
MOVTL1,#0FDH
MOVTH0,#0
MOVTL0,#0
SETBET1
SETBEA
SETBTR1
SETBTR0
MOVDPTR,#0E100H
MOVA,#3
MOVX@DPTR,A
LOOP:
MOVR0,#50H;
MOVR1,#4
MOVR2,#1
LCALLDISPLAY
SJMPLOOP
INT_1:
MOVTH1,#4BH
MOVTL1,#0FDH
DJNZR5,CON
MOVR5,#20
PUSHACC
PUSHB
MOVA,TL0
MOVB,#10H
DIVAB
MOV50H,B
MOV51H,A
MOVA,TH0
MOVB,#10H
DIVAB
MOV52H,B
MOV53H,A
MOVTH0,#0
MOVTL0,#0
POPB
POPACC
CON:
RETI
DISPLAY:
MOVA,@R0
MOV0FH,#8
ACALLP164
ACALLPBIT
ACALLDELAY
MOVA,#0
MOVX@DPTR,A
INCR0
DJNZR1,DISPLAY
RET
P164:
MOVDPTR,#TAB
MOVCA,@A+DPTR
P164_1:
RRCA
MOVR3,A
MOVACC.0,C
ANLA,#0FDH
MOVDPTR,#0E102H
MOVX@DPTR,A
ORLA,#0FEH
MOVX@DPTR,A
MOVA,R3
DJNZ0FH,P164_1
MOV0FH,#8
RET
PBIT:
MOVDPTR,#0E101H
MOVA,R2
MOVX@DPTR,A
RLA
MOVR2,A
RET
DELAY:
MOVR6,#4
DEL:
MOVR7,#250
DJNZR7,$
DJNZR6,DEL
RET
TAB:
DB0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H
DB0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH
END
12.串并转换实验
ORG0000
SJMPSTART
ORG0030H
START:
MOVSP,#60H
MOVR1,#0
MOVDPTR,#TAB
LOOP:
MOVA,#0
CLRC
MOVR4,#24
ACALLS164
MOVA,R1
MOVCA,@A+DPTR
MOVR4,#8
ACALLS164
INCR1
MOVR5,#10
ACALLDELAY
CJNER1,#10,EXIT
MOVR1,#0
EXIT:
SJMPLOOP
S164:
RRCA
MOVP1.0,C
CLRP1.1
SETBP1.1
DJNZR4,S164
RET
DELAY:
MOVR6,#200
DEL:
MOVR7,#230
DJNZR7,$
DJNZR6,DEL
DJNZR5,DELAY
RET
TAB:
DB0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H,0FEH,0F6H
END
13.直流电机速度检测
ORG0000H
AJMPMAIN
ORG001BH
AJMPINT_1
ORG0030H
MAIN:
MOVSP,#60H
MOVTMOD,#15H
MOVTH1,#4BH
MOVTL1,#0FDH
MOVR0,#20
MOVTH0,#0
MOVTL0,#0
SETBET1
SETBEA
SETBTR0
SETBTR1
MOVR2,#00H
UP:
MOVA,R2
MOVDPTR,#0A000H//注意片选接YC2,非YC3
MOVX@DPTR,A
MOVR5,#1
ACALLDELAY;100ms
INCR2
CJNER2,#0FFH,UP
DOWN:
MOVA,R2
MOVDPTR,#0A000H
MOVX@DPTR,A
MOVR5,#1
ACALLDELAY;100ms
DECR2
CJNER2,#00H,DOWN
AJMPUP
INT_1:
MOVTH1,#4BH
MOVTL1,#0FDH
DJNZR0,EXIT
MOVR0,#20
PUSHACC
PUSHDPH
PUSHDPL
MOVA,TL0
ACALLXS
MOVTL0,#0
MOVA,TH0
ACALLXS
MOVTH0,#0
POPDPL
POPDPH
POPACC
EXIT:
RETI
XS:
MOVDPTR,#TAB
;MOVA,R2
MOVB,#10;显示10进制数
DIVAB
XCHA,B
;MOVDPTR,#TAB
MOVCA,@A+DPTR
ACALLFS
XCHA,B
;MOVDPTR,#TAB
MOVCA,@A+DPTR
ACALLFS
RET
FS:
MOVR1,#8
YW:
RRCA
MOVP1.0,C
CLRP1.1
SETBP1.1
DJNZR1,YW
RET
DELAY:
MOVR6,#200
DEL:
MOVR7,#230
DJNZR7,$
DJNZR6,DEL
DJNZR5,DELAY
RET
TAB:
DB0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H
DB0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EH
END
14.8255PB外接8个开关,编程将开关状态显示在数码管上(串并转换动态扫描方式)
ORG0000
AJMPSTART
ORG0030H
START:
MOVDPTR,#0AC03H
MOVA,#82H
MOVX@DPTR,A
MOVDPTR,#0E100H
MOV