《最新51单片机实验及代码.doc》由会员分享,可在线阅读,更多相关《最新51单片机实验及代码.doc(23页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品资料51单片机实验及代码.单片机实验实验一:开发环境的应用(1) 将数据存储器的40H-4FH的内容复制成00H-0FH,并将40H-4FH的内容整体复制到外部数据存储器的1050H-105FH.(2) P1口接八只二极管,使二极管逐个循环点亮ORG 0000HAJMP STARTORG 0100HSTART: MOV A,#00H MOV R0,#40HLOOP: MOV R0,A INC A INC R0 CJNE A,#10H,LOOPSECOND: MOV DPTR,#1050H MOV R1,#40HCOPY: MOV A,R1 MOVX DPTR,A INC DPTR INC
2、R1 CJNE R1,#50H,COPY SJMP $ END实验二:外部中断(1) 用单脉冲信号申请中断,在中断服务程序中对输出信号进行翻转(可通过P1.0接一二极管观察)ORG 0000HLJMP STARTORG 0003HLJMP INTORG 0100HSTART: SETB EX0 SETB PX0 SETB IT0 SETB EA SJMP $INT: CPL P1.0 RETI END(2) 用单脉冲信号申请中断,要求对中断进行累加计数,并将计数结果显示在二极管上ORG 0000HLJMP STARTORG 0003HLJMP INTORG 0100HSTART: MOV A,
3、#00H MOV P1,A SETB EX0 SETB PX0 SETB IT0 SETB EA SJMP $INT: MOV P1,A INC A RETI END实验三:定时器实验(1) 用定时器1的方式1定时,P1.0节发光二极管,使二极管每秒闪烁五次ORG 0000HLJMP MAINORG 001BHLJMP INTORG 0100HMAIN: MOV TMOD,#10H MOV TH1,#3CH MOV TL1,#0B0H ;50ms 2 = 100ms MOV R2,#2 SETB EA SETB ET1 SETB TR1 SJMP $INT: DJNZ R2,NEXT CPL
4、P1.0 MOV R2,#2NEXT: MOV TH1,#3CH MOV TL1,#0B0H SETB TR1 RETI END(2) 用定时器1的方式2计数,T1引脚接单脉冲,用P1口接二极管显示计数。ORG 0000HLJMP MAINORG 001BHLJMP LOOPORG 0100HMAIN: MOV TMOD,#60H MOV TH1,#00H MOV TL1,#00H SETB EA SETB ET1 SETB TF1 SETB TR1 SJMP $LOOP: MOV P1,TL1 LJMP LOOP RETI END实验四:串行通信波特率2400 bit/s。PC发送8个字节的
5、数据给单片机,单片机接受完数据后发送两个字节55H和AAH给PC 机(使用查询方式)。ORG 0000HAJMP MAINORG 0030HMAIN:MOV SCON,#50HMOV PCON,#00HMOV TMOD,#20HSETB TR1MOVTL1,#0F3HMOV TH1,#0F3HLOOP: MOV R0,#30HMOV R1,#08HWAIT1: JBC RI,RECEIVESJMP WAIT1RECEIVE: MOV A,SBUFMOV R0,AINC R0DJNZ R1,WAIT1MOV A,#55HMOV SBUF,AWAIT21: JBC TI,SENDSJMP WAIT
6、21SEND: MOV A,#0AAHMOV SBUF,AWAIT22: JNB TI,WAIT22CLR TISJMP LOOPEND实验五:数码管显示及键盘扫描(1) 将30H-32H中存储的6位十进制数在6个数码管上显示出来(要求编写一个将内存单元中的压缩BCD码转换为两个七段显示码的子程序和延时子程序)ORG 0000HLJMP MAINORG 0030HMAIN: MOV 30H,#12H MOV 31H,#34H MOV 32H,#56H MOV R0,#30H MOV R1,#20HBCD: MOV A,R0 SWAP A ANL A,#0FH ACALL CTAB MOV R2
7、,A MOV A,R0 ANL A,#0FH ACALL CTAB MOV R3,A MOV DPTR,#8002H MOV A,R1 MOVX DPTR,A MOV DPTR,#8004H MOV A,R2 MOVX DPTR,A MOV A,R1 RR A MOV R1,A LCALL DLEY MOV DPTR,#8002H MOV A,R1 MOVX DPTR,A MOV DPTR,#8004H MOV A,R3 MOVX DPTR,A MOV A,R1 RR A MOV R1,A LCALL DLEY INC R0 CJNE R0,#33H,BCD LJMP MAINCTAB: PU
8、SH DPH PUSH DPL MOV DPTR,#TAB MOVC A,A+DPTR POP DPL POP DPH RETTAB: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH,39H,5EH,79H,71HDLEY: MOV R7,#02H DL:MOV R6,#0FFH DL1:DJNZ R6,DL1 DJNZ R7,DL RET END(2) 扫描矩阵键盘,将对应键盘值送LED显示 ORG 0000H LJMP START ORG 1000HSTART: ACALL KEY1 AJMP STARTKEY1: ACALL KEY1
9、4 JNZ KEY3KEY2: ACALL KEY16 AJMP KEY1 KEY3: ACALL KEY16 ACALL DELAY ACALL KEY14 JNZ KEY4 ACALL KEY16 AJMP KEY1KEY4: MOV R2, #0FEH MOV R4, #00HKEY5: MOV DPTR,#8002H MOV A, R2 MOVX DPTR, A MOV DPTR,#8001H MOVX A,DPTR JB ACC.0,KEY6 MOV A, #00H AJMP KEY9KEY6: JB ACC.1,KEY7 MOV A, #06H AJMP KEY9 KEY7: JB
10、 ACC.2,KEY8 MOV A, #0CH AJMP KEY9KEY8: JB ACC.3, KEY12 MOV A, #12H KEY9: ADD A, R4 KEY10: MOV R5,AKEY11: ACALL KEY14 JNZ KEY11 MOV A,R5 ACALL KEY15 RET KEY12: INC R4 MOV A, R2 JNB ACC.5,KEY13 RL A MOV R2, A AJMP KEY5 KEY13: AJMP KEY1 KEY14: MOV DPTR,#8002H MOV A,#00H MOVX DPTR,A MOV DPTR,#8001H MOVX
11、 A,DPTR CPL A ANL A, #0FH RETKEY15: MOV DPTR,#TABLE MOVC A,A+DPTR MOV R3,AKEY16: MOV DPTR,#8002H MOV A,#01H MOVX DPTR,A MOV DPTR,#8004H MOV A,R3 MOVX DPTR,A ACALL DELAY RETDELAY: MOV R7,#12LOOP: MOV R6,#250 DJNZ R6,$ DJNZ R7,LOOP RETTABLE: DB 7EH,7EH,5EH,79H,71H,3FH,7EH,7EH,39H,4FH,5BH,06H,7EH,7EH,7
12、CH,7DH,6DH,66H,7EH,7EH,77H,6FH,7FH,07H END实验六:D/A和A/D(1) 利用实验箱上的电位器提供模拟量输入,将模拟量转换成二进制数字量,将转换的结果用P1口输出到二极管显示;ORG 0000HLJMP MAINORG 0100HMAIN: MOV DPTR,#8000HMOV A,#00HMOVX DPTR,AMOV R2,#100DJNZ R2,$MOVX A,DPTRMOV P1,ALJMP MAINEND(2) 用DAC0832将一个数值转换为模拟电压,该模拟电压接ADC0809的一个模拟输入进行模/数转换,转换结果通过P1口送到二极管显示。OR
13、G 0000HLJMP MAINORG 0100HMAIN: MOV A,#90HMOV DPTR,#9000HMOVX DPTR,AMOV DPTR,#8000HMOVX DPTR,AMOV R2,#64HDJNZ R2,$MOVX A,DPTRMOV P1,ASJMP $END实验七:电子钟实现一个24小时制的电子钟程序,在6个数码管上显示时分秒(用定时器0中断更新计时,以压缩BCD码形式保存在内部RAM的30H、31H和32H单元);可以通过串行口调时!ORG 0000HAJMP MAINORG 000BHLJMP CLOCKORG 0023HLJMP INPUTORG 0100HMAI
14、N: MOV TMOD,#21H CLR AMOV 30H,AMOV 31H,AMOV 32H,AMOV TH0,#3CHMOV TL0,#0B0HMOV 50H,#14H ;50 * 20MOV TH1,#0F3HMOV TL1,#0F3H ;波特率MOV SCON,#50HSETB PSMOV IE,#92HSETB TR1SETB TR0SLED: MOV R0,#30HMOV R1,#20HLJMP LEDCLOCK:MOV TH0,#3CHMOV TL0,#0B0HDJNZ 50H,RETURNMOV 50H,#14HMOV A,#01HADD A,32HDA AMOV 32H,AC
15、JNE A,#60H,RETURNMOV 32H,#00HMOV A,#01HADD A,31HDA AMOV 31H,ACJNE A,#60H,RETURNMOV 31H,#00HMOV A,#01HADD A,30HDA AMOV 30H,ACJNE A,#24H,RETURNMOV 30H,#00HRETURN:RETILED: MOV A,R0SWAP AANL A,#0FHACALL CTABMOV R2,AMOV A,R0ANL A,#0FHLCALL CTABMOV R3,AMOV DPTR,#8002HMOV A,R1MOVX DPTR,AMOV DPTR,#8004HMOV
16、A,R2MOVX DPTR,AMOV A,R1RR AMOV R1,ALCALL DLYMOV DPTR,#8002HMOV A,R1MOVX DPTR,AMOV DPTR,#8004HMOV A,R3MOVX DPTR,AMOV A,R1RR AMOV R1,ALCALL DLYINC R0CJNE R0,#33H,LEDLJMP SLEDCTAB: PUSH DPHPUSH DPLMOV DPTR,#TABMOVC A,A+DPTRPOP DPLPOP DPHRETINPUT:CLR RICLR EAMOV 30H,SBUFJNB RI,$CLR RIMOV 31H,SBUFJNB RI,$CLR RIMOV 32H,SBUFSETB EARETITAB: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FHDLY: MOV R7,#02H DL: MOV R6,#0FFHDL1: DJNZ R6,DL1DJNZ R7,DLRETEND