51单片机实验及代码.docx
- 文档编号:29565458
- 上传时间:2023-07-24
- 格式:DOCX
- 页数:18
- 大小:16.87KB
51单片机实验及代码.docx
《51单片机实验及代码.docx》由会员分享,可在线阅读,更多相关《51单片机实验及代码.docx(18页珍藏版)》请在冰豆网上搜索。
51单片机实验及代码
单片机实验
实验一:
开发环境的应用
(1)将数据存储器的40H-4FH的内容复制成00H-0FH,并将40H-4FH的内容整体复制到外部数据存储器的1050H-105FH.
(2)P1口接八只二极管,使二极管逐个循环点亮
ORG0000H
AJMPSTART
ORG0100H
START:
MOVA,#00H
MOVR0,#40H
LOOP:
MOV@R0,A
INCA
INCR0
CJNEA,#10H,LOOP
SECOND:
MOVDPTR,#1050H
MOVR1,#40H
COPY:
MOVA,@R1
MOVX@DPTR,A
INCDPTR
INCR1
CJNER1,#50H,COPY
SJMP$
END
实验二:
外部中断
(1)用单脉冲信号申请中断,在中断服务程序中对输出信号进行翻转(可通过P1.0接一二极管观察)
ORG0000H
LJMPSTART
ORG0003H
LJMPINT
ORG0100H
START:
SETBEX0
SETBPX0
SETBIT0
SETBEA
SJMP$
INT:
CPLP1.0
RETI
END
(2)用单脉冲信号申请中断,要求对中断进行累加计数,并将计数结果显示在二极管上
ORG0000H
LJMPSTART
ORG0003H
LJMPINT
ORG0100H
START:
MOVA,#00H
MOVP1,A
SETBEX0
SETBPX0
SETBIT0
SETBEA
SJMP$
INT:
MOVP1,A
INCA
RETI
END
实验三:
定时器实验
(1)用定时器1的方式1定时,P1.0节发光二极管,使二极管每秒闪烁五次
ORG0000H
LJMPMAIN
ORG001BH
LJMPINT
ORG0100H
MAIN:
MOVTMOD,#10H
MOVTH1,#3CH
MOVTL1,#0B0H;50ms×2=100ms
MOVR2,#2
SETBEA
SETBET1
SETBTR1
SJMP$
INT:
DJNZR2,NEXT
CPLP1.0
MOVR2,#2
NEXT:
MOVTH1,#3CH
MOVTL1,#0B0H
SETBTR1
RETI
END
(2)用定时器1的方式2计数,T1引脚接单脉冲,用P1口接二极管显示计数。
ORG0000H
LJMPMAIN
ORG001BH
LJMPLOOP
ORG0100H
MAIN:
MOVTMOD,#60H
MOVTH1,#00H
MOVTL1,#00H
SETBEA
SETBET1
SETBTF1
SETBTR1
SJMP$
LOOP:
MOVP1,TL1
LJMPLOOP
RETI
END
实验四:
串行通信
波特率2400bit/s。
PC发送8个字节的数据给单片机,单片机接受完数据后发送两个字节55H和AAH给PC机(使用查询方式)。
ORG0000H
AJMPMAIN
ORG0030H
MAIN:
MOVSCON,#50H
MOVPCON,#00H
MOVTMOD,#20H
SETBTR1
MOVTL1,#0F3H
MOVTH1,#0F3H
LOOP:
MOVR0,#30H
MOVR1,#08H
WAIT1:
JBCRI,RECEIVE
SJMPWAIT1
RECEIVE:
MOVA,SBUF
MOV@R0,A
INCR0
DJNZR1,WAIT1
MOVA,#55H
MOVSBUF,A
WAIT21:
JBCTI,SEND
SJMPWAIT21
SEND:
MOVA,#0AAH
MOVSBUF,A
WAIT22:
JNBTI,WAIT22
CLRTI
SJMPLOOP
END
实验五:
数码管显示及键盘扫描
(1)将30H-32H中存储的6位十进制数在6个数码管上显示出来(要求编写一个将内存单元中的压缩BCD码转换为两个七段显示码的子程序和延时子程序)
ORG0000H
LJMPMAIN
ORG0030H
MAIN:
MOV30H,#12H
MOV31H,#34H
MOV32H,#56H
MOVR0,#30H
MOVR1,#20H
BCD:
MOVA,@R0
SWAPA
ANLA,#0FH
ACALLCTAB
MOVR2,A
MOVA,@R0
ANLA,#0FH
ACALLCTAB
MOVR3,A
MOVDPTR,#8002H
MOVA,R1
MOVX@DPTR,A
MOVDPTR,#8004H
MOVA,R2
MOVX@DPTR,A
MOVA,R1
RRA
MOVR1,A
LCALLDLEY
MOVDPTR,#8002H
MOVA,R1
MOVX@DPTR,A
MOVDPTR,#8004H
MOVA,R3
MOVX@DPTR,A
MOVA,R1
RRA
MOVR1,A
LCALLDLEY
INCR0
CJNER0,#33H,BCD
LJMPMAIN
CTAB:
PUSHDPH
PUSHDPL
MOVDPTR,#TAB
MOVCA,@A+DPTR
POPDPL
POPDPH
RET
TAB:
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH,39H,5EH,79H,71H
DLEY:
MOVR7,#02H
DL:
MOVR6,#0FFH
DL1:
DJNZR6,DL1
DJNZR7,DL
RET
END
(2)扫描矩阵键盘,将对应键盘值送LED显示
ORG0000H
LJMPSTART
ORG1000H
START:
ACALLKEY1
AJMPSTART
KEY1:
ACALLKEY14
JNZKEY3
KEY2:
ACALLKEY16
AJMPKEY1
KEY3:
ACALLKEY16
ACALLDELAY
ACALLKEY14
JNZKEY4
ACALLKEY16
AJMPKEY1
KEY4:
MOVR2,#0FEH
MOVR4,#00H
KEY5:
MOVDPTR,#8002H
MOVA,R2
MOVX@DPTR,A
MOVDPTR,#8001H
MOVXA,@DPTR
JBACC.0,KEY6
MOVA,#00H
AJMPKEY9
KEY6:
JBACC.1,KEY7
MOVA,#06H
AJMPKEY9
KEY7:
JBACC.2,KEY8
MOVA,#0CH
AJMPKEY9
KEY8:
JBACC.3,KEY12
MOVA,#12H
KEY9:
ADDA,R4
KEY10:
MOVR5,A
KEY11:
ACALLKEY14
JNZKEY11
MOVA,R5
ACALLKEY15
RET
KEY12:
INCR4
MOVA,R2
JNBACC.5,KEY13
RLA
MOVR2,A
AJMPKEY5
KEY13:
AJMPKEY1
KEY14:
MOVDPTR,#8002H
MOVA,#00H
MOVX@DPTR,A
MOVDPTR,#8001H
MOVXA,@DPTR
CPLA
ANLA,#0FH
RET
KEY15:
MOVDPTR,#TABLE
MOVCA,@A+DPTR
MOVR3,A
KEY16:
MOVDPTR,#8002H
MOVA,#01H
MOVX@DPTR,A
MOVDPTR,#8004H
MOVA,R3
MOVX@DPTR,A
ACALLDELAY
RET
DELAY:
MOVR7,#12
LOOP:
MOVR6,#250
DJNZR6,$
DJNZR7,LOOP
RET
TABLE:
DB7EH,7EH,5EH,79H,71H,3FH,7EH,7EH,39H,4FH,5BH,06H,7EH,7EH,7CH,7DH,6DH,66H,7EH,7EH,77H,6FH,7FH,07H
END
实验六:
D/A和A/D
(1)利用实验箱上的电位器提供模拟量输入,将模拟量转换成二进制数字量,将转换的结果用P1口输出到二极管显示;
ORG0000H
LJMPMAIN
ORG0100H
MAIN:
MOVDPTR,#8000H
MOVA,#00H
MOVX@DPTR,A
MOVR2,#100
DJNZR2,$
MOVXA,@DPTR
MOVP1,A
LJMPMAIN
END
(2)用DAC0832将一个数值转换为模拟电压,该模拟电压接ADC0809的一个模拟输入进行模/数转换,转换结果通过P1口送到二极管显示。
ORG0000H
LJMPMAIN
ORG0100H
MAIN:
MOVA,#90H
MOVDPTR,#9000H
MOVX@DPTR,A
MOVDPTR,#8000H
MOVX@DPTR,A
MOVR2,#64H
DJNZR2,$
MOVXA,@DPTR
MOVP1,A
SJMP$
END
实验七:
电子钟
实现一个24小时制的电子钟程序,在6个数码管上显示时分秒(用定时器0中断更新计时,以压缩BCD码形式保存在内部RAM的30H、31H和32H单元);可以通过串行口调时!
ORG0000H
AJMPMAIN
ORG000BH
LJMPCLOCK
ORG0023H
LJMPINPUT
ORG0100H
MAIN:
MOVTMOD,#21H
CLRA
MOV30H,A
MOV31H,A
MOV32H,A
MOVTH0,#3CH
MOVTL0,#0B0H
MOV50H,#14H;50*20
MOVTH1,#0F3H
MOVTL1,#0F3H;波特率
MOVSCON,#50H
SETBPS
MOVIE,#92H
SETBTR1
SETBTR0
SLED:
MOVR0,#30H
MOVR1,#20H
LJMPLED
CLOCK:
MOVTH0,#3CH
MOVTL0,#0B0H
DJNZ50H,RETURN
MOV50H,#14H
MOVA,#01H
ADDA,32H
DAA
MOV32H,A
CJNEA,#60H,RETURN
MOV32H,#00H
MOVA,#01H
ADDA,31H
DAA
MOV31H,A
CJNEA,#60H,RETURN
MOV31H,#00H
MOVA,#01H
ADDA,30H
DAA
MOV30H,A
CJNEA,#24H,RETURN
MOV30H,#00H
RETURN:
RETI
LED:
MOVA,@R0
SWAPA
ANLA,#0FH
ACALLCTAB
MOVR2,A
MOVA,@R0
ANLA,#0FH
LCALLCTAB
MOVR3,A
MOVDPTR,#8002H
MOVA,R1
MOVX@DPTR,A
MOVDPTR,#8004H
MOVA,R2
MOVX@DPTR,A
MOVA,R1
RRA
MOVR1,A
LCALLDLY
MOVDPTR,#8002H
MOVA,R1
MOVX@DPTR,A
MOVDPTR,#8004H
MOVA,R3
MOVX@DPTR,A
MOVA,R1
RRA
MOVR1,A
LCALLDLY
INCR0
CJNER0,#33H,LED
LJMPSLED
CTAB:
PUSHDPH
PUSHDPL
MOVDPTR,#TAB
MOVCA,@A+DPTR
POPDPL
POPDPH
RET
INPUT:
CLRRI
CLREA
MOV30H,SBUF
JNBRI,$
CLRRI
MOV31H,SBUF
JNBRI,$
CLRRI
MOV32H,SBUF
SETBEA
RETI
TAB:
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
DLY:
MOVR7,#02H
DL:
MOVR6,#0FFH
DL1:
DJNZR6,DL1
DJNZR7,DL
RET
END
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 实验 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)