整理单片机实验三报告参考模板0526Word文件下载.docx
《整理单片机实验三报告参考模板0526Word文件下载.docx》由会员分享,可在线阅读,更多相关《整理单片机实验三报告参考模板0526Word文件下载.docx(22页珍藏版)》请在冰豆网上搜索。
sbitkey1=P3^2;
sbitkey2=P3^3;
#definewxP2
#definedxP0
unsignedcharcount=0;
unsignedcharcodeled_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,
0x58,0x00,0x10};
//0~9及其小数部分显示
voidkeyscan_0()interrupt0//外部中断0按键中断函数定义
{
{
while(key1==0);
count++;
if(count==10)count=0;
}
}
voidkeyscan_2()interrupt2//外部中断1按键中断函数定义
while(key2==0);
count--;
if(count==0xff)count=10;
voiddisp()
wx=0xfe;
dx=led_code[count];
voidmain()
P3=0xff;
IE=0X81;
//允许int0中断,字节操作赋值语句
EA=1;
EX0=1;
//为操作赋值语句
EX1=1;
IP=0X01;
//表示int0优先级为高级,字节操作赋值语句
PX0=1;
//位操作赋值语句
PX1=1;
IT0=1;
//表示下降沿触发,位操作赋值语句
IT1=1;
TCON=0X01;
//字节操作赋值语句
while
(1)
disp();
4、画出每个源程序配套的流程图(好的流程图反映了编程的逻辑思路清晰);
5、思考与提高,相关补充关键的知识点归纳(自学收获小结,找出自己所学的知识点中不足的部分);
(二)任务2:
TC用做内部定时器的应用(结合LED、数码管、蜂鸣器等)
2、实现的功能描述
(1)用T0方式1定时50ms中断编程实现P1.0的LED灯亮灭各0.5s交替循环变化。
(2)实现每隔1S的多花样流水灯(1S用定时器中断法编程);
(3)简易电子琴的设计(不同按键控制,令蜂鸣器发出DOREMIFASELAXI七个音频信号,同时数码管显示对应的音阶数字);
(4)实现1位数码管每隔1S循环0-9的显示变化(1S用定时器中断法编程);
(5)实现2位数码管每隔1S循环00-59的显示变化(1S用定时器中断法编程);
(6)实现8位数码管显示模拟时钟的“时-分-秒”(1S用定时器中断法编程);
sbitmic=P1^0;
//定义喇叭接口
bitflag;
//定义标志位
unsignedchari;
//定义变量
unsignedcharsong[]=
1,1,2,1,4,3,1,1,2,1,5,4,
1,1,8,6,4,3,2,2,7,7,6,4,5,4,15
};
//定义生日歌歌谱
inttone[]=
1012,903,852,759,716,638,568,506,478,426,379
//定义简谱音调
unsignedcharbeat[]=
60,10,40,40,40,80,60,10,40,40,40,80,
60,10,40,40,40,40,40,60,10,40,40,40,80
//定义节拍
unsignedcharbeath=(65536-50000)/256;
unsignedcharbeatl=(65536-50000)%256;
//定义节拍变量
unsignedchartime,j,k;
inttoneh,tonel;
mic=0;
i=0;
time=30;
TMOD=1;
//定义计时器工作方式
EA=1;
//开总中断
while(song[i]!
=15)
toneh=(65536-tone[song[i]])/256;
tonel=(65536-tone[song[i]])%256;
TH0=toneh;
TL0=tonel;
ET0=1;
ET1=1;
TR0=1;
time=beat[i];
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
flag=0;
TR1=1;
while(flag==0);
i++;
voidtime0()interrupt1
TH0=toneh;
TL0=tonel;
mic=~mic;
voidtime1()interrupt3
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
if(--time==0)
flag=1;
TR1=0;
#include<
unsignedintx,a,b,i=0;
unsignedchartab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//0-9数字显示
voiddisplay(unsignedintk)
P2=0xfd;
//数码管位定义
P0=tab[(k%100)/10];
P2=0xfe;
P0=tab[k%10];
P2=0Xff;
voidmain()
unsignedchari;
x=0;
TR0=1;
TMOD=0x01;
TL0=(65535-50000)%256;
TH0=(65535-50000)/256;
if(x==59)//计时到59然后清零
x=0;
if(0<
=i<
9)
{
display(x);
//调用显示函数
x++;
}
if(i==10)
i=0;
if(a==20)
a=0;
}
voidexter()interrupt1
TL0=(65535-50000)%256;
TH0=(65535-50000)/256;
a++;
REG51.H>
#defineuintunsignedint
unsignedcharcodedy[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
uintt=0,sec=0,fen=0,shi=0,a,b,c,d,e,f;
voiddelay()
unsignedintx,y;
for(x=3;
x>
0;
x--)
for(y=110;
y>
y--);
voidsmsm()
P2=0XFB;
P0=0Xbf;
delay();
P2=0Xdf;
P2=0XFE;
P0=dy[b];
P2=0XFD;
P0=dy[a];
P2=0Xf7;
P0=dy[d];
P2=0Xef;
P0=dy[c];
P2=0Xbf;
P0=dy[f];
P2=0X7f;
P0=dy[e];
voidgd()
a=sec/10;
b=sec%10;
c=fen/10;
d=fen%10;
e=shi/10;
f=shi%10;
voidinit()
P0=0XFF;
TMOD=0X01;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
init();
gd();
smsm();
delay();
if(t==20)
t=0;
sec++;
if(sec==60)
sec=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}
}
voidet0()interrupt1
TR0=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
(三)任务3:
TC用做外部计数器的应用(外接信号源)
2、实现的功能描述:
简易频率计设计,即:
测量周期方波信号的频率并数码管显示;
unsignedcharcodedy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uintt=0,a,b,c,d,e,f,g,h,tt;
sbitled=P1^0;
voiddelay(unsignedintz)//延时函数
for(x=z;
voidsmsm()//数码管显示函数
{
delay(3);
P0=dy[g];
P0=dy[h];
a=t%10;
b=t/10%10;
c=t/100%10;
d=t/1000%10;
e=t/10000%10;
f=t/100000%10;
g=t/1000000%10;
h=t/10000000;
voidinit()//定时器函数
TMOD=0X51;
TH0=(65536-1)/256;
TL0=(65536-1)%256;
tt++;
if(tt==20)
tt=0;
t=TH1*256+TL1;
TH1=0;
TL1=0;
(四)任务4:
综合实现电子钟(用定时中断实现的电子钟)
(1)定时与中断综合应用实现可调时间的电子钟(按键中断+数码管),具体细节功能自定义;
(2)闹钟(有整点报时或者特定时间的闹铃),具体细节功能自定义;
(3)简易倒(正)计时器的设计,具体细节功能自定义;
#defineLEDP1
sbitled=P3^7;
//调整
//+
sbitkey3=P3^4;
//-
sbitkey4=P3^5;
//启动
unsignedcharPMD[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsignedcharcodeseg_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x90,0xbf};
unsignedcharcodewx_code[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsignedcharxshc[8]={0,0,10,0,0,10,0,0};
unsignedcharhour;
unsignedcharmin;
unsignedcharsec;
unsignedchara,i,count,addr;
unsignedinttt;
voiddelay(unsignedintxms)
unsignedintj,k;
for(k=0;
k<
xms;
k++)
for(j=0;
j<
125;
j++);
voidxianshi()
for(i=0;
i<
8;
i++)
dx=seg_code[xshc[i]];
if(addr==i)
count++;
if(count<
5)wx=0xff;
elseif(count<
10)wx=wx_code[i];
elsecount=0;
}
elsewx=wx_code[i];
delay
(2);
wx=0xff;
voidclock()
if(sec==60){sec=0;
min++;
}
if(min==60){min=0;
hour++;
if(hour==24)hour=0;
xshc[0]=sec%10;
xshc[1]=sec/10;
xshc[3]=min%10;
xshc[4]=min/10;
xshc[6]=hour%10;
xshc[7]=hour/10;
voidkeyscan()
if(key1==0)
while(key1==0)xianshi();
addr++;
if(addr>
=8){addr=0xff;
LED=0xff;
}
if(key2==0)
while(key2==0)xianshi();
switch(addr)
case0:
sec++;
if(sec==60)sec=0;
break;
case1:
sec+=10;
if(sec/10*10==60)sec=sec%10;
case3:
min++;
if(min==60)min=0;
case4:
min+=10;
if(min/10*10==60)min=min%10;
case6:
hour++;
if(hour==24)hour=0;
case7:
hour+=10;
if(hour/10==2&
&
hour%10>
=4||hour/10==3)hour=hour%10;
default:
break;
if(key3==0)
while(key3==0)xianshi();
switch(addr)
{
case0:
sec--;
if(sec==0xff)sec=59;
case1:
sec-=10;
if(sec>
=0xf0&
sec<
=0xff)sec=50;
case3:
min--;
if(min==0xff)min=59;
case4:
min-=0x0a;
if(min>
=0xff)min=50;
case6:
hour--;
if(hour==0xff)hour=23;
case7:
if(hour>
=252&
hour<
=255)hour=23;
default:
}
if(key4==0)
while(~key4);
TR0=~TR0;
addr=0xff;
P0=0X00;
P2=0XFF;
keyscan();
clock();
xianshi();
if(sec<
=6&
sec>
=0&
min==0)
led=~led;