#include<reg52.h> #define uchar unsigned char #define uint unsigned int //sbit key1=P1^0; //sbit key2=P1^1; //sbit key3=P1^2; //sbit key4=P1^3; //sbit key5=P1^4; //sbit key6=P1^5; //sbit key7=P1^6; //sbit key8=P1^7; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //数组0到F uchar code table1[]={0,1,2,3,4,5,6,7}; uint key; void delayms(uchar k) { uchar i,j; for(i=k;i>0;i--) for(j=125;j>0;j--); } void display(uint vaule) //显示子函数 {P2=table1[0]; P0=table[vaule]; //uint bai,shi,ge; // bai=vaule/100; //shi=vaule/10%10; //ge=vaule%10; // P0=table[ge]; //送段选数据 //// dula=1; // P2=table1[2]; //送位选数据 // delay(50); //// dula=0; // P0=table[shi]; //// dula=1; // P2=table1[1]; // delay(50); //// dula=0; // P0=table[bai]; //// dula=1; // P2=table1[0]; // delay(50); } void keyscan() //按键子函数 { uint X,Y,Z; P1=0X0F;//行扫描 if(P1!=0X0F) { if(P1!=0X0F) {delayms(50); X=P1&0X0F; //取出行扫描键值 P1=0XF0;//列扫描 Y=P1&0XF0;//取出列扫描键值 Z=X|Y;//取出行扫描列扫描合并值 switch(Z) { case 0X7E: key=0; break; case 0XBE: key=1; break; case 0XDE: key=2; break; case 0XEE: key=3; break; // case 0X7D: key=4; break; case 0XBD: key=5; break; case 0XDD: key=6; break; case 0XED: key=7; break; // case 0X7B: key=8; break; case 0XBB: key=9; break; case 0XDB: key=10; break; case 0XEB: key=11; break; case 0X77: key=12; break; case 0XB7: key=13; break; case 0XD7: key=14; break; case 0XE7: key=15; break; }; // break; } // P1=0X0F; // while(P1!=0X0F); } } void main() { while(1) { keyscan(); display(key); } } |