4*4 矩阵键盘扫描
不得不说这蓝桥杯的板子矩阵按键改的真蛋疼。
1 #include "key.h" 2 3 #define LongTrg 0 //1 长触发 0 不支持 4 #define Timeout 3//超时时间 5 #define KeyLow(i) P33 = i;P32 = i;P31 = i;P30 = i 6 #define KeyHigh(i) P44 = i;P42 = i;P35 = i;P34 = i 7 uchar KeyRead (void) 8 { 9 static uchar State = 0; 10 static uchar temp; 11 uchar key_x , key_y ; 12 KeyHigh(1); 13 KeyLow(0); 14 key_y = (!P44)?0:(!P42)?1:(!P35)?2:(!P34)?3:4;//列 15 16 KeyHigh(0); 17 KeyLow(1); 18 key_x = (!P33)?0:(!P32)?1:(!P31)?2:(!P30)?3:4;//行 19 20 if(key_x == 4 || key_y == 4)//无触发or弹起 21 { 22 State = 0; 23 return 0; 24 } 25 else 26 { 27 State++; 28 if(State==1)//第一次保存键值 29 { 30 temp = key_y*4 + key_x +4; 31 return 0; 32 } 33 if( temp != key_y*4 + key_x +4 || (State > Timeout && !LongTrg) )//弹起or超时 34 { 35 return 0; 36 } 37 } 38 return temp; 39 }