原创 51单片机复习札记之四——按键

2010-7-18 11:05 2311 12 12 分类: MCU/ 嵌入式

独立按键


我的开发板上独立按键有4个,分别是接在P3.2~P3.5上,当有键按下时,该端口被拉到低电平。按键在使用时会产生机械抖动,这里采用软件延时的方法去除抖动。


下面的程序是扫描接到P3.2上的按键,即S15,每按一次,数码管第一位从F开始减一递减,当减到0时,再回到F。


 


#include <reg52.h>


#include "Config.h"


 


//数码管段选和位选信号


sbit Seg = P2^6;


sbit Bit = P2^7;


//数码管0~F字形码


code uchar LED[] = {0x7B,0x42,0x1F,0x4F,0x66,0x6D,0x7D,0x43,


                            //     0  
1   2   3   
4    5    6   
7                                                                           0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x3D,0x35};


                            //    8    9   A    B   
C   D   E   F


 


uchar c = 0;    //全局变量


 


/********************


函数名称:Delay


函数功能:延时


入口参数:uint t—要延时的毫秒数


返回参数:无


备    注:无


*********************/


void Delay(uint t)


{


       uint
i;


       while(t--)


       {


              for
(i=0;i<125;i++)


              {}


       }


}


//********End of Function************


 


/********************


函数名称:KeyScan


函数功能:按键扫描


入口参数:无


返回参数:无


备    注:无


*********************/


void KeyScan(void)


{


       if((P3&0xFF)
== 0xFB) //定点循环扫描该按键


       {


              P3
= 0xFF;


              Delay(50);             //延时去抖动


              if((P3&0xFF)
== 0xFB)


              {


                     if(c>=15)


                            c
= 0;


                     else


                            c++;


              }


       }


       else


       {


              P3
= 0xFF;


       }


}


 


void main(void)


{


       P3
= 0xFF; //初始化为输入形式


      


       while(1)


       {


              KeyScan();


              Bit
= 1;


              P0
= LED[15-c];   


              Bit
= 0;


 


              Seg
= 1;


              P0
= 0xEF;


              Seg
= 0;


              Delay(50);


       }


}


 


矩阵键盘


我的开发板上有由16个按键组成的4×4矩阵键盘:行接到P3.0~P3.3,列接到P3.4~P3.7。


下面的程序中,定义这16个按键依次为0~F,当对应的按键按下时,由数码管进行显示。


#include "KeyPad.h"


 


 


/********************


函数名称:Delay


函数功能:延时


入口参数:uint t—要延时的毫秒数


返回参数:无


备    注:无


*********************/


void Delay(uint t)


{


       uint
i;


       while(t--)


       {


              for
(i=0;i<125;i++)


              {}


       }


}


//********End of Function************


 


/********************


函数名称:KeyScan


函数功能:键盘扫描


入口参数:无


返回参数:uchar—扫描到键值


备    注:无


*********************/


uchar KeyScan(void)


{


       uchar
KeyValue;


       uchar
ScanCode;


       P3
= 0xF0;                   //全0行扫描码


       if((P3&0xF0)!=0xF0)    //若有键按下 


       {


              Delay(20);
           //延时去抖


              if((P3&0xF0)!=0xF0)    //确有键按下


              {


                     ScanCode
= 0xFE;


                     while(ScanCode
!= 0xEF)     //逐行扫描


                     {


                            P3
= ScanCode;            //发送扫描码


                            if((P3&0xF0)!=0xF0)    //本行有键按下           


                            {


                                   KeyValue
= P3;


                                   return
KeyValue;     //返回扫描码


                            }    


                            else


                            {


                                   ScanCode
= _crol_(ScanCode,1);


                                   //本行没有键按下,扫描码左移1位,继续扫描


                            }


                     }


              }


       }


       return
0; 


}


//********End of Function************


 


/********************


函数名称:GetCode


函数功能:根据扫描到的键值得到要显示的字型码


入口参数:uchar i—扫描到的键值


返回参数:uchar—要送数码管显示的字型码


备    注:无


*********************/


uchar GetCode(uchar i)


{


       uchar
num;


       switch(i)


       {


              case
0xEE:      num = LED[0];      break;


              case
0xDE:     num = LED[1];      break;


              case
0xBE:      num = LED[2];      break;


              case
0x7E:      num = LED[3];      break;


              case
0xED:     num = LED[4];      break;


              case
0xDD:     num = LED[5];      break;


              case
0xBD:     num = LED[6];      break;


              case
0x7D:     num = LED[7];      break;


              case
0xEB:      num = LED[8];      break;


              case
0xDB:     num = LED[9];      break;


              case
0xBB:      num = LED[10];    break;


              case
0x7B:      num = LED[11];     break;


              case
0xE7:      num = LED[12];    break;


              case
0xD7:     num = LED[13];    break;


              case
0xB7:      num = LED[14];    break;


              case
0x77:      num = LED[15];    break;


              default:    break;           


       }


       return
num;   


}


//********End of Function************


 


void main(void)


{


       uchar
key,led;


       while(1)


       {


              key
= KeyScan();


              led
= GetCode(key);


 


              Bit
= 1;


              P0
= led;


              Bit
= 0;


 


              Seg
= 1;


              P0
= 0xEF;


              Seg
= 0;


 


              Delay(50);


       }


}

PARTNER CONTENT

文章评论0条评论)

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