const int numRows = 4;//定义4行
const int numCols = 4;//定义4列
const int debounceTime = 20;//去抖动时间长度
const char keymap[numRows][numCols] = { //键值,可以按需要更改
{ '1','2','3','+'},
{ '4','5','6','-'},
{ '7','8','9','X'},
{ '*','0','#','/'},
};
const int rowPins[numRows] = {7,6,5,4}; //设置硬件对应的引脚
const int colPins[numCols] = {11,10,9,8};
//初始化功能
void setup()
{
Serial.begin(9600);
for(int row = 0;row < numRows;row++)
{
pinMode(rowPins[row],INPUT);
digitalWrite(rowPins[row],HIGH);
}
for(int column = 0;column < numCols;column++)
{
pinMode(colPins[column],OUTPUT);
digitalWrite(colPins[column],HIGH);
}
}
//主循环
void loop()
{
//添加其他程序,循环进行
char key = getkey();
if(key != 0)
{
Serial.print("Got key"); //串口打印键值
Serial.println(key);
}
}
//读取兼职程序
char getkey()
{
char key = 0;
for(int column = 0;column < numCols;column++)
{
digitalWrite(colPins[column],LOW);
for(int row = 0;row < numRows;row++)
{
if(digitalRead(rowPins[row]) == LOW) //是否有键值按下
{
delay(debounceTime);
while(digitalRead(rowPins[row]) == LOW); //等待按键释放
key = keymap[row][column];
}
}
digitalWrite(colPins[column],HIGH); //De-active the current column
}
return key;
}
本文来源:https://www.wddqw.com/doc/c6fc1d71a9956bec0975f46527d3240c8447a168.html
正在阅读:
矩阵计算器01-01
剂组四字词语01-01
什么十什么四字成语有哪些01-01
开学典礼演讲稿01-01
常州市中考满分作文-常州中考满分作文01-01
像像像造句01-01
关于禁毒的文章01-01
创建绿色校园简介01-01
手抄报经验谈01-01