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

  • 格式:doc
  • 大小:679.50 KB
  • 文档页数:52

单片机上机考试试题

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

#include <>

int count=0;

int minute=0;

int temp;

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

void desplay()

{

temp=minute%8;

P0= style[temp];

}

void toProc() interrupt 1

{

count++;

TH0=0x0c;

TL0=0x78;

}

void main()

{

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口对应发光二极管的状态

K1=0,K2=0 ○ ○ ○ ○ ○ ○ ○ ○ K1=0,K2=1 ● ● ● ● ○ ○ ○ ○

K1=1,K2=0 ● ● ○ ○ ● ● ○ ○

K1=1,K2=1 ● ● ● ● ● ● ● ●

#include <>

char code style[4]={0x0,0xf0,0x33,0xff};

sbit P1_0=P1^0;

sbit P1_1=P1^1;

void main()

{

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 <>

char code style[5]={0x89,0x86,0xc7,0xc7,0xc0};

int i;

int count=0;

int second=0;

void t0Pro() interrupt 1

{

count++;

TL0=0xB0;

TH0=0x3c; }

void display()

{

i=second%5;

P0=style[i];

}

void main()

{

TMOD=0x01;

EA=1;

TR0=1;

ET0=1;

TL0=0xB0;

TH0=0x3c;

while(1)

{

if(count==20)

{

second++;

count=0;

}

display();

}

}

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

#include<>

char code code1[]={0x4f,0x4f,0x01,0x06,0x06,0x4f};

sbit p1_0=P1^0;

char num;

void delay(int timer)

{

while(timer)

{

--timer; }

}

void main()

{

int i;

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""

int count;

count=0;

void t0Proc() interrupt 1

{

TH0=0x3c;

TL0=0xb0;

count++;

if(count==10)

{

P0=P0+1;

count=0; if(P0==0xff)

{

P0=0xe1;

}

}

}

void main()

{

EA=1;

ET0=1;

TMOD=1;

TH0=0x3c;

TL0=0xb0;

TR0=1;

P0=0xe1;

while(1);

}

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

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

无按键按下(循环) ● ● ○ ○ ● ● ○ ○

● ● ● ● ○ ○ ● ●

有按键按下 ● ● ● ● ○ ○ ○ ○

#include<>

过A/D转换电路,将模拟电压值在两个发光二极管上显示出来。(查询和中断方式均可)

#include<>

#include<>

#define a XBYTE[0xcfa8]

sbit p=P3^2;

sbit shi=P3^4;

sbit ge=P3^5; void sepr(unsigned char w);

void disp();

unsigned char i,x,chh,chl;

void main()

{

while(1)

{

adc0809=0;

while(p);

x=adc0809;

sepr(x);

disp();

}

}

void sepr(unsigned char w)

{

unsigned char ch;

ch=w;

chh=ch/51;

ch=ch%51;,

chl=(ch*10)/51;

}

void disp()

{

unsigned char led1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char led2[]={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

○ ○ ○ ○ ● ● ● ●

● ● ● ● ○ ○ ○ ○

● ● ○ ○ ● ● ○ ○

● ● ● ● ● ● ● ●

#include <>

char code style[4]={0x0f,0xf0,0xcc,0xff};

int i;

int count=0;

int second=0;

void t0Pro() interrupt 1

{

count++;

TL0=0xB0; TH0=0x3c;

}

void display()

{

i=second%4;

P0=style[i];

}

void main()

{

TMOD=0x01;

EA=1;

TR0=1;

ET0=1;

TL0=0xB0;

TH0=0x3c;

while(1)

{

if(count==40)

{

second++;

count=0;

}

display();

}