书接上回;http://t.csdnimg.cn/T4b0W
键盘位置如上
效果图
因为是四位,就没显示等于号, 而且段数比较少 显示不出来加号 所以 A = + ; B = - ; C = *; D =/ 基本原来都一样.
代码奉上
#include <reg51.h>
#include <STRING.H>
//利用矩阵键盘按键按下依次在数码管显示0-F。
/*
接线:
矩阵键盘:P3端口
数码管: P0端口
*/
#define KEY P3
#define DIG P0
void delay10ms(void);
unsigned char GetKey(void);
void CheckKeyValue(unsigned char);
unsigned char StrToNum(unsigned char);
void Refresh(); //刷新
void Equal(); //等于
//void ShowNum(); //显示数码管
void delay(unsigned int t)
{
unsigned int i, j;
for (i = 0; i < t; i++) {
for (j = 0; j < 1275; j++) {
; // 空循环,等待时间过去
}
}
}
//计算机显示数组
unsigned char numShow[] = {0x01,0x02,0x03,0x04};
unsigned char numKey = 0;
unsigned char operationKey;
//独立数码管的显示0-F
unsigned char leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00 //熄灭
};
unsigned char keynum = 0;
unsigned char keyBool = 1;
void main(void)
{
unsigned char Key = 0;
unsigned char i = 0;
//P2=0;
while(1)
{
Key = GetKey();
if ((KEY == 0x0e || KEY == 0x0d||KEY == 0x0b||KEY == 0x07||KEY == 0xe0
||KEY == 0xd0||KEY == 0xb0||KEY == 0x70)&&keyBool == 0 )
{
//keyBool = 1 ;
}
else
{
keyBool = 1 ;
}
for ( i = 0; i < 5; i++)
{
switch (i)
{
case 0:
P0 =leddata[numShow[0]];
P2 =0xFE; //1111 1110
break;
case 1:
P0 = leddata[numShow[1]];
P2 =0xFD;//1111 1101
break;
case 2:
P0 =leddata[numShow[2]];
P2 =0xFB;//1111 1011
break;
case 3:
P0 =leddata[numShow[3]];
P2 =0xF7;//1111 0111
break;
default:
break;
}
delay(1);
P0=0x00;
}
}
}
void delay10ms(void) //误差 0us
{
unsigned char a,b,c;
for(c=5;c>0;c--)
for(b=4;b>0;b--)
for(a=248;a>0;a--);
}
unsigned char GetKey(void)
{
unsigned char KeyValue = 0;
unsigned char hang = 0,lie = 0;
unsigned char flag = 0;
KEY = 0x0f;
if(KEY != 0x0f && keyBool == 1 )
{
keyBool = 0;
delay10ms();
switch (KEY)
{
case 0x0e: hang = 1; break; //0000 1110
case 0x0d: hang = 2; break; //0000 1101
case 0x0b: hang = 3; break; //0000 1011
case 0x07: hang = 4; break; //0000 0111
default:break;
}
KEY = 0xf0;
if(KEY != 0xf0)
{
switch (KEY)
{
case 0xe0: lie = 1; break;//1110 0000
case 0xd0: lie = 2; break;//1101 0000
case 0xb0: lie = 3; break;//1011 0000
case 0x70: lie = 4; break;//0111 0000
default:break;
}
KeyValue = (hang - 1)*4 + lie;
CheckKeyValue(KeyValue);
}
return KeyValue;
}
return 0;
}
void CheckKeyValue(unsigned char keyvalue)
{
switch (keyvalue)
{
case 1:
numShow[keynum] = 1;
break;
case 2:
numShow[keynum] = 2;
break;
case 3:
numShow[keynum] = 3;
break;
case 4:
operationKey = 10;//+
numShow[keynum] = 10;
break;
case 5:
numShow[keynum] = 4;
break;
case 6:
numShow[keynum] = 5;
break;
case 7:
numShow[keynum] = 6;
break;
case 8:
operationKey = 11;// /
numShow[keynum] = 11;
break;
case 9:
numShow[keynum] = 7;
break;
case 10:
numShow[keynum] = 8;
break;
case 11:
numShow[keynum] = 9;
break;
case 12:
operationKey = 12;// /
numShow[keynum] = 12;// *
break;
case 13:
//刷新
Refresh();
break;
case 14:
numShow[keynum] = 0;
break;
case 15:
//等于
Equal();
break;
case 16:
operationKey = 13;// /
numShow[keynum] = 13; // /
break;
default:
break;
}
++keynum;
//ShowNum();
}
void Refresh()
{
keynum = -1;
numShow[0] = 0x00;
numShow[1] = 0x00;
numShow[2] = 0x00;
numShow[3] = 0x00;
}
void Equal()
{
switch (operationKey)
{
case 10:
numShow[3] =numShow[0] + numShow[2];
break;
case 11:
numShow[3] = numShow[0] - numShow[2];
break;
case 12:
numShow[3] = numShow[0] * numShow[2];
break;
case 13:
numShow[3] = numShow[0] / numShow[2];
break;
default:
numShow[3] = operationKey;
//Refresh();
break;
}
//ShowNum();
}
//字符串转化为数字
unsigned char StrToNum(unsigned char dig){
unsigned int num = 0;
switch (dig)
{
case '0':
num = 0;
break;
case '1':
num = 1;
break;
case '2':
num = 2;
break;
case '3':
num = 3;
break;
case '4':
num = 4;
break;
case '5':
num = 5;
break;
case '6':
num = 6;
break;
case '7':
num = 7;
break;
case '8':
num = 8;
break;
case '9':
num = 9;
break;
default:
break;
}
return(num);
}