幕布链接
https://www.mubucm.com/doc/CAXAv_OT2a
参考《零基础学51单片机(C语言版)》
16键矩阵键盘行列扫描方式输入及显示
#include<reg51.h>
sbit DuanSuo=P2^2;
sbit WeiSuo =P2^3;
#define WDM P0
#define KeyP1 P1
unsigned char code DMtable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code WMtable[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char TData[8];
void DelayUs(unsigned char tu);
void DelayMs(unsigned char tm);
void Display(unsigned char ShiWei,unsigned char WeiShu);
void T0Int_S(void);
unsigned char KeyS(void);
unsigned char KeyZ(void);
void main (void)
{
unsigned char num,i,j;
T0Int_S();
while(1)
{
num=KeyZ();
if(num!=0xff)
{
if(i<8)
{
TData=DMtable[num];
}
i++;
if(i==9)
{
for(j=0;j<8;j++)
{
TData[j]=0;
}
i=0;
}
}
}
}
void Display(unsigned char ShiWei,unsigned char WeiShu)
{
static unsigned char i;
WDM=WMtable[i+ShiWei];
WeiSuo=1;
WeiSuo=0;
WDM=TData;
DuanSuo=1;
DuanSuo=0;
i++;
if(i==WeiSuo)
{
i=0;
}
}
void T0Int_S(void)
{
TMOD=0x01;
Th0=0;
Tl0=0;
EA =1;
ET0=1;
TR0=1;
}
void T0Int_Z(void) interrupt 1
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
Display(0,8);
}
unsigned char KeyS(void)
{
unsigned char KeyM;
KeyP1=0xf0;
if(KeyP1!=0xf0)
{
DelayMs(10);
if(KeyP1!=0xf0)
{
KeyP1=0xfe;
if(KeyP1!=0xfe)
{
KeyM=KeyP1;
DelayMs(10);
while(KeyP1!=0xfe);
return KeyM;
}
KeyP1=0xfd;
if(KeyP1!=0xfd£©
{
KeyM=KeyP1;
DelayMs(10);
while(KeyP1!=0xfd);
return KeyM;
}
KeyP1=0xfb;
if(KeyP1!=0xfb£©
{
KeyM=KeyP1;
DelayMs(10);
while(KeyP1!=0xfb);
return KeyM;
}
KeyP1=0xf7;
if(KeyP1!=0xf7£©
{
KeyM=KeyP1;
DelayMs(10);
while(KeyP1!=0xf7);
return KeyM;
}
}
}
return 0xff;
}
unsigned char KeyZ(void)
{
switch(KeyS())
{
case 0x7e:return 0;break;
case 0x7d:return 1;break;
case 0x7b:return 2;break;
case 0x77:return 3;break;
case 0xbe:return 4;break;
case 0xbd:return 5;break;
case 0xbb:return 6;break;
case 0xb7:return 7;break;
case 0xde:return 8;break;
case 0xdd:return 9;break;
case 0xdb:return 10;break;
case 0xd7:return 11;break;
case 0xee:return 12;break;
case 0xed:return 13; break;
case 0xeb:return 14; break;
case 0xe7:return 15; break;
default:return 0xff;break;
}
}
|