单片机的40个实验2.docx

上传人:b****5 文档编号:4709320 上传时间:2022-12-07 格式:DOCX 页数:46 大小:382.87KB
下载 相关 举报
单片机的40个实验2.docx_第1页
第1页 / 共46页
单片机的40个实验2.docx_第2页
第2页 / 共46页
单片机的40个实验2.docx_第3页
第3页 / 共46页
单片机的40个实验2.docx_第4页
第4页 / 共46页
单片机的40个实验2.docx_第5页
第5页 / 共46页
点击查看更多>>
下载资源
资源描述

单片机的40个实验2.docx

《单片机的40个实验2.docx》由会员分享,可在线阅读,更多相关《单片机的40个实验2.docx(46页珍藏版)》请在冰豆网上搜索。

单片机的40个实验2.docx

单片机的40个实验2

14.4×4矩阵式键盘识别技术

1.实验任务

如图4.14.2所示,用AT89S51的并行口P1接4×4矩阵键盘,以P1.0-P1.3作输入线,以P1.4-P1.7作输出线;在数码管上显示每个按键的“0-F”序号。

对应的按键的序号排列如图4.14.1所示

图4.14.1

2.硬件电路原理图

图4.14.2

3.程序框图

图4.14.3

4.C语言源程序

#include

unsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71};

unsignedchartemp;

unsignedcharkey;

unsignedchari,j;

voidmain(void)

{

while

(1)

{

P3=0xff;

P3_4=0;

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

for(i=50;i>0;i--)

for(j=200;j>0;j--);

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

switch(temp)

{

case0x0e:

key=7;

break;

case0x0d:

key=8;

break;

case0x0b:

key=9;

break;

case0x07:

key=10;

break;

}

temp=P3;

P1_0=~P1_0;

P0=table[key];

temp=temp&0x0f;

while(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

}

}

}

P3=0xff;

P3_5=0;

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

for(i=50;i>0;i--)

for(j=200;j>0;j--);

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

switch(temp)

{

case0x0e:

key=4;

break;

case0x0d:

key=5;

break;

case0x0b:

key=6;

break;

case0x07:

key=11;

break;

}

temp=P3;

P1_0=~P1_0;

P0=table[key];

temp=temp&0x0f;

while(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

}

}

}

P3=0xff;

P3_6=0;

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

for(i=50;i>0;i--)

for(j=200;j>0;j--);

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

switch(temp)

{

case0x0e:

key=1;

break;

case0x0d:

key=2;

break;

case0x0b:

key=3;

break;

case0x07:

key=12;

break;

}

temp=P3;

P1_0=~P1_0;

P0=table[key];

temp=temp&0x0f;

while(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

}

}

}

P3=0xff;

P3_7=0;

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

for(i=50;i>0;i--)

for(j=200;j>0;j--);

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

switch(temp)

{

case0x0e:

key=0;

break;

case0x0d:

key=13;

break;

case0x0b:

key=14;

break;

case0x07:

key=15;

break;

}

temp=P3;

P1_0=~P1_0;

P0=table[key];

temp=temp&0x0f;

while(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

}

}

}

}

}

15.定时计数器T0作定时应用技术

(一)

1.实验任务

用AT89S51单片机的定时/计数器T0产生一秒的定时时间,作为秒计数时间,当一秒产生时,秒计数加1,秒计数到60时,自动从0开始。

硬件电路如下图所示

2.电路原理图

图4.15.1

3.程序框图

 

 

 

 

 

 

 

 

 

图4.15.2

4.C语言源程序(查询法)

#include

unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsignedcharsecond;

unsignedchartcount;

voidmain(void)

{

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

TR0=1;

tcount=0;

second=0;

P0=dispcode[second/10];

P2=dispcode[second%10];

while

(1)

{

if(TF0==1)

{

tcount++;

if(tcount==20)

{

tcount=0;

second++;

if(second==60)

{

second=0;

}

P0=dispcode[second/10];

P2=dispcode[second%10];

}

TF0=0;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

}

}

}

5.C语言源程序(中断法)

#include

unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsignedcharsecond;

unsignedchartcount;

voidmain(void)

{

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

TR0=1;

ET0=1;

EA=1;

tcount=0;

second=0;

P0=dispcode[second/10];

P2=dispcode[second%10];

while

(1);

}

voidt0(void)interrupt1using0

{

tcount++;

if(tcount==20)

{

tcount=0;

second++;

if(second==60)

{

second=0;

}

P0=dispcode[second/10];

P2=dispcode[second%10];

}

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

}

16.定时计数器T0作定时应用技术

(二)

1.实验任务

用AT89S51的定时/计数器T0产生2秒钟的定时,每当2秒定时到来时,更换指示灯闪烁,每个指示闪烁的频率为0.2秒,也就是说,开始L1指示灯以0.2秒的速率闪烁,当2秒定时到来之后,L2开始以0.2秒的速率闪烁,如此循环下去。

0.2秒的闪烁速率也由定时/计数器T0来完成。

2.电路原理图

图4.16.1

3.程序框图

 

T0中断服务程序框图

主程序框图

图4.16.2 

4.C语言源程序

#include

unsignedchartcount2s;

unsignedchartcount02s;

unsignedcharID;

voidmain(void)

{

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

TR0=1;

ET0=1;

EA=1;

while

(1);

}

voidt0(void)interrupt1using0

{

tcount2s++;

if(tcount2s==40)

{

tcount2s=0;

ID++;

if(ID==4)

{

ID=0;

}

}

tcount02s++;

if(tcount02s==4)

{

tcount02s=0;

switch(ID)

{

case0:

P1_0=~P1_0;

break;

case1:

P1_1=~P1_1;

break;

case2:

P1_2=~P1_2;

break;

case3:

P1_3=~P1_3;

break;

}

}

}

17.99秒马表设计

1.实验任务

(1.开始时,显示“00”,第1次按下SP1后就开始计时。

(2.第2次按SP1后,计时停止。

(3.第3次按SP1后,计时归零。

2.电路原理图

图4.17.1

3.程序框图

主程序框图

 

T0中断服务程序框图

图4.17.2

4.C语言源程序

#include

unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsignedcharsecond;

unsignedcharkeycnt;

unsignedinttcnt;

voidmain(void)

{

unsignedchari,j;

TMOD=0x02;

ET0=1;

EA=1;

second=0;

P0=dispcode[second/10];

P2=dispcode[second%10];

while

(1)

{

if(P3_5==0)

{

for(i=20;i>0;i--)

for(j=248;j>0;j--);

if(P3_5==0)

{

keycnt++;

switch(keycnt)

{

case1:

TH0=0x06;

TL0=0x06;

TR0=1;

break;

case2:

TR0=0;

break;

case3:

keycnt=0;

second=0;

P0=dispcode[second/10];

P2=dispcode[second%10];

break;

}

while(P3_5==0);

}

}

}

}

voidt0(void)interrupt1using0

{

tcnt++;

if(tcnt==400)

{

tcnt=0;

second++;

if(second==100)

{

second=0;

}

P0=dispcode[second/10];

P2=dispcode[second%10];

}

}

18.“嘀、嘀、……”报警声

1.实验任务

用AT89S51单片机产生“嘀、嘀、…”报警声从P1.0端口输出,产生频率为1KHz,根据上面图可知:

1KHZ方波从P1.0输出0.2秒,接着0.2秒从P1.0输出电平信号,如此循环下去,就形成我们所需的报警声了。

2.电路原理图

图4.18.1

3.程序框图

 

主程序框图

中断服务程序框图

图4.18.2

4.C语言源程序

#include

unsignedintt02s;

unsignedchart05ms;

bitflag;

voidmain(void)

{

TMOD=0x01;

TH0=(65536-500)/256;

TL0=(65536-500)%256;

TR0=1;

ET0=1;

EA=1;

while

(1);

}

voidt0(void)interrupt1using0

{

TH0=(65536-500)/256;

TL0=(65536-500)%256;

t02s++;

if(t02s==400)

{

t02s=0;

flag=~flag;

}

if(flag==0)

{

P1_0=~P1_0;

}

}

19.“叮咚”门铃

1.实验任务

当按下开关SP1,AT89S51单片机产生“叮咚”声从P1.0端口输出到LM386,经过放大之后送入喇叭。

2.电路原理图

图4.19.1

3.程序框图

主程序框图

T0中断服务程序框图

图4.19.2

4.C语言源程序

#include

unsignedchart5hz;

unsignedchart7hz;

unsignedinttcnt;

bitstop;

bitflag;

voidmain(void)

{

unsignedchari,j;

TMOD=0x02;

TH0=0x06;

TL0=0x06;

ET0=1;

EA=1;

while

(1)

{

if(P3_7==0)

{

for(i=10;i>0;i--)

for(j=248;j>0;j--);

if(P3_7==0)

{

t5hz=0;

t7hz=0;

tcnt=0;

flag=0;

stop=0;

TR0=1;

while(stop==0);

}

}

}

}

voidt0(void)interrupt1using0

{

tcnt++;

if(tcnt==2000)

{

tcnt=0;

if(flag==0)

{

flag=~flag;

}

else

{

stop=1;

TR0=0;

}

}

if(flag==0)

{

t7hz++;

if(t7hz==3)

{

t7hz=0;

P1_0=~P1_0;

}

}

else

{

t5hz++;

if(t5hz==4)

{

t5hz=0;

P1_0=~P1_0;

}

}

}

20.数字钟﹝★﹞

1.实验任务

(1.开机时,显示12:

00:

00的时间开始计时;

(2.P0.0/AD0控制“秒”的调整,每按一次加1秒;

(3.P0.1/AD1控制“分”的调整,每按一次加1分;

(4.P0.2/AD2控制“时”的调整,每按一次加1个小时;

2.电路原理图

图4.20.1

3.程序框图

4.C语言源程序

#include

unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsignedchardispbitcode[]={0xfe,0xfd,0xfb,0xf7,

0xef,0xdf,0xbf,0x7f};

unsignedchardispbuf[8]={0,0,16,0,0,16,0,0};

unsignedchardispbitcnt;

unsignedcharsecond;

unsignedcharminite;

unsignedcharhour;

unsignedinttcnt;

unsignedcharmstcnt;

unsignedchari,j;

voidmain(void)

{

TMOD=0x02;

TH0=0x06;

TL0=0x06;

TR0=1;

ET0=1;

EA=1;

while

(1)

{

if(P0_0==0)

{

for(i=5;i>0;i--)

for(j=248;j>0;j--);

if(P0_0==0)

{

second++;

if(second==60)

{

second=0;

}

dispbuf[0]=second%10;

dispbuf[1]=second/10;

while(P0_0==0);

}

}

if(P0_1==0)

{

for(i=5;i>0;i--)

for(j=248;j>0;j--);

if(P0_1==0)

{

minite++;

if(minite==60)

{

minite=0;

}

dispbuf[3]=minite%10;

dispbuf[4]=minite/10;

while(P0_1==0);

}

}

if(P0_2==0)

{

for(i=5;i>0;i--)

for(j=248;j>0;j--);

if(P0_2==0)

{

hour++;

if(hour==24)

{

hour=0;

}

dispbuf[6]=hour%10;

dispbuf[7]=hour/10;

while(P0_2==0);

}

}

}

}

voidt0(void)interrupt1using0

{

mstcnt++;

if(mstcnt==8)

{

mstcnt=0;

P1=dispcode[dispbuf[dispbitcnt]];

P3=dispbitcode[dispbitcnt];

dispbitcnt++;

if(dispbitcnt==8)

{

dispbitcnt=0;

}

}

tcnt++;

if(tcnt==4000)

{

tcnt=0;

second++;

if(second==60)

{

second=0;

minite++;

if(minite==60)

{

minite=0;

hour++;

if(hour==24)

{

hour=0;

}

}

}

dispbuf[0]=second%10;

dispbuf[1]=second/10;

dispbuf[3]=minite%10;

dispbuf[4]=minite/10;

dispbuf[6]=hour%10;

dispbuf[7]=hour/10;

}

}

 21.拉幕式数码显示技术

1.实验任务

用AT89S51单片机的P0.0/AD0-P0.7/AD7端口接数码管的a-h端,8位数码管的S1-S8通过74LS138译码器的Y0-Y7来控制选通每个数码管的位选端。

AT89S51单片机的P1.0-P1.2控制74LS138的A,B,C端子。

在8位数码管上从右向左循环显示“12345678”。

能够比较平滑地看到拉幕的效果。

2.电路原理图

图4.21.1

3.程序框图

主程序框图

中断服务程序框图

 

图4.21.2

4.C语言源程序

#include

unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71,0x00};

unsignedchardispbitcode[]={0xf8,0xf9,0xfa,0xfb,

0xfc,0xfd,0xfe,0xff};

unsignedchardispbuf[8]={16,16,16,16,16,16,16,16};

unsignedchardispbitcnt;

unsignedintt02scnt;

unsignedchart5mscnt;

unsignedcharu;

unsignedchari;

voidmain(void)

{

TMOD=0x02;

TH0=0x06;

TL0=0x06;

TR0=1;

ET0=1;

EA=1;

while

(1);

}

voidt0(void)interrupt1using0

{

t5mscnt++;

if(t5mscnt==4)

{

t5mscnt=0;

P0=dispcode[dispbuf[dispbitcnt]];

P1=dispbitcode[dispbitcnt];

dispbitcnt++;

if(dispbitcnt==8)

{

dispbitcnt=0;

}

}

t02scnt++;

if(t02scnt==1600)

{

t02scnt=0;

u++;

if(u==9)

{

u=0;

}

for(i=0;i<8;i++)

{

dispbuf[i]=16;

}

for(i=0;i

{

dispbuf[i]=8;

}

}

}

22.电子琴

1.实验任务

(1.由4X4组成16个按钮矩阵,设计成16个音。

(2.可随意弹奏想要表达的音乐。

2.电路原理图

图4.22.1

3.程序框图

图4.22.2

4.C语言源程序

#include

unsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71};

unsignedchartemp;

unsignedcharkey;

unsignedchari,j;

unsignedcharSTH0;

unsign

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 解决方案

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1