標題: (程序共享)矩陣鍵盤 [打印本頁]

作者: mafu    時間: 2015-5-7 16:20
標題: (程序共享)矩陣鍵盤
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
    0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
    0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8e
};  
uchar num,temp;
void delay(uint z)//延時函數(shù)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void main()
{
        P2=0xff;
        P0=0xff;
        P1=0xff;                                               //初始化
        while(1)
        {
                P1=0xfe;                                //給矩陣按鍵賦初值

                temp=P1;
                temp=temp&0xf0;                //按位與
                if(temp!=0xf0)                    //判斷鍵盤是否按下去,如果按下去,則執(zhí)行括號里的內(nèi)容
                {
                        delay(5);

                        temp=P1;            
                        temp=temp&0xf0;   //再次判斷
                        if(temp!=0xf0)       //確認按下去以后,執(zhí)行括號里的內(nèi)容
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xee:num=1;
                                        break;
                                        case 0xde:num=2;
                                        break;
                                        case 0xbe:num=3;
                                        break;
                                        case 0x7e:num=4;
                                        break;
                                }
                                P2=0;
                          P0=table[num-1];       
                        }
                 
                 }
                 P1=0xfd;

                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(5);

                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xed:num=5;
                                        break;
                                        case 0xdd:num=6;
                                        break;
                                        case 0xbd:num=7;
                                        break;
                                        case 0x7d:num=8;
                                        break;
                                }
                                P2=0;
                        P0=table[num-1];       
                        }
                 
                 }

                P1=0xfb;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(5);

                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xeb:num=9;
                                        break;
                                        case 0xdb:num=10;
                                        break;
                                        case 0xbb:num=11;
                                        break;
                                        case 0x7b:num=12;
                                        break;
                                }
                                P2=0;
                        P0=table[num-1];       
                        }
                 
                 }

                P1=0xfe;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(5);

                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xe7:num=13;
                                        break;
                                        case 0xd7:num=14;
                                        break;
                                        case 0xb7:num=15;
                                        break;
                                        case 0x77:num=16;
                                        break;
                                }
                                P2=0;
                        P0=table[num-1];       
                        }
                 
                 }       
        }
}







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1