学习笔记(01):按键-第1季第9部分-1.9.9.矩阵键盘编程实战

立即学习:https://edu.csdn.net/course/play/3557/61858?utm_source=blogtoedu

#include<reg51.h>

#define LED P0

#define KEY P3

unsigned char val[16] = {};//暂时省略
void main(void)
{
    unsigned char keyValue = 0;
    while(1)
    {
        keyValue = getkey();
        LED = val[keyValue];
    }
}

unsigned char getValue(void)
{
    unsigned char hang=0,lie=0,keyValue = 0;
    KEY = 0x0f;
    if(KEY != 0x0f)
    {
        delay10ms();  //暂不实现
        switch(KEY)
        {
            case 0x0e: hang = 1; break;
            case 0x0d: hang = 2; break;
            case 0x0b: hang = 3; break;
            case 0x07: hang = 4; break;
        }
        KEY = 0xf0;
    }
    if (KEY != 0xf0)
    {
        switch(KEY)
        {
            case 0xe0: lie = 1; break;
            case 0xd0: lie = 2; break;
            case 0xb0: lie = 3; break;
            case 0x70: lie = 4; break;
            default:                  break;
        }
    }
    keyValue = (hang -1) *4 + lie;
    return keyValue;
}

 

©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页