我寫了一個短的函數,它檢測矩陣鍵盤上按下的按鈕,並寫下他的ID(48個按鈕,所以ID是0 - 47)。不完全檢測按下的按鈕
問題是,它只檢測ButtonID 0,其他檢測非常難看,就像ButtonID 1爲ButtonID 3放置位置;
我在代碼中找不到任何問題...我初始化了所有權利(我希望),我正在檢測他們...但他們仍然有錯誤的ID。感謝您的任何迴應或解決方案。
其實我在代碼 爲ButtonID10測試還有就是我的代碼,我爲我的6(COLS)書面X8(行)矩陣鍵盤:
#include <avr/io.h>
// PIN's Init definitions
#define O(DIR,PIN) DIR |= (1 << PIN) // Marks PIN as output pin
#define OX(DIR) DIR = 0xFF // Makes all PINs as output pins
#define I(DIR,PIN) DIR &= ~(1 << PIN) // Makes PIN as input pin
#define IX(DIR) DIR = 0x00 // Makes all PINs as input pins
// PIN's SET definitions
#define H(PORT,PIN) PORT |= (1<<PIN) // Makes PIN 5V
#define HX(PORT) PORT = 0xFF // Makes all PINs 5V
#define L(PORT,PIN) PORT &= ~(1<<PIN) // Makes PIN 0V
#define LX(PORT) PORT = 0x00 // Makes all PINs 0V
#define S(PORT,BIN) PORT = BIN // Makes PINs activated from binary pattern
// PIN's GET definitions
#define GET(GPIN,PIN) GPIN & (1<<PIN) // Reads digital value on PIN
#define GET2(GPIN,BIN) GPIN & BIN // Reads digital value of binary defined PIN
int cols = 6;
int rows = 8;
int binary[8] = {0b00000001,0b00000010,0b00000011,0b00000100,0b00000101,0b00000110,0b00000111,0b00001000};
int checkKeys();
void initPins();
int main(void)
{
initPins();
//TESTS
OX(DDRH);
L(PORTH,PH1);
//TESTS
while (1){
if (checkKeys() == 10){
H(PORTH,PH1);
}else{
L(PORTH,PH1);
}
}
return 0;
}
void initPins(){
OX(DDRK);
IX(DDRF);
}
int checkKeys(){
for (int x = 0; x < rows; x++){
S(PORTK,binary[x]);
for (int y = 0; y < cols; y++){
if (GET2(PINF,binary[y])){
return (x*cols + y);
}
}
}
return -1;
}
製作一個映射哪個按鈕輸出被檢測到哪個物理按鈕被按下。看數據。弄清楚你的端口/ kepad被錯誤地掃描/連線。 –
一切都是有線的,因爲它必須是。我製作了一張地圖,但看起來有些ID是無法檢測的......我只能檢測到其中一半左右。 – user3674256
'00000010' =='8':以0開頭的數字是八進制常數。不是二進制數。 – BLUEPIXY