打印

一个思路新颖的矩阵键盘扫描程序

[复制链接]
3260|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xsmcu|  楼主 | 2008-12-21 19:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
ayb_ice| | 2008-12-21 20:28 | 只看该作者

这有什么新颖

不就是通过循环让端口输出低电平,再通过查表看是哪个按下,再查找显示代码显示出来.
我把代码整理了一下:


#include<reg52.h>
#include<intrins.h>
#define display P1
#define keyboardCode P2

#define uint unsigned int
#define uchar unsigned char

uchar code initialKeyboardCode[] = { 0xef, 0xdf, 0xbf, 0x7f }; // Set the initial keyboard code.

uchar code getKeyboardCode[] = {
0xee, 0xed, 0xeb, 0xe7, 0xde, 0xdd, 0xdb, 0xd7,     // When the keyboard pressed
0xbe, 0xbd, 0xbb, 0xb7, 0x7e, 0x7d, 0x7b, 0x77 };   // down ,the mcu will get one of them.

uchar code theActionCode[] = {
0xf7, 0xf4, 0xf1, 0xf0, 0xf8, 0xf5, 0xf2, 0xf0,     // The display code, you can rewrite
0xf9, 0xf6, 0xf3, 0xf0, 0xf8, 0xf5, 0xf2, 0xf0
 }; // them if you want . But the code you rewrite must correspondence
// with the ”getkeyboardCode”.

void KeyboardScan(void)
{
    uchar sender = 0x00;
    uchar counterOne = 0x00;
    uchar counterTwo = 0x00;

    for(counterOne=0x00; counterOne<4; counterOne++)    // 轮流输出低电平
    {
        keyboardCode = initialKeyboardCode[counterOne]; // Load the initialKeyboardCode
        sender = keyboardCode; // Get the keyboardCode when the keyboard press down

        // perhaps a delay here.
        for(counterTwo=0x00; counterTwo<16; counterTwo++)
        {
            // When a geyKeyboardC ode equal to the sender ,
            if(sender==getKeyboardCode[counterTwo]) // 查表按键
            {
                // Then load the display code from the theActioncode
                display = theActionCode[counterTwo]; // 查表显示代码
                // You can write you control functions here too.
            }
        }
    }
}

// The test main function.
void main(void)
{
    display = 0x00;
    while(1)
    {
        KeyboardScan();
    }
}
// You can refer to the circuit below.

使用特权

评论回复
板凳
icmap| | 2008-12-21 20:46 | 只看该作者

新颖,简洁,但功能不强。

用该方法时,多个按键同时按下等效于无按键,这在简单应用下应该够了。
我更喜欢像电脑键盘那样支持多个按键同时按下的键盘,我写了一个有这样功能的程序,而且能判别矩阵键盘的“矩形三键”冲突。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

17

帖子

0

粉丝