#include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit we = P2^7; sbit duan = P2^6; uint i; uint num; 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, //熄灭 0x00 //自定义 };//十六进制代码 void delay(uint z)//延时函数 { uint x,y; for(x = z;x > 0;x--) for(y = 114;y > 0;y--); } uchar KeyScan()//带返回值的按键扫描子函数 { uchar cord_h,cord_l;//声明列线和行线的值的储存变量 P3 = 0xf0;//1111 0000 if((P3 & 0xf0) != 0xf0)//判断是否有按键被按下 { delay(5);//软件消抖 if((P3 & 0xf0) != 0xf0)//判断是否有按键被按下 { i++;//按键次数加一 cord_l = P3 & 0xf0;//把列线值保存起来 P3 = cord_l | 0x0f;//将低四位全部赋1,同时高四位保持不变 cord_h = P3 & 0x0f;//把行线值保存起来 while((P3 & 0x0f) != 0x0f)//松手检测 不等于 即为1 等于 为0 跳出while循环 return(cord_l + cord_h);//返回键值码 } } } void KeyPro() { switch(KeyScan()) { case 0xee:num = leddata[1]; break; case 0xde:num = leddata[2]; break; case 0xbe:num = leddata[3]; break;//第一行键值码 case 0xed:num = leddata[4]; break; case 0xdd:num = leddata[5]; break; case 0xbd:num = leddata[6]; break;//第二行键值码 case 0xeb:num = leddata[7]; break; case 0xdb:num = leddata[8]; break; case 0xbb:num = leddata[9]; break;//第三行键值码 case 0xd7:num = leddata[0]; break;//第四行键值码 } } |