3-独孤九剑第二式:"心花怒放"之矩阵键盘

  有关矩阵键盘觉得有必要写下来,因为其中出现的关键点挺多。好吧,开始修炼:

  整体编写思路:1)首先要置P3口即矩阵键盘连接口初始值为1(因为只有置为1后,P3口作为输出口,才能让其他来读到P3口的状态);

         2)读取要操作按键对应P3口读取位的状态,如果对应位为0,则说明按键已经按下,然后可以进行相应的操作。

  注意事项:1、按下去,和弹起来都有缓冲电流流过,所以应该进行相应消除处理;

       2、当按下时,会出现反复加1的情况,所以应该进行阻止处理。

  1、初探矩阵键盘

  /*******************************************************************************
 *function    : 点击矩阵键盘P3^4口对应的按键,点击一下,led显示数字加1;
 *author    :                  
 *time        :
 *
 ****l***************************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit d1 = P1^0;
sbit key1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;
uchar coun;    
uchar code table[] = {    //数字编码表
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f,0
};
      

void display(uchar num)
{
    dula = 1;
    P0 = table[num];
    dula = 0;
    P0 = 0xff;
    wela = 1;
    P0 = 0xfe;
    wela = 0;
}

void main()
{
    coun = 0;//initial global variable

    P3 = 0xff;//initial P3 because it will be effect ,only setting P3 as one;
    while(1)                  
    {
        if(key1 == 0)
        {
            coun = (coun+1)%10;
            display(coun); //display the num
        }
    }
}

 

上述程序看似没有什么问题,但当实际去操作时就会发现led在那里显示时,跳动频率很快,累加数字的方向是正确。通过后续发现,由于没去消除按键引起的电流缓冲引起的变化

,具体解决此方案如下程序:

2、消除缓冲

/*******************************************************************************
 *function    : 点击矩阵键盘P3^4口对应的按键,点击一下,led显示数字加1;
 *author    :                  
 *time        :
 *
 ****l***************************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit d1 = P1^0;
sbit key1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;
uchar coun,iflag;    
uchar code table[] = {    //数字编码表
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f,0
};
      

void display(uchar num)
{
    dula = 1;
    P0 = table[num];
    dula = 0;
    P0 = 0xff;
    wela = 1;
    P0 = 0xfe;
    wela = 0;
}

void delay(uint ms)
{
    uint x;
    uchar y;
    for(x=ms; x>0; --x)
        for(y=110; y>0; --y);
}

void main()
{
    coun = 0;//initial global variable
    iflag = 0;

    P3 = 0xff;//initial P3 because it will be effect ,only setting P3 as one;
    while(1)                  
    {

        if(key1 == 0)
        {
            if(iflag == 0)//avoid number being added repeatedly when key1 are pressed down.
            {
                iflag = 1;
                delay(20);
                coun = (coun+1)%10;
                display(coun);//display the num
            }
            else
            {
                display(coun);//display the num
            }
            delay(20);//erass the intermittence time when key1 are pressed up.
        }
        else
        {
            iflag = 0;
        }
        
    }
}

                   

 



                       

 

posted @ 2014-09-28 09:50  第九剑  阅读(157)  评论(0)    收藏  举报