⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 键盘扫描4.4.c

📁 本程序是一个4*4键盘扫描程序. 可以用于初学者对于单片机的开发.
💻 C
字号:
#include<reg51.h>
#define uchar unsigned char

sbit P0_4=p0^4
sbit P0_5=p0^5
sbit P0_6=p0^6
sbit P0_7=p0^7

uchar  code display[]={0x3f,0x06,0x5b,0x4f, 
0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar dispbit[]={0x01,0x02};
uchar discount=0;
uchar dispbuff[2];
uchar temp,key;


uchar keyscan(void);
void waitkey(void);

uchar keyscan(void)
{uchar temp;
 P0=0xff;
 P0_4=1;      //?
 temp=P0;
 temp=temp|0xf0;
 if(temp!=0xf0)
  {delay10ms();
   temp=P0;
   temp=temp|0xf0;
   switch(temp)
   {case 0xf1:key=0;break;
    case 0xf2:key=4;break;
    case 0xf4:key=8;break;
    case 0xf8:key=12;break;
   }
  }
P0=0xff;
P0_5=1;      //?测试第2列
 temp=P0;
 temp=temp|0xf0;
 if(temp!=0xf0)
  {delay10ms();
   temp=P0;
   temp=temp|0xf0;
   switch(temp)
   {case 0xf1:key=1;break;
    case 0xf2:key=5;break;
    case 0xf4:key=9;break;
    case 0xf8:key=13;break;
   }
  }
P0=0xff;
P0_6=1;      //?
 temp=P0;
 temp=temp|0xf0;
 if(temp!=0xf0)
  {delay10ms();
   temp=P0;
   temp=temp|0xf0;
   switch(temp)
   {case 0xf1:key=2;break;
    case 0xf2:key=6;break;
    case 0xf4:key=10;break;
    case 0xf8:key=14;break;
   }
  }
P0=0xff;
P0_7=1;      //?
 temp=P0;
 temp=temp|0xf0;
 if(temp!=0xf0)
  {delay10ms();
   temp=P0;
   temp=temp|0xf0;
   switch(temp)
   {case 0xf1:key=3;break;
    case 0xf2:key=7;break;
    case 0xf4:key=11;break;
    case 0xf8:key=15;break;
   }
  }
}
void wait(void)
{temp=P0;
 temp=temp|0xf0;
 if(temp!=0xf0)
  {
   temp=P0;
   temp=temp|0xf0;
   }
}

void delay10ms(uchar T)
{uchar a,b;
 for(a=0;a<T;a++)
    for(b=0;b<1000;b++)
   ;
}
 ///////////////////////////////////////////////// 
//定时器1中断服务程序
//功能:将键值送出显示
/////////////////////////////////////////////////   
void time1() interrupt 3  using 0  
{
  TH1=0xec;
  TL1=0x78;
  dispbuff[1]=key/10;
  dispbuff[0]=key%10;
  P1=display[dispbuff[discount]];
  P2=dispbit[discount];
  discount++;
  if(discount==2)
  discount=0;
}
/////////////////////////////////////////////////
void main(void)
{
  TMOD=0x11;
  TH1=0xec;
  TL1=0x78;
  EA=1;
  ET1=1;
  TR1=1;
  while(1)
  {
    keyscan();
    waitkey();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -