单片机扫描矩阵键盘

采用分时复用的方法,定时置位行,检测列,确定按下的按键,输出不同的按键值。

void KbScanProcess(void)
{
//uint8_t Row=0;  //按键所在行
//uint8_t Col=0;  //按键所在列
//uint8_t RowCount=0;  //按键触发行的个数,用于判断多个按键按下
uint8_t ColCount=0;  //按键触发列的个数,用于判断多个按键按
KeyVal = 0;
SET_ROW_ALL_OUT1;   //查询第一行
ROW1 = 0;
SET_COL_ALL_IN;
KbScanWaitIOConfigSteady();

if(COL1 == 0)
{
KeyVal = 1;     //分组1
ColCount++;
}
if(COL2 == 0)
{
KeyVal = 2;     //色温+
ColCount++;
}
if(COL3 == 0)
{
KeyVal = 3;     //分组3
ColCount++;
}
if(COL4 == 0)
{
KeyVal = 4;     //分组2
ColCount++;
}

SET_ROW_ALL_OUT1;   //查询第二行
ROW2 = 0;
SET_COL_ALL_IN;
KbScanWaitIOConfigSteady();
if(COL1 == 0)
{
KeyVal = 5;     //七档彩色循环键
ColCount++;
}                 //6留空
if(COL3 == 0)
{
KeyVal = 7;     //小夜灯
ColCount++;
}
if(COL4 == 0)
{
KeyVal = 8;     //定时
ColCount++;
}

SET_ROW_ALL_OUT1;   //查询第三行
ROW3 = 0;
SET_COL_ALL_IN;
KbScanWaitIOConfigSteady();
if(COL1 == 0)
{
KeyVal = 9;     //开/关
ColCount++;
}                 //10留空
if(COL3 == 0)
{
KeyVal = 11;      //色温切换 
ColCount++;
}
if(COL4 == 0)
{
KeyVal = 12;      //彩光切换
ColCount++;
}

SET_ROW_ALL_OUT1;   //查询第四行
ROW4 = 0;
SET_COL_ALL_IN;
KbScanWaitIOConfigSteady();
if(COL1 == 0)
{
KeyVal = 13;      //色温-
ColCount++;
}                 
if(COL2 == 0)
{
KeyVal = 14;      //亮度+
ColCount++;
}             
if(COL3 == 0)
{
KeyVal = 15;      //亮度-
ColCount++;
}
if(COL4 == 0)
{
KeyVal = 16;      //空键
ColCount++;
}
//KeyVal = 11;
if(ColCount != 1)
{
KeyVal = 0;
}
//KeyVal = 11; 
}
posted @ 2023-07-11 15:03  steven_lg  阅读(27)  评论(0编辑  收藏  举报