简洁高效的ESP32处理矩阵键盘代码…
/**********矩阵键盘IO映射***************
3(9) 1(8) 5(4)
2(13)
7(12)
6(18)
4(19)
*************************************/
uint8_t Trg=0,Cont=0;
void Key_Task(void)
{
uint8_t ReadData,ColumnData,RowData;
pinMode(9,INPUT_PULLUP);pinMode(8,INPUT_PULLUP);pinMode(4,INPUT_PULLUP);//列线输入
pinMode(13,OUTPUT);pinMode(12,OUTPUT);pinMode(18,OUTPUT);pinMode(19,OUTPUT);//行线
//列高行低
digitalWrite(13,0);digitalWrite(12,0);digitalWrite(18,0);digitalWrite(19,0);
ColumnData =0x01<<8 | (digitalRead(9)<<6) | (digitalRead(8)<<5) | (digitalRead(4)<<4); //读取列线
pinMode(9,OUTPUT);pinMode(8,OUTPUT);pinMode(4,OUTPUT);//列线
pinMode(13,INPUT_PULLUP);pinMode(12,INPUT_PULLUP);pinMode(18,INPUT_PULLUP);pinMode(19,INPUT_PULLUP);//行线输入
//列低行高
digitalWrite(9,0);digitalWrite(8,0);digitalWrite(4,0);
RowData = (digitalRead(19)<<3) | (digitalRead(18)<<2) | (digitalRead(12)<<1) | digitalRead(13); //读取行线
ReadData=(ColumnData | RowData)^0XFF;
Trg = ReadData&(ReadData^Cont); //记录短按键值
Cont = ReadData; //记录长按键值
/****************矩阵键码表***********
1-0X41 2-0X21 3-0X11
4-0X42 5-0X22 6-0X12
7-0X44 8-0X24 9-0X14
*-0X48 0-0X28 #-0X18
************************************/
//按键任务区
if(Trg == 0x48){ //*按下
}
}