当前位置:文档之家› 单片机上机操作考试题及答案

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

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

#include <reg51.h>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();}}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 <reg51.h>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 <reg51.h>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<reg51.h>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<reg51.h>#include"stdio.h"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来控制,去实现下列功能。

P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7无按键按下(循环)●●○○●●○○●●●●○○●●有按键按下●●●●○○○○#include<reg51.h>//sbit k1=P2^0;int count=0;void delay(unsigned char i); void int0Proc() interrupt 0 {count++;P1=0xf0;}void main(){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);}}}}void delay(unsigned char i) {unsigned char j,k;for(k=0;k<i;k++)for(j=0;j<255;j++);}7.通过A/D转换电路,将模拟电压值在两个发光二极管上显示出来。

(查询和中断方式均可)#include<reg51.h>#include<absacc.h>#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++);}#include <reg51.h>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();}}9.在一个数码管上循环显示“0”“1”“5”3个数字,循环的时间为1s。

#include <reg51.h>char code style[3]={0xc0,0xf9,0x92};int i;int count=0;int second=0;void t0Pro() interrupt 1 {count++;TL0=0xB0;TH0=0x3c;}void display(){i=second%3;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();}}10.在4个数码管上稳定显示出“A”“C”“E”“P”四个字符。

#include <reg51.h>char code style[4]={0x88,0xc6,0x86,0x8c};void main(){while(1){P0=style[0];P2=style[1];P3=style[2];P1=style[3];}}11.试采用定时方式2,在P1.0引脚上输出频率为1KHz的方波(可能有问题)#include<reg51.h>int count;count=0;sbit p1_0=P1^0;void t0Proc() interrupt 1{p1_0=~p1_0;}void main(){EA=1;ET0=1;TMOD=2;TH0=0x06;TL0=0x06;TR0=1;p1_0=1;while(1);}12.做一个20s的简易秒表,当按键K1按下时,秒表暂停计数,K2按下时,开始计数。

#include "reg51.h"#define uchar unsigned char#define uint unsigned intuint count = 0;uchar i;//0-9段码uchar code time[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};void Stop() interrupt 2 //INT1中断{ET0 = 1;TR0 = 1;}void Start() interrupt 0 //INT0中断{ET0 = 0;TR0 = 0;}void Timer() interrupt 1 //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 }}void main(){ //初始显示00P0 = 0xc0;P1 = 0xc0; //T0采用方式1TMOD=0x01;//中断总开关EA = 1;EX0 = 1;EX1 = 1;IT0 = 1;IT1 = 1;//INT0优先PX0 = 1;while(1);}14.使用外中断1来控制,当无按键按下时8个发光二极管循环点亮,循环点亮的时间为1s,有按键按下时,8个发光二极管全部熄灭。

相关主题