单片机实训程序.docx
《单片机实训程序.docx》由会员分享,可在线阅读,更多相关《单片机实训程序.docx(13页珍藏版)》请在冰豆网上搜索。
单片机实训程序
一、工业控制
ORG0000H
LJMPHA2S
ORG0013H
LJMPHA2S3
ORG0580H
HA2S:
MOVP1,#00H
ORLP3,#00H
HA2S1:
JNBP3.4,HA2S1
ORLIE,#84H
ORLIP,#04H
MOVPSW,#00H
MOVSP,#53H
HA2S2:
MOVP1,#01H
ACALLHA2S7
MOVP1,#04H
ACALLHA2S7
MOVP1,#08H
ACALLHA2S7
MOVP1,#10H
ACALLHA2S7
MOVP1,#20H
ACALLHA2S7
MOVP1,#40H
ACALLHA2S7
SJMPHA2S2
HA2S3:
MOVB,R2
HA2S4:
MOVP1,#00H
MOV20H,#0A0H
HA2S5:
SETBP1.7
ACALLHA2S6
CLRP1.7
ACALLHA2S6
DJNZ20H,HA2S5
CLRP1.7
ACALLHA2S6
JNBP3.3,HA2S4
MOVR2,B
RETI
HA2S6:
MOVR2,#06H
ACALLDELAY
RET
HA2S7:
MOVR2,#30H
ACALLDELAY
RET
DELAY:
PUSH02H
DELAY1:
PUSH02H
DELAY2:
PUSH02H
DELAY3:
DJNZR2,DELAY3
POP02H
DJNZR2,DELAY2
POP02H
DJNZR2,DELAY1
POP02H
DJNZR2,DELAY
RET
END
P1,P3口的应用
ORG1000H
MOVA,#00H
MOVP1,A
LOOP:
JBP3.3,$
JNBP3.3,$
INCA
MOVP1,A
AJMPLOOP
END
二、多字节加法
ORG1000H
MOVR0,#32H
MOVR1,#42H
MOVR7,#03H
CLRC
LOOP:
MOVA,@R0
ADDCA,@R1
DAA
MOV@R0,A
DECR0
DECR1
DJNZR7,LOOP
MOV7FH,C
SJMP$
END
三、拼字程序
ORG0100H
MOVDPTR,#7000H
MOVXA,@DPTR
ANLA,#0FH
SWAPA
MOVR0,A
INCDPTR
MOVXA,@DPTR
ANLA,#0FH
ADDA,R0
INCDPTR
MOVX@DPTR,A
END
四、数据排序
ORG1000H
MAIN:
MOVR0,#30H
MOVR7,#09H
CLR7FH
LOOP2:
MOV20H,@R0
MOVA,20H
INCR0
MOV21H,@R0
CJNEA,21H,LOOP
LOOP:
JCLOOP1
MOV@R0,A
DECR0
MOV@R0,21H
INCR0
SETB7FH
LOOP1:
DJNZR7,LOOP2
JB7FH,MAIN
SJMP$
END
五、多分支程序
MOVA,20H
RLA
MOVDPTR,#TAB
JMP@A+DPTR
TAB:
AJMPROUT0
AJMPROUT1
AJMPROUT2
ROUT0:
MOVA,#00H
SJMP$
ROUT1:
MOVA,#01H
SJMP$
ROUT2:
MOVA,#02H
END
六、数据传送互换
ORG1000H
MOVR0,#30H
MOVDPTR,#7000H
LOOP1:
CJNE@R0,#0DH,LOOP
SJMP$
LOOP:
MOVXA,@DPTR
MOV20H,@R0
MOV@R0,A
MOVA,20H
MOVX@DPTR,A
INCR0
INCDPTR
SJMPLOOP1
END
七、16进制转为ASCII
ORG1000H
MOVR0,#30H
MOVR1,#40H
MOVR7,#03H
LOOP1:
MOVA,@R0
ANLA,#0FH
CJNEA,#10,LOOP
LOOP:
JNCLOOP2
ADDA,#30H
MOV@R1,A
AJMPLOOP3
LOOP2:
ADDA,#37H
MOV@R1,A
AJMPLOOP3
LOOP3:
INCR0
INCR1
DJNZR7,LOOP1
SJMP$
END
八、拆字程序
ORG0000H
MOVDPTR,#7000H
MOVXA,@DPTR
SWAPA
ANLA,#0FH
INCDPTR
MOVX@DPTR,A
MOVDPTR,#7000H
MOVXA,@DPTR
ANLA,#0FH
INCDPTR
INCDPTR
MOVX@DPTR,A
SJMP$
END
九、交通灯控制程序
ORG0630H
HA4S:
MOVSP,#60H
MOVDPTR,#0FF2BH
MOVA,#80H
MOVX@DPTR,A
MOVDPTR,#0FF29H
MOVA,#49H
MOVX@DPTR,A
INCDPTR
MOVA,#49H
MOVX@DPTR,A
MOVR2,#25H
LCALLDELAY
HA4S3:
MOVDPTR,#0FF29H
MOVA,#08H
MOVX@DPTR,A
INCDPTR
MOVA,#61H
MOVX@DPTR,A
MOVR2,#55H
LCALLDELAY
MOVR7,#05H
HA4S1:
MOVDPTR,#0FF29H
MOVA,#04H
MOVX@DPTR,A
INCDPTR
MOVA,#51H
MOVX@DPTR,A
MOVR2,#20H
LCALLDELAY
MOVDPTR,#0FF29H
MOVA,#00H
MOVX@DPTR,A
INCDPTR
MOVA,#41H
MOVX@DPTR,A
MOVR2,#20H
LCALLDELAY
DJNZR7,HA4S1
MOVDPTR,#0FF29H
MOVA,#03H
MOVX@DPTR,A
INCDPTR
MOVA,#0cH
MOVX@DPTR,A
MOVR2,#55H
LCALLDELAY
MOVR7,#05H
HA4S2:
MOVDPTR,#0FF29H
MOVA,#02H
MOVX@DPTR,A
INCDPTR
MOVA,#8aH
MOVX@DPTR,A
MOVR2,#20H
LCALLDELAY
MOVDPTR,#0FF29H
MOVA,#02H
MOVX@DPTR,A
INCDPTR
MOVA,#08H
MOVX@DPTR,A
MOVR2,#20H
LCALLDELAY
DJNZR7,HA4S2
LJMPHA4S3
DELAY:
PUSH02H
DELAY1:
PUSH02H
DELAY2:
PUSH02H
DELAY3:
DJNZR2,DELAY3
POP02H
DJNZR2,DELAY2
POP02H
DJNZR2,DELAY1
POP02H
DJNZR2,DELAY
RET
END
十、定时器中断
ORG000BH
LJMPTIP
ORG0100H
MAIN:
MOVR7,#14H
MOVA,#00H
MOVP1,A
MOVTMOD,#01H
MOVTH0,#3CH
MOVTL0,#0BH
SETBEA
SETBET0
SETBTR0
SJMP$
TIP:
DJNZR7,TIP1
INCA
MOVP1,A
MOVR7,#14H
TIP1:
MOVTH0,#3CH
MOVTL0,#0B0H
RETI
END
十一、串并转换
TIMEREQU01H
org0000h
ljmpstart
ORG000BH
AJMPINT_T0
ORG0790H
START:
MOVSP,#53H
MOVTMOD,#01H
MOVTL0,#00H
MOVTH0,#4BH
MOVR0,#0H
MOVTIMER,#20
MOVSCON,#00H
CLRTI
CLRRI
SETBTR0
SETBET0
SETBEA
SJMP$
INT_T0:
PUSHACC
PUSHPSW
CLREA
CLRTR0
MOVTL0,#0H
MOVTH0,#4BH
SETBTR0
DJNZTIMER,EXIT
MOVTIMER,#20
MOVDPTR,#TAB
MOVA,R0
MOVCA,@A+DPTR
CLRTI
CPLA
MOVSBUF,A
INCR0
jnbti,$
clrti
CJNER0,#0AH,EXIT
MOVR0,#0H
EXIT:
SETBEA
POPPSW
POPACC
RETI
TAB:
DB03H,9FH,25H,0DH,99H,49H,41H,1FH,01H,09H
END
A/D转换
ORG06D0H
START:
MOVA,#00H显示器初值
MOVDPTR,#9000H0809的地址
MOVX@DPTR,A启动A/D转换地址
JNBP3.3,$判断转换结束与否,EOC=1结束
MOVSBUF,A
MOVSBUF,A显示00
MOVXA,@DPTR读取转换结果
DISP:
MOVR0,A
ANLA,#0FH
LP:
MOVDPTR,#TAB表头送DPTR
MOVCA,@A+DPTR取字模
MOVSBUF,A显示低四位
MOVR7,#0FH延时
H55S:
DJNZR7,H55S
MOVA,R0
SWAPA
ANLA,#0FH
MOVCA,@A+DPTR取字模
MOVSBUF,A显示高四位
MOVR7,#0FH
H55S1:
DJNZR7,H55S1
LCALLDELAY
AJMPSTART
TAB:
DB0fch,60h,0dah,0f2h,66h,0b6h,0beh,0e0h
DB0feh,0f6h,0eeh,3eh,9ch,7ah,9eh,8eh
DELAY:
MOVR6,#0FFh
DELY2:
MOVR7,#0FFh
DELY1:
DJNZR7,DELY1
DJNZR6,DELY2
RET
END
D/A转换
ORG0740H
HA6S:
MOVSP,#53H
HA6S1:
MOVR6,#00H转换初值
HA6S2:
MOVDPTR,#8000H0832地址
MOVA,R6
MOVX@DPTR,AD/A转换
MOVR2,#0BH
LCALLDELAY延时
INCR6转换值增加
CJNER6,#0FFH,HA6S2判断是否到最高电压
HA6S3:
MOVDPTR,#8000H
DECR6转换值减小
MOVA,R6
MOVX@DPTR,A
MOVR2,#0BH
LCALLDELAY
CJNER6,#00H,HA6S3判断是否到最低电压
SJMPHA6S1
DELAY:
PUSH02H
DELAY1:
PUSH02H
DELAY2:
PUSH02H
DELAY3:
DJNZR2,DELAY3
POP02H
DJNZR2,DELAY2
POP02H
DJNZR2,DELAY1
POP02H
DJNZR2,DELAY
RET
END