玩电子 > 自动控制
+

矩阵键盘C语言程序+电路图

2021-12-16    作者:未知    来源:网络文摘

    下面是51单片机驱动矩阵键盘的电路图 采用一位数码管显示 按键的结果
 下面是c语言源程序:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint led[]={0xc0,0xf9,0xa4, 0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e}; 
uchar a,b,c,temp;
void delay7ms(void)   //误差 -0.217013888891us
{
    unsigned char a,b;
    for(b=208;b>0;b--)
        for(a=14;a>0;a--);
}
uchar key()
{
  P1=0x0f;
    if(P1!=0x0f)
     {
       delay7ms();
       if(P1!=0x0f);

       P1=0xfe;
       a=P1^0xfe;
       switch(a)
        {
         case 0x80:c=0; break;
          case 0x40:c=1; break;
          case 0x20:c=2; break;
          case 0x10:c=3; break; 
      }
   temp=P1;
   while(temp!=0xfe)
    {
   temp=P1;
    }
   }
   
    P1=0x0f;
    if(P1!=0x0f)
     {
       delay7ms();
       if(P1!=0x0f);
       P1=0xfd;
       a=P1^0xfd;
       switch(a)
        {
         case 0x80:c=4; break;
          case 0x40:c=5; break;
          case 0x20:c=6; break;
          case 0x10:c=7; break;
      }
   temp=P1;
   while(temp!=0xfd)
    {
   temp=P1;
    }
   }
   
  P1=0x0f;
    if(P1!=0x0f)
     {
       delay7ms();
       if(P1!=0x0f);
       P1=0xfb;
       a=P1^0xfb;
       switch(a)
        {
          case 0x80:c=8; break;
          case 0x40:c=9; break;
          case 0x20:c=10; break;
          case 0x10:c=11; break;
     }
  temp=P1;
   while(temp!=0xfb)
    {
   temp=P1;
    }
   }
   P1=0x0f;
    if(P1!=0x0f)
     {
       delay7ms();
       if(P1!=0x0f);
       P1=0xf7;
       a=P1^0xf7;
       switch(a)
        {
       case 0x80:c=12; break;
          case 0x40:c=13; break;
          case 0x20:c=14; break;
          case 0x10:c=15; break;
         }
    temp=P1;
   while(temp!=0xf7)
    {
   temp=P1;
    }
   }
 return c;
}
void main(void)
{
 while(1)
  {
  P2=led[b];
    b=key();      
   }
}
阅读:706    评论:0
  • 相关文章
  • 热门文章
  • 相关评论
网站地图

免责申明:我要玩起网旨在提供一个相互学习交流的平台,是一个完全免费的网站,部分原创作品,欢迎转载,部分内容来自互联网,如果侵犯了您的权利请尽快通知我们!邮箱:279459762@qq.com Copyright 2018-2021我要玩起网

  湘ICP备17006802号-2
【电脑版】  【回到顶部】