zhulinshenchu 发表于 2012-2-18 16:56:57

16路抢答器自己写的,分享下

#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code led = {0,1,2,3,4,5,6,7};
uchar num,second,temp,count,start_flag = 0,reset_flag,keyflag = 0,stop_flag=0,keylock,sounderflag;
sbit start= P3^2;
sbit fu= P3^1;
sbit stop= P3^4;
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;
sbit key5 = P1^4;
sbit key6 = P1^5;
sbit key7 = P1^6;
sbit key8 = P1^7;
sbit buzzer = P3^5;
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar num,uchar second)
{
uchar shi,ge;
shi=second/10;
ge=second%10;
P2=led;
P0=table;
delay(2);
P2=led;
P0=table;
delay(2);
P2=led;
P0=table;
delay(2);
}
void sounder()
{
buzzer=1;
delay(10);
buzzer=0;
delay(10);
buzzer=1;
delay(10);
buzzer=0;
delay(10);
buzzer=1;
delay(10);
buzzer=0;
delay(10);
}
uchar keyscan()
{
      P1=0xfe;
       temp=P1;
       temp=temp&0xf0;
    while(temp!=0xf0)
       {
           delay(2);
               temp=P1;
               temp=temp&0xf0;
                while(temp!=0xf0)
               {       
                  keyflag=1;
                  temp=P1;
                       switch(temp)
                       {
                           case 0xee: num=1;
                                   break;
                           case 0xde: num=2;
                                   break;
                           case 0xbe: num=3;
                                   break;
                 case 0x7e: num=4;
                                   break;
                       }
                       while(temp!=0xf0)
                       {
                          temp=P1;
                          temp=temp&0xf0;
                       }
               }
       }
       //
    P1=0xfd;
       temp=P1;
       temp=temp&0xf0;
    while(temp!=0xf0)
       {
           delay(2);
               temp=P1;
               temp=temp&0xf0;
                while(temp!=0xf0)
               {
             keyflag=1;
                 temp=P1;
                       switch(temp)
                       {
                           case 0xed: num=5;
                                   break;
                           case 0xdd: num=6;
                                   break;
                           case 0xbd: num=7;
                                   break;
                 case 0x7d: num=8;
                                   break;
                       }
                       while(temp!=0xf0)
                       {
                          temp=P1;
                          temp=temp&0xf0;
                       }
               }
       }
       //
    P1=0xfb;
       temp=P1;
       temp=temp&0xf0;
    while(temp!=0xf0)
       {
           delay(2);
               temp=P1;
               temp=temp&0xf0;
                while(temp!=0xf0)
               {       
                   keyflag=1;
                  temp=P1;
                       switch(temp)
                       {
                           case 0xeb: num=9;
                                   break;
                           case 0xdb: num=10;
                                   break;
                           case 0xbb: num=11;
                                   break;
                 case 0x7b: num=12;
                                   break;
                       }
                       while(temp!=0xf0)
                       {
                          temp=P1;
                          temp=temp&0xf0;
                       }
               }
       }
       //
    P1=0xf7;
       temp=P1;
       temp=temp&0xf0;
    while(temp!=0xf0)
       {
           delay(2);
               temp=P1;
               temp=temp&0xf0;
                while(temp!=0xf0)
               {
             keyflag=1;
                  temp=P1;
                       switch(temp)
                       {
                           case 0xe7: num=13;
                                   break;
                           case 0xd7: num=14;
                                   break;
                           case 0xb7: num=15;
                                   break;
                 case 0x77: num=16;
                                   break;
                       }
                       while(temp!=0xf0)
                       {
                          temp=P1;
                          temp=temp&0xf0;
                       }
               }
       }
return num;
}

void start1()
{
if(start==0)
   {
   delay(5);
   {
       if(start==0)
         {
          start_flag=1;
          TR0=1;
         }
   }
   }
}
void stop1()
{
    if(stop==0)
   {
   delay(5);
   {
       if(stop==0)
         {

          stop_flag=1;
          TR0=0;
         }
   }
   }
}

void init()
{
start_flag=0;
stop_flag=0;
num=0;
second=20;
count=0;
fu=0;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=0;
P1=0XFF;
}
void main()
{
init();
while(1)
{
        stop1();
   start1();
   if((start_flag==1)&&(keyflag==0)&&(stop_flag!=1))
    {
   num=keyscan();
    }
   display(num,second);
        if((num==0)&&(sounderflag==1))
          sounder();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
   {
   count=0;
   if(second!=0)
   second--;
          if(second==5)
           sounderflag=1;
   }
}

caiseyuzhou 发表于 2012-4-13 22:43:24

没有硬件啊?这个
页: [1]
查看完整版本: 16路抢答器自己写的,分享下