两个单片机小项目(一)

来源:互联网 发布:法国经济知乎 编辑:程序博客网 时间:2024/06/04 18:31
#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit dula=P2^6;sbit wela=P2^7;sbit beep=P2^3;unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,                        0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};uchar x=60,j,k;         //x用于计时,改x值可以改变留给选手反应的时间uchar a,b;              //函数内部显示传递参数用uchar pp,num,ok,temp;  //计时器计数,num表示选手号码,ok表示选手按下按钮,temp用来检测键盘的随机数void delay(uchar);void keyscan();void display(uchar,uchar);void main(){  TMOD=0x01;TR0=1;TH0=(65536-46080)/256;// 由于晶振为11.0592,故所记次数应为46080,计时器每隔50000微秒发起一次中断。TL0=(65536-46080)%256;//46080的来历,为50000*11.0592/12ET0=1;EA=1;  while(1)   {  keyscan();   if(ok==1){x=num;ok=0;ET0=0;} if(pp==20)     {    if(x>0){ pp=0;x--;}else   {x=0;beep=0;} }  a=x/10;             b=x%10; display(a,b);   }} void delay(uchar i){  for(j=i;j>0;j--)    for(k=125;k>0;k--);}void keyscan(){   P3=0xfe;temp=P3;temp=temp&0xf0;if(temp!=0xf0) {delay(50);if(temp!=0xf0) {switch(temp){case 0xe0: {num=1;ok=1;}break;case 0xd0: {num=2;ok=1;}break;case 0xb0: {num=3;ok=1;}break;case 0x70: {num=4;ok=1;}break;} } }P3=0xfd;temp=P3;temp=temp&0xf0;if(temp!=0xf0) {delay(50);if(temp!=0xf0) {switch(temp){case 0xe0: {num=5;ok=1;}break;case 0xd0: {num=6;ok=1;}break;case 0xb0: {num=7;ok=1;}break;case 0x70: {num=8;ok=1;}break;} } }P3=0xfb;temp=P3;temp=temp&0xf0;if(temp!=0xf0) {delay(50);if(temp!=0xf0) {switch(temp){case 0xe0: {num=9;ok=1;}break;case 0xd0: {num=10;ok=1;}break;case 0xb0: {num=11;ok=1;}break;case 0x70: {num=12;ok=1;}break;} } }P3=0xf7;temp=P3;temp=temp&0xf0;if(temp!=0xf0) {delay(50);if(temp!=0xf0) {switch(temp){case 0xe0: {num=13;ok=1;}break;case 0xd0: {num=14;ok=1;}break;case 0xb0: {num=15;ok=1;}break;case 0x70: {num=16;ok=1;}  break;  } } }}void display(uchar shi,uchar ge){    dula=0;   P0=table[shi];   dula=1;   dula=0;      wela=0;   P0=0xfe;   wela=1;   wela=0;   delay(5);   dula=0;   P0=table[ge];   dula=1;   dula=0;      wela=0;   P0=0xfd;   wela=1;   wela=0;   delay(5);}void time0() interrupt 1{TH0=(65536-46080)/256;TL0=(65536-46080)%256;pp++;}