#include "reg52.h"
sfr P4=0xC0;
sbit YMQ_A=P2^5;
sbit YMQ_B=P2^6;
sbit YMQ_C=P2^7;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C1=P4^4;
sbit C2=P4^2;
sbit C3=P3^5;
sbit C4=P3^4;
unsigned char key_num;
int SMG[18]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0x88,0x83,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
void InitHC138(unsigned char n)
{
switch(n)
{
case 4:
YMQ_C=1;
YMQ_B=0;
YMQ_A=0;
break;
case 5:
YMQ_C=1;
YMQ_B=0;
YMQ_A=1;
break;
case 6:
YMQ_C=1;
YMQ_B=1;
YMQ_A=0;
break;
case 7:
YMQ_C=1;
YMQ_B=1;
YMQ_A=1;
break;
}
}
void DisplayKeyNum(unsigned char value)
{
InitHC138(6);
P0=0x01;
InitHC138(7);
P0=value;
}
void ScanKeysMulti()
{
R1=0;
R2=R3=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
while(C1==0);
key_num=0;
DisplayKeyNum(SMG[key_num]);
}
else if(C2==0)
{
while(C2==0);
key_num=1;
DisplayKeyNum(SMG[key_num]);
}
else if(C3==0)
{
while(C3==0);
key_num=2;
DisplayKeyNum(SMG[key_num]);
}
else if(C4==0)
{
while(C4==0);
key_num=3;
DisplayKeyNum(SMG[key_num]);
}
R2=0;
R1=R3=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
while(C1==0);
key_num=4;
DisplayKeyNum(SMG[key_num]);
}
else if(C2==0)
{
while(C2==0);
key_num=5;
DisplayKeyNum(SMG[key_num]);
}
else if(C3==0)
{
while(C3==0);
key_num=6;
DisplayKeyNum(SMG[key_num]);
}
else if(C4==0)
{
while(C4==0);
key_num=7;
DisplayKeyNum(SMG[key_num]);
}
R3=0;
R2=R1=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
while(C1==0);
key_num=8;
DisplayKeyNum(SMG[key_num]);
}
else if(C2==0)
{
while(C2==0);
key_num=9;
DisplayKeyNum(SMG[key_num]);
}
else if(C3==0)
{
while(C3==0);
key_num=10;
DisplayKeyNum(SMG[key_num]);
}
else if(C4==0)
{
while(C4==0);
key_num=11;
DisplayKeyNum(SMG[key_num]);
}
R4=0;
R2=R3=R1=1;
C1=C2=C3=C4=1;
if(C1==0)
{
while(C1==0);
key_num=12;
DisplayKeyNum(SMG[key_num]);
}
else if(C2==0)
{
while(C2==0);
key_num=13;
DisplayKeyNum(SMG[key_num]);
}
else if(C3==0)
{
while(C3==0);
key_num=14;
DisplayKeyNum(SMG[key_num]);
}
else if(C4==0)
{
while(C4==0);
key_num=15;
DisplayKeyNum(SMG[key_num]);
}
}
void main()
{
while(1)
{
ScanKeysMulti();
}
}