单片机上机操作考试题与答案.docx

上传人:b****6 文档编号:4370205 上传时间:2022-12-01 格式:DOCX 页数:58 大小:519.14KB
下载 相关 举报
单片机上机操作考试题与答案.docx_第1页
第1页 / 共58页
单片机上机操作考试题与答案.docx_第2页
第2页 / 共58页
单片机上机操作考试题与答案.docx_第3页
第3页 / 共58页
单片机上机操作考试题与答案.docx_第4页
第4页 / 共58页
单片机上机操作考试题与答案.docx_第5页
第5页 / 共58页
点击查看更多>>
下载资源
资源描述

单片机上机操作考试题与答案.docx

《单片机上机操作考试题与答案.docx》由会员分享,可在线阅读,更多相关《单片机上机操作考试题与答案.docx(58页珍藏版)》请在冰豆网上搜索。

单片机上机操作考试题与答案.docx

单片机上机操作考试题与答案

单片机上机考试试题

1.使得8个发光二极管循环点亮,采用定时器方式0使得每个发光二极管点亮的时间为0.5s。

#include

intcount=0;

intminute=0;

inttemp;

charcodestyle[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

voiddesplay()

{

temp=minute%8;

P0=style[temp];

}

voidtoProc()interrupt1

{

count++;

TH0=0x0c;

TL0=0x78;

}

voidmain()

{

TMOD=0;

TH0=0x0c;

TL0=0x78;

TR0=1;

ET0=1;

EA=1;

while

(1)

{

if(count==100)

{

minute++;

count=0;

}

desplay();

}

}

2.完成下面电路所示的功能,K1,K2对应两个开关按键。

P1口对应发光二极管的状态

P1.0

P1.1

P1.2

P1.3

P1.4

P1.5

P1.6

P1.7

K1=0,K2=0

K1=0,K2=1

K1=1,K2=0

K1=1,K2=1

#include

charcodestyle[4]={0x0,0xf0,0x33,0xff};

sbitP1_0=P1^0;

sbitP1_1=P1^1;

voidmain()

{

P0=0xff;

while

(1)

{

if(P1_0==0&&P1_1==0)

{

P0=style[0];

}

if(P1_0==0&&P1_1==1)

{

P0=style[1];

}

if(P1_0==1&&P1_1==0)

{

P0=style[2];

}

if(P1_0==1&&P1_1==1)

{

P0=style[3];

}

}

}

 

3.在一个数码管上循环显示“H”“E”“L”“L”“O”,循环的时间为1s。

#include

charcodestyle[5]={0x89,0x86,0xc7,0xc7,0xc0};

inti;

intcount=0;

intsecond=0;

voidt0Pro()interrupt1

{

count++;

TL0=0xB0;

TH0=0x3c;

}

voiddisplay()

{

i=second%5;

P0=style[i];

}

voidmain()

{

TMOD=0x01;

EA=1;

TR0=1;

ET0=1;

TL0=0xB0;

TH0=0x3c;

while

(1)

{

if(count==20)

{

second++;

count=0;

}

display();

}

}

 

4.在6个数码管上分别显示自己学号的后六位数字。

#include

charcodecode1[]={0x4f,0x4f,0x01,0x06,0x06,0x4f};

sbitp1_0=P1^0;

charnum;

voiddelay(inttimer)

{

while(timer)

{

--timer;

}

}

voidmain()

{

inti;

while

(1)

{

num=0xFE;

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

{

P1=num;

P2=code1[i];

delay(1000);

num=(num<<1)|1;

}

}

}

5.做一个简易30s的倒计时秒表,秒表的显示通过8个发光二极管显示出来。

#include

#include"stdio.h"

intcount;

count=0;

voidt0Proc()interrupt1

{

TH0=0x3c;

TL0=0xb0;

count++;

if(count==10)

{

P0=P0+1;

count=0;

if(P0==0xff)

{

P0=0xe1;

}

}

}

voidmain()

{

EA=1;

ET0=1;

TMOD=1;

TH0=0x3c;

TL0=0xb0;

TR0=1;

P0=0xe1;

while

(1);

}

 

6.使用外中断0来控制,去实现下列功能。

其中K1为按键,P1口对应8个发光二极管的状态

P1.0

P1.1

P1.2

P1.3

P1.4

P1.5

P1.6

P1.7

无按键按下(循环)

有按键按下

#include

//sbitk1=P2^0;

intcount=0;

voiddelay(unsignedchari);

voidint0Proc()interrupt0

{

count++;

P1=0xf0;

}

voidmain()

{

EA=1;

EX0=1;

IT0=1;

P1=0xcc;

while

(1)

{if(count%2==0)

{

if(P1==0xcc)

{

P1=0x30;

delay(500);

}

else

{

P1=0xcc;

delay(500);

}

}

}

}

voiddelay(unsignedchari)

{

unsignedcharj,k;

for(k=0;k

for(j=0;j<255;j++);

}

 

7.通过A/D转换电路,将模拟电压值在两个发光二极管上显示出来。

(查询和中断方式均可)

#include

#include

#defineaXBYTE[0xcfa8]

sbitp=P3^2;

sbitshi=P3^4;

sbitge=P3^5;

voidsepr(unsignedcharw);

voiddisp();

unsignedchari,x,chh,chl;

voidmain()

{

while

(1)

{

adc0809=0;

while(p);

x=adc0809;

sepr(x);

disp();

}

}

voidsepr(unsignedcharw)

{

unsignedcharch;

ch=w;

chh=ch/51;

ch=ch%51;,

chl=(ch*10)/51;

}

voiddisp()

{

unsignedcharled1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

unsignedcharled2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};

shi=0;

ge=1;

a=led2[chh];

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

shi=1;

ge=0;

a=led1[chl];

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

}

8.采用定时器方式1使得8个发光二极管按如下方式点亮,循环的时间为2s

P1.0

P1.1

P1.2

P1.3

P1.4

P1.5

P1.6

P1.7

#include

charcodestyle[4]={0x0f,0xf0,0xcc,0xff};

inti;

intcount=0;

intsecond=0;

voidt0Pro()interrupt1

{

count++;

TL0=0xB0;

TH0=0x3c;

}

voiddisplay()

{

i=second%4;

P0=style[i];

}

voidmain()

{

TMOD=0x01;

EA=1;

TR0=1;

ET0=1;

TL0=0xB0;

TH0=0x3c;

while

(1)

{

if(count==40)

{

second++;

count=0;

}

display();

}

}

9.在一个数码管上循环显示“0”“1”“5”3个数字,循环的时间为1s。

#include

charcodestyle[3]={0xc0,0xf9,0x92};

inti;

intcount=0;

intsecond=0;

voidt0Pro()interrupt1

{

count++;

TL0=0xB0;

TH0=0x3c;

}

voiddisplay()

{

i=second%3;

P0=style[i];

}

voidmain()

{

TMOD=0x01;

EA=1;

TR0=1;

ET0=1;

TL0=0xB0;

TH0=0x3c;

while

(1)

{

if(count==20)

{

second++;

count=0;

}

display();

}

}

 

10.在4个数码管上稳定显示出“A”“C”“E”“P”四个字符。

#include

charcodestyle[4]={0x88,0xc6,0x86,0x8c};

voidmain()

{

while

(1)

{

P0=style[0];

P2=style[1];

P3=style[2];

P1=style[3];

}

}

11.试采用定时方式2,在P1.0引脚上输出频率为1KHz的方波(可能有问题)

#include

intcount;

count=0;

sbitp1_0=P1^0;

voidt0Proc()interrupt1

{

p1_0=~p1_0;

}

voidmain()

{

EA=1;

ET0=1;

TMOD=2;

TH0=0x06;

TL0=0x06;

TR0=1;

p1_0=1;

while

(1);

}

12.做一个20s的简易秒表,当按键K1按下时,秒表暂停计数,K2按下时,开始计数。

#include"reg51.h"

#defineucharunsignedchar

#defineuintunsignedint

uintcount=0;

uchari;

//0-9段码

ucharcodetime[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

voidStop()interrupt2//INT1中断

{

ET0=1;

TR0=1;

}

voidStart()interrupt0//INT0中断

{

ET0=0;

TR0=0;

}

voidTimer()interrupt1//T0中断

{

TH0=(65536-50000)/256;//恢复定时器0初值

TL0=(65536-50000)%256;

if(++i==20)//50ms*20=1s转换状态

{

i=0;

count++;

P0=time[count/10];

P1=time[count%10];

if(count==20)count=0;//满20s后显示00

}

}

voidmain()

{//初始显示00

P0=0xc0;

P1=0xc0;//T0采用方式1

TMOD=0x01;//中断总开关

EA=1;

EX0=1;

EX1=1;

IT0=1;

IT1=1;//INT0优先

PX0=1;

while

(1);

}

 

14.使用外中断1来控制,当无按键按下时8个发光二极管循环点亮,循环点亮的时间为1s,有按键按下时,8个发光二极管全部熄灭。

#include"stdio.h"

#include"reg51.h"

intcount=0;

voidinterINT1()interrupt2

{

P0=0xff;

ET0=0;

TR0=0;

}

voidinterT0()interrupt1

{

if(count==15)

{

TH0=0xBD;

TL0=0xC0;

P0=(P0<<1)|1;

if(P0==0xff)

{

P0=0xfe;

}

count=0;

}

count++;

}

voidmain()

{

EA=1;

ET0=1;

TR0=1;

TMOD=0x01;

TH0=0xBD;

TL0=0xC0;

P0=0xfe;

while

(1)

{

if(P3^3==0)

{

EX1=1;

IT1=1;

}

}

}

 

15.通过串口控制单片机产生1KHz的波型,当PC通过串口发过来“K”时,开始产生波型,当发过来“S”时,停止产生波型,fosc=12MHz。

(没答案)

15.通过A/D转换电路,将模拟电压值在两个发光二极管上显示出来。

(查询和中断方式均可)

#include

#include

#defineaXBYTE[0xcfa8]

sbitp=P3^2;

sbitshi=P3^4;

sbitge=P3^5;

voidsepr(unsignedcharw);

voiddisp();

unsignedchari,x,chh,chl;

voidmain()

{

while

(1)

{

adc0809=0;

while(p);

x=adc0809;

sepr(x);

disp();

}

}

voidsepr(unsignedcharw)

{

unsignedcharch;

ch=w;

chh=ch/51;

ch=ch%51;,

chl=(ch*10)/51;

}

voiddisp()

{

unsignedcharled1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

unsignedcharled2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};

shi=0;

ge=1;

a=led2[chh];

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

shi=1;

ge=0;

a=led1[chl];

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

}

 

16.完成下面电路所示的功能,K1,K2对应两个开关按键。

P1口对应发光二极管的状态

P1.0

P1.1

P1.2

P1.3

P1.4

P1.5

P1.6

P1.7

K1=0,K2=0

K1=0,K2=1

K1=1,K2=0

K1=1,K2=1

#include"stdio.h"

#include"reg51.h"

sbitp0_0=P0^0;

sbitp0_1=P0^1;

voiddelay()

{

inttime=1000;

while(time>0)

{

time--;

}

}

voidmain()

{

//P1=0xaa;

while

(1)

{

if(p0_0==0)

{

if(p0_1==0)

{

P1=0X00;

delay();

}

elseif(p0_1==1)

{

P1=0X0f;

delay();

}

}

elseif(p0_0==1)

{

if(p0_1==0)

{

P1=0Xf3;

delay();

}

elseif(p0_1==1)

{

P1=0Xaa;

delay();

}

}

}

}

17.完成下面电路的功能,P1口对应发光二极管的状态,状态1显示的时间为1s,状态2显示的时间为2s,状态3显示的时间为3s,如此循环

P1.0

P1.1

P1.2

状态1

状态2

状态3

#include"stdio.h"

#include"reg51.h"

intcount=0;

intflag=0;

voidfirst()

{

P1=0x02;

flag=0;

TH0=0xBD;

TL0=0xC0;

}

voidinterT0()interrupt1

{P1=0x02;

if(flag==0)

{

if(count==15)

{

flag=1;

count=0;

TH0=0xBD;

TL0=0xC0;

}

count++;

}

elseif(flag==1)

{P1=0x01;

if(count==15)

{

TH0=0xBD;

TL0=0xC0;

}

if(count==30)

{

flag=2;

count=0;

TH0=0xBD;

TL0=0xC0;

}

count++;

}

elseif(flag==2)

{P1=0x04;

if(count==15)

{

TH0=0xBD;

TL0=0xC0;

}

if(count==30)

{

TH0=0xBD;

TL0=0xC0;

}

if(count==45)

{

first();

count=0;

}

count++;

}

}

voidmain()

{

EA=1;

TMOD=1;

ET0=1;

TR0=1;

first();

while

(1);

}

 

18.彩灯系统,实现8个发光二极管从左到右点亮,每个发光二极管点亮的时间为2s,然后从右到左亮,每个发光二极管点亮的时间为1.5s.

#include

#include

#defineucharunsignedchar

ucharcodetab[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

ucharcodetab1[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

ucharState=0;

ucharT_count=0;

uchari=1;

voidt0Proc()interrupt1

{

TH0=0x3c;

TL0=0xb0;

switch(State)

{

case0:

if(++T_count==20)

{

P0=tab[i];

i=(i+1)%8;

if(i==7)

{

State=1;

}

T_count=0;

}

break;

case1:

if(++T_count==15)

{

i=(i+1)%8;

P0=tab1[i];

if(i==7)

{

State=0;

i=1;

}

T_count=0;

}

break;

}

}

voidmain()

{

TMOD=1;

TH0=0x3c;

TL0=0xb0;

EA=1;

ET0=1;

TR0=1;

P0=tab[0];

while

(1);

}

19.在4个数码管上显示出当天的日期。

#include

#defineucharunsignedchar

#defineuintunsignedint

ucharcodetab[]={0xc0,0xb0,0xc0,0x92};

voiddelay(intx)

{

uchari;

while(x--)

{

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

}

}

voidmain()

{

P0=tab[0];

P2=tab[1];

P3=tab[2];

P1=tab[3];

delay(500);

}

20.完成下面电路的功能,P1口对应发光二极管的状态,状态1显示的时间为3s,状态2显示的时间为1s,状态3显示的时间为0.5s,状态4显示的时间为0.1s,如此循环

P1.0

P1.1

P1.2

P1.3

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

当前位置:首页 > 高中教育 > 初中教育

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

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