原创 3×3矩阵键盘,分别让按键控制LED显示0~8

2009-3-28 21:47 3679 6 6 分类: MCU/ 嵌入式

#include<reg52.h>


sbit   led1=P2^0;


sbit   led2=P2^1;


sbit   led3=P2^2;


sbit   led4=P2^3;


void  delayms(unsigned   int    x);


void  matrixkeycan();


void  display(unsigned     char num);


unsigned   char code       table[]={


0xc0,0xf9,0xa4,0xb0,0x99,0x92,


0x82,0xf8,0x80,0x90,0x88,0x83,


0xc6,0xa1,0x86,0x8e,0x00


};//共阳级数码管字符表


void  main()


{


led1=0;


led2=0;


led3=0;


led4=0;//打开4个数码管


while(1)


{


        matrixkeycan();


}


}


void  delayms(unsigned   int    x)//延时程序


{


unsigned  i,j;


for(i=x;i>0;i--)


        for(j=110;j>0;j--);


}


void  display(unsigned     char num)


{


P0=table[num];


}//显示程序


void  matrixkeycan()      


{


unsigned  char temp,key;



P1=0x3e;//确定行,111110,使行低电平


temp=P1;


temp=temp&0x38;//111000与运算,如果按键按下,111000会变化,否则不变


if(temp!=0x38)


{


        delayms(10);//去抖动


        temp=P1;


        temp=temp&0x38;


        if(temp!=0x38)//按键按下


        {


               temp=P1;


               switch(temp)


               {


                      case 0x1e:


                                    key=0;


                                    break;


                      case 0x2e:


                                    key=1;


                                    break;


                      case 0x36:


                                    key=2;


                                    break;//分别确定3


               }


               while(temp!=0x38)


                                    {


                                           temp=P1;


                                           temp=temp&0x38;


                                    }


        }


               display(key);


}


P1=0x3d;


temp=P1;


temp=temp&0x38;


if(temp!=0x38)


{


        temp=P1;


        switch(temp)


        {


               case 0x1d:


                             key=3;


                             break;


               case 0x2d:


                             key=4;


                             break;


               case 0x35:


                             key=5;


                             break;


        }


        while(temp!=0x38)


        {


               temp=P1;


               temp=temp&0x38;


        }


        display(key);


}


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 


P1=0x3b;


temp=P1;


temp=temp&0x38;


if(temp!=0x38)


{


        delayms(10);


        temp=P1;


        temp=temp&0x38;


        if(temp!=0x38)


        {


               temp=P1;


               switch(temp)


               {


               case 0x33:


                             key=8;


                             break;


               case 0x2b:


                             key=7;


                             break;


               case 0x1b:


                             key=6;


                             break;


               }


        }


        while(temp!=0x38)


        {


               temp=P1;


               temp=temp&0x38;


        }


       display(key);


}     

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /3 下一条