极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11195|回复: 4

发一个4X4的键盘查询

[复制链接]
发表于 2016-1-8 10:39:04 | 显示全部楼层 |阅读模式

const int numRows=4;
const int numCols=4;
const int debounceTime=20;//
const char keymap[numRows][numCols]={//按键面板排列
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};


const int rowPins[numRows]={3,4,5,6};
const int colPins[numCols]={7,8,9,10};


void setup()
{
  Serial.begin(9600);
  for (int row=0;row<numRows;row++)
   {
     pinMode(rowPins[row],INPUT);
     digitalWrite(rowPins[row],HIGH);
    }
   
    for (int col=0;col<numCols;col++)
   {
     pinMode(colPins[col],OUTPUT);
     digitalWrite(colPins[col],HIGH);
    }
}

void loop()
{
  char key=getkey();
  if(key!='X')
   {
      Serial.print("Got key:");
      Serial.println(key);
    }
}


char getkey()
{
char key='X';

   for (int col=0;col<numCols;col++)
   {
      digitalWrite(colPins[col],LOW);   

for (int row=0;row<numRows;row++)
   {
     if(digitalRead(rowPins[row])==LOW)
       {
         delay(debounceTime);
         while(digitalRead(rowPins[row])==LOW);
         key=keymap[row][col];

        }

    }
   
digitalWrite(colPins[col],HIGH);

}
return key;
}
回复

使用道具 举报

发表于 2016-1-8 14:00:42 | 显示全部楼层
好分享~谢谢了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-19 07:43:40 | 显示全部楼层
本帖最后由 lqh 于 2016-5-2 12:07 编辑

做一个小试验Si?KWA51#
回复 支持 反对

使用道具 举报

发表于 2016-7-25 21:12:40 | 显示全部楼层
Si?b做一个小试验Si?KWA51#
回复 支持 反对

使用道具 举报

发表于 2016-12-28 23:47:15 | 显示全部楼层
谢谢楼主无私奉献仿制了一个带I2C的1602程序


#include <Keypad.h>
//LingShun lab  
#include <Wire.h>   
#include <LiquidCrystal_I2C.h> //引用I2C库  
  
//设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册  
LiquidCrystal_I2C lcd(0x3F,16,2);   
  

  

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  lcd.init();                  // 初始化LCD  
  lcd.backlight();             //设置LCD背景等亮  
  Serial.begin(9600);
}
   
void loop(){
  char customKey = customKeypad.getKey();
   
  if (customKey){
    Serial.println(customKey);
    lcd.setCursor(0,0);                //设置显示指针  
  lcd.print(customKey);     //输出字符到LCD1602上  
  lcd.setCursor(9,1);  
  lcd.print(" Z.W.F.");  
   
   
   
  }
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 20:30 , Processed in 0.052183 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表