按键功能实现不了

2019-03-24 18:25发布

#include<reg51.h>
#include<stdlib.h>
#define uchar unsigned char
#define uint  unsigned int
  sbit key1=P0^1;         //显示‘1’按下
  sbit key2=P0^3;          //显示‘2’按下
  sbit key3=P0^5;          //显示‘3’按下
  sbit key4=P0^7;           //显示‘4’按下
  sbit over=P2^0;                 //结束标志

  #define temp1 1
  #define temp2 2
  #define temp3 3
  #define temp4 4
uchar c,count,count1,count2,temp,num;
uchar code table[]={ 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};   //数码管显示
  void delayms(uchar xms)             //延时函数
  { uchar i,j;
    for(i=xms;i>0;i--)
          for(j=110;j>0;j--);
          }
void  c_to_port(uchar c)          //发送单个字符
  { SBUF=c;
    while(TI==0);
        TI=0;
           }
  void         s_to_port(uchar *s)            //发送字符串
  {  while(*s!='')
      { c_to_port(*s);
            s++;
            delayms(5);
          }
        }

        void keyscan( uchar temp)         //键盘扫描
        {           count1=0;
        if( key1==0)
              { delayms(10);
                       
                     if(key1==0)
                           {   if( temp1==temp)
                                    count1++;          //判断正确,count1+1
                                   else
                                   { count2++;                   //判断错误,count2+1
                                    if(count2==3)            //错误超过两次,结束
                                         over=0;                 //低电平,P2^0口LED亮                  
                                         }         
                                }
                         while(!key1);                
                  }
          if( key2==0)
              { delayms(10);
                     if(key2==0)
                           {   if(temp2==temp)
                                count1++;
                                   else
                                   { count2++;
                                     if(count2==3)
                                         over=0;
                                   }          
                                }
                            while(!key2);             
                  }
           if( key3==0)
              { delayms(10);
                     if(key3==0)
                           { if( temp3==temp)
                                    count1++;
                                 else
                                   { count2++;
                                     if(count2==3)
                                      over=0;
                                   }               
                                }
                            while(!key3);      
                  }                                                                                           
           if( key4==0)
              { delayms(10);
                     if(key4==0)
                           {   if( temp4==temp)
                                    count1++;
                                   else
                                   { count2++;
                                   if(count2==3)
                    over=0;       
                                   }
                           }
                            while(!key4);             
                  }
            count=count1;
        }

        void main()
        { P0=0xaa;         //4个按键,初始值置高电平
          over=1;
          SCON=0x40;
      TMOD=0x21;
          TH1=0xfd;
          TL1=0xfd;
          TH0=(65536-45872)/256;
          TL0=(65536-45872)%256;  //定时50ms
          PCON=0x00;
          TI=0;
          TR1=1;
          TR0=1;
          ET0=1;
          EA=1;
          delayms(100);
           while(1)
                {   if(num==60)           //延时3s
                      { num=0;
                            c=rand()%4;          //产生随机数
                            c_to_port(c+'1');  //发送给主机
                                temp=c+1;          //与按下的对应键对比
                                keyscan(temp);
                           P1=table[count-1]; //显示正确的个数
                          }
            }
       
        }
          
void T0_time() interrupt 1             //定时器T0中断
         { TH0=(65536-45872)/256;
           TL0=(65536-45872)%256;
           num++;
         } 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。