《单片机技术》实验讲义参考答案.docx
《《单片机技术》实验讲义参考答案.docx》由会员分享,可在线阅读,更多相关《《单片机技术》实验讲义参考答案.docx(56页珍藏版)》请在冰豆网上搜索。
《单片机技术》实验讲义参考答案
三.程序清单及程序流程框图
ORG0000H
LJMPMAIN
MAIN:
MOVR0,#30H
MOVR2,#10H
CLRA
A1:
MOV@R0,A
INCR0
INCA
DJNZR2,A1
MOVR0,#30H
MOVR1,#40H
MOVR2,#10H
A2:
MOVA,@R0
MOV@R1,A
INCR0
INCR1
DJNZR2,A2
MOVR1,#40H
MOVDPTR,#4800H
MOVR2,#10H
A3:
MOVA,@R1
MOVX@DPTR,A
INCR1
INCDPTR
DJNZR2,A3
MOVSP,#60H
MOVR2,#10H
MOVDPTR,#4800H
PUSHDPL
PUSHDPH
MOVDPTR,#5800H
MOVR3,DPL
MOVR4,DPH
A4:
POPDPH
POPDPL
MOVXA,@DPTR
INCDPTR
PUSHDPL
PUSHDPH
MOVDPL,R3
MOVDPH,R4
MOVX@DPTR,A
INCDPTR
MOVR3,DPL
MOVR4,DPH
DJNZR2,A4
MOVR0,#50H
MOVDPTR,#5800H
MOVR2,#10H
A5:
MOVXA,@DPTR
MOV@R0,A
INCR0
INCDPTR
DJNZR2,A5
POPDPH
POPDPL
HERE:
LJMPHERE
END
三.实验电路
四.程序清单及流程图
程序一
ORG0000H
LJMPMAIN
ORG000BH
LJMPIPTO
MAIN:
MOVSP,#30H
MOVTMOD,#01H
CLR00H
SETBEA
SETBET0
MOVTH0,#3CH
MOVTL0,#0B0H
MOVR1,#14H
SETBTR0
MOVA,#0feH
MOVP1,A
NT:
JNB00H,NT
RLA
MOVP1,A
CLR00H
LJMPNT
IPTO:
MOVTH0,#3CH
MOVTL0,#0B0H
DJNZR1,TIO
MOVR1,#14H
SETB00H
TIO:
RETI
END
程序二只需将程序一中“RLA”改为“RRA”即可实现其功能。
程序三
ORG0000H
LJMPMAIN
ORG000BH
LJMPIPTO
MAIN:
MOVSP,#30H
MOVTMOD,#01H
CLR00H
SETBEA
SETBET0
MOVTH0,#3CH
MOVTL0,#0B0H
MOVR2,#0AH
MOVR1,#02H
SETBTR0
MOVA,#0feH
MOVP1,A
NT:
SETBP3.0
NT0:
JNB01H,NT0
CLRP3.0
CLR01H
NT1:
JNB00H,NT1
CLR01H
RLA
MOVP1,A
CLR00H
LJMPNT
IPTO:
MOVTH0,#3CH
MOVTL0,#0B0H
DJNZR2,TIO
MOVR2,#0AH
SETB01H
DJNZR1,TIO
MOVR1,#02H
SETB00H
TIO:
RETI
END
程序四(思考题程序)
ORG0000H
LJMPMAIN
ORG000BH
LJMPIPTO
MAIN:
MOVSP,#30H
MOVP1,#0ffH
MOVTMOD,#01H
CLR00H
SETBEA
SETBET0
MOVTH0,#3CH
MOVTL0,#0B0H
MOVR1,#14H
SETBTR0
MOVA,#00H
NT:
JNB00H,NT
INCA
MOVR3,A
CPLA
MOVP1,A
CLR00H
MOVA,R3
LJMPNT
IPTO:
MOVTH0,#3CH
MOVTL0,#0B0H
DJNZR1,TIO
MOVR1,#14H
SETB00H
TIO:
RETI
END
三.电路原理图
四.程序清单及流程图
程序一ORG0000H
LJMPMAIN
MAIN:
MOVTMOD,#02H
MOVTH0,#9CH
MOVTL0,#9CH
SETBTR0
SETBP1.0
HERE:
JNBTF0,HERE
CPLP1.0
CLRTF0
LJMPHERE
END
程序二
ORG0000H
LJMPMAIN
ORG000BH
LJMPIPTO
MAIN:
MOVSP,#60H
MOVTMOD,#02H
MOVIE,#82H
MOVTH0,#88H
MOVTL0,#88H
SETBTR0
SETBP1.1
HERE:
LJMPHERE
IPTO:
CPLP1.1
RETI
END
程序三(思考题)
ORG0000H
LJMPMAIN
MAIN:
MOVTMOD,#11H
MOVTH0,#0FEH
MOVTL0,#0D4H
MOVTH1,#0FFH
MOVTL1,#38H
SETBTR1
SETBP1.0
HERE:
JNBTF1,HERE
CPLP1.0
SETBTR0
CLRTF1
CLRTR1
MOVTH1,#0FFH
MOVTL1,#38H
HERE1:
JNBTF0,HERE1
CPLP1.0
SETBTR1
CLRTF0
CLRTR0
MOVTH0,#0FEH
MOVTL0,#0D4H
LJMPHERE
END
五.实验结果
周期为200us方波周期为240us方波
周期为500us占空比为2:
5的方波
三、电路原理图
四、程序清单及流程图
程序1:
ORG0000H
LJMPMAIN
MAIN:
MOVTMOD,#06H
MOVTH0,#9CH
MOVTL0,#9CH
SETBTR0
HERE:
JNBTF0,HERE
CPLP1.0
CLRTF0
LJMPHERE
END
程序2:
ORG0000H
LJMPMAIN
ORG000BH
LJMPITPO
MAIN:
MOVSP,#60H
MOVTMOD,#06H
MOVTH0,#38H
MOVTL0,#38H
MOVIE,#82H
SETBTR0
HERE:
LJMPHERE
ITPO:
CPLP1.0
RETI
END
程序3(思考题):
ORG0000H
LJMPMAIN
ORG001BH
LJMPITPO
MAIN:
MOVSP,#60H
MOVTMOD,#62H
MOVTH0,#9CH
MOVTL0,#9CH
MOVTH1,#0CEH
MOVTL1,#0CEH
MOVIE,#88H
SETBTR0
SETBTR1
SETBP1.0
HERE:
JNBTF0,HERE
CPLP1.0
CLRTF0
LJMPHERE
ITPO:
CPLP1.1
RETI
END
五、实验结果
程序1与程序2从T0接入1000Hz的方波,思考题从P1.0口线产生的方波从T1接入
计100次脉冲计200次脉冲
思考题波形
三、电路原理图
四、程序清单及流程图
程序一
:
ORG0000H
LJMPMAIN
ORG0003H
LJMPEXTER0
ORG000BH
LJMPIPTO
ORG00013H
LJMPEXTER1
MAIN:
MOVSP,#60H
MOVTMOD,#11H
MOVIE,#87H
MOVIP,#02H
MOVTH0,#9EH
MOVTL0,#58H
MOVTH1,#0D8H
MOVTL1,#0F0H
HERE:
LJMPHERE
DELAY:
SETBTR1
HEREY:
JNBTF1,HEREY
CLRTR1
CLRTF1
MOVTH1,#0D8H
MOVTL1,#0F0H
RET
EXTER0:
LCALLDELAY
JBP1.0,LOOP0
HERE0:
JNBP1.0,HERE0
SETBTR0
LOOP0:
RETI
EXTER1:
LCALLDELAY
JBP1.1,LOOP1
HERE1:
JNBP1.1,HERE1
CLRTR0
LOOP1:
RETI
IPTO:
CPLP1.2
MOVTH0,#9EH
MOVTL0,#58H
RETI
END
程序二(思考题)
ORG0000H
LJMPMAIN
ORG0003H
LJMPEXTER0
ORG000BH
LJMPIPTO
MAIN:
MOVSP,#60H
MOVTMOD,#11H
MOVIE,#83H
MOVIP,#02H
MOVTH0,#0B1H
MOVTL0,#0E0H
MOVTH1,#0D8H
MOVTL1,#0F0H
MOVR1,#02H
HERE:
LJMPHERE
DELAY:
SETBTR1
HEREY:
JNBTF1,HEREY
CLRTR1
CLRTF1
MOVTH1,#0D8H
MOVTL1,#0F0H
RET
EXTER0:
LCALLDELAY
JBP1.0,LOOP0
HERE0:
JNBP1.0,HERE0
CPLTR0
OOP0:
RETI
IPTO:
DJNZR1,LOOP
CPLP1.2
CPL00H
JNB00H,LOOP2
MOVR1,#03H
LJMPLOOP
LOOP2:
MOVR1,#02H
LOOP:
MOVTH0,#0B1H
MOVTL0,#0E0H
RETI
END
五、实验结果
1、基本要求部分:
将程序产生的十六进制代码加载到proteus中,运行仿真开始无波形输出,按下K0键输出周期为50mS的连续方波,如图5.1所示,当按下K1键波形消失,重复操作结果一样。
2、思考题部分:
将程序产生的十六进制代码加载到proteus中,运行仿真开始无波形输出,按下K0键输出周期为100mS占空比为2:
5的连续矩形波,如图5.2所示,当再次按下K0键波形消失,重复操作结果一样。
图5.1图5.2
三、电路原理图
四、程序清单及流程图
程序一:
ORG0000H
LJMPMAIN
ORG000BH
LJMPINTO
MAIN:
LCALLDELY
MOVSP,#60H
MOVTMOD,#02H
MOVIE,#82H
MOVTH0,#9CH
MOVTL0,#9CH
MOVDPTR,#7FFFH
MOVA,#81H
MOVX@DPTR,A
MOVR3,#0FFH
SETBTR0
LOOP0:
MOVDPTR,#7FFEH
MOVXA,@DPTR
SWAPA
MOVX@DPTR,A
LJMPLOOP0
INTO:
PUSHDPL
PUSHDPH
PUSHACC
MOVDPTR,#7FFCH
MOVA,R3
MOVX@DPTR,A
CPLA
MOVR3,A
POPACC
POPDPH
POPDPL
RETI
DELY:
MOVR4,#0FFH
LOOP:
NOP
NOP
DJNZR4,LOOP
RET
END
程序二(思考题):
ORG0000H
LJMPMAIN
MAIN:
LCALLDELY
MOVDPTR,#7FFFH
MOVA,#80H
MOVX@DPTR,A
MOVR3,#08H
MOVDPTR,#7FFEH
MOVA,#0FFH
MOVX@DPTR,A
LOOP0:
MOVDPTR,#7FFFH
MOVA,#00H
LOOP1:
MOVX@DPTR,A
LCALLDELY
CPLACC.0
MOVX@DPTR,A
LCALLDELY
CPLACC.0
INCACC
INCACC
DJNZR3,LOOP1
MOVR3,#08H
LJMPLOOP0
DELY:
MOVR4,#0FFH
LOOP3:
MOVR5,#0FFH
LOOP:
NOP
NOP
NOP
NOP
NOP
DJNZR5,LOOP
DJNZR4,LOOP3
RET
END
五、实验结果
1、基本要求部分:
将程序产生的十六进制代码加载到proteus中,运行仿真可以在示波器中看到如图6.1所示波形,当按下k0键时可以看到D4点亮,同理按下K1键,D5点亮,按下K2键,D6点亮,按下K3键,D7点亮。
2、思考题部分:
将程序产生的十六进制代码加载到proteus中,运行仿真开始二极管全部熄灭,然后从上倒下依次循环点亮。
图6.1
三、实验电路
四、程序清单及流程图
程序一:
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitsmg0=P2^0;sbitsmg1=P2^1;sbitsmg2=P2^2;sbitsmg3=P2^3;
unsignedcharcodesmg_we[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
ucharzs,xs,ad_data;
voidad(void);
voiddelay(uintt);
voiddelay_1ms(void);
voiddisplay(void);
/******************************主函数************************************/
voidmain()
{while
(1)
{ucharxdata*dr_ad;
uintad_data;
dr_ad=0xfff7;
*dr_ad=0;
delay(15);//延时100us
ad_data=*dr_ad;
ad_data=ad_data*1.963125;
zs=ad_data/100;
xs=ad_data%100;
display();
}
}
/*****************************延时1ms函数********************************/
voiddelay_1ms(void)
{
ucharj,t;
for(t=100;t>0;t--)
{
for(j=19;j>0;j--);
}
}
/******************************延时子函数*********************************/
voiddelay(uintt)
{while(t--);}
/******************************显示子函数**********************************/
voiddisplay(void)
{
P1=smg_we[xs%10];
smg0=1;
smg1=0;
smg2=0;
smg3=0;
delay_1ms();
P1=smg_we[xs/10];
smg0=0;
smg1=1;
smg2=0;
smg3=0;
delay_1ms();
P1=smg_we[zs%10]&0x7f;
smg0=0;
smg1=0;
smg2=1;
smg3=0;
delay_1ms();
smg0=0;
smg1=0;
smg2=0;
smg3=0;
delay_1ms();
}
程序二:
查询方式只需将主函数中的延时改为一个while循环语句,条件为AD中EOC引脚接的单片的口线为1即可。
程序三:
主函数中将总中断和外部中断0开启,将取数据部分放到中断服务程序中,中断服务程序如下。
/************************中断服务函数************************************/
voidad(void)interrupt0using0
{ucharxdata*dr_ad;
dr_ad=0xfff7;
ad_data=ad_data*1.963125;
*dr_ad=0;
}
五、实验结果
将程序产生的十六进制代码加载到proteus中,运行仿真,调节滑动变阻器改变AD输入的电压,可以到如图7.1—7.4所示的实验结果。
图7.1
图7.2
图7.3
图7.4
三、实验原理电路图
四、程序清单及流程图
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharjz;
unsignedcharcodezxb_daima[]={0x7F,0x85,0x8B,0x92,0x98,0x9E,0xA4,0xAA,0xB0,0xB6,0xBB,0xC1,0xC6,0xCB,0xD0,0xD5,0xD9,0xDD,0xE2,0xE5,0xE9,0xEC,0xEF,0xF2,0xF5,0xF7,0xF9,0xFB,0xFC,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFD,0xFC,0xFB,0xF9,0xF7,0xF5,0xF2,0xEF,0xEC,0xE9,0xE5,0xE2,0xDD,0xD9,0xD5,0xD0,0xCB,0xC6,0xC1,0xBB,0xB6,0xB0,0xAA,0xA4,0x9E,0x98,0x92,0x8B,0x85,0x7F,0x79,0x73,0x6C,0x66,0x60,0x5A,0x54,0x4E,0x48,0x43,0x3D,0x38,0x33,0x2E,0x29,0x25,0x21,0x1C,0x19,0x15,0x12,0x0F,0x0C,0x09,0x07,0x05,0x03,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x05,0x07,0x09,0x0C,0x0F,0x12,0x15,0x19,0x1C,0x21,0x25,0x29,0x2E,0x33,0x38,0x3D,0x43,0x48,0x4E,0x54,0x5A,0x60,0x66,0x6C,0x73,0x79};//正弦波数据代码
/****************************延时10ms函数**********************************/
voiddelay_10ms(void)
{ucharj,t;
for(t=100;t>0;t--)
{for(j=190;j>0;j--);
}
}
/********************************延时子函数***********************************/
voiddelay(uintt)
{while(t--);}
/
*******************************取键值子函数*********************************/
ucharkey_data(void)
{
uchari;
i=P1;
i=~i;
i=i&0x1f;
return(i);
}
/**********************判键子函数************************************