2014-06-28 97 views
0

我寫了一個短的函數,它檢測矩陣鍵盤上按下的按鈕,並寫下他的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; 
} 
+2

製作一個映射哪個按鈕輸出被檢測到哪個物理按鈕被按下。看數據。弄清楚你的端口/ kepad被錯誤地掃描/連線。 –

+0

一切都是有線的,因爲它必須是。我製作了一張地圖,但看起來有些ID是無法檢測的......我只能檢測到其中一半左右。 – user3674256

+4

'00000010' =='8':以0開頭的數字是八進制常數。不是二進制數。 – BLUEPIXY

回答

0

好吧,我終於想通爲什麼我以前不工作。代碼是正確的,但我在int類型數組'binary'中犯了一個錯誤 - 第一個錯誤是他們是八進制數,第二個是我正在計數,沒有改變數值。

但是現在還有另一個問題...程序沒​​有檢測到最後2個(0,1,2,3是可檢測的而4,5個不是)。

相信我 - 我試圖解決這個問題,我甚至解決了整個單片機和所有按鈕&零電阻,但任何工作。 Thx

#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 
#define INV(PORT) PORT = ~PORT // Inverts all PINs values 
// 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 

// Definitions for KEYBOARD ROWS and COLS 
#define KEYBOARD_ROW PORTK 
#define KEYBOARD_COL PINF 

// Some important variables for this program 
int cols = 6; 
int rows = 8; 
unsigned int binary[8] = {0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000}; 

// To be sure 
int checkKeys(); 
void initPins(); 

int main(void) 
{ 

    initPins(); 

    //TESTS 
    TCCR1B |= (1 << CS10); 
    OX(DDRH); 
    L(PORTH,PH1); 
    //TESTS 

    while (1){ 
     if (TCNT1 >= 49999){ 
      if (checkKeys() >= 0){ 
       H(PORTH,PH1); 
      }else { 
       L(PORTH,PH1); 
      } 
      TCNT1 = 0; 
     } 
    } 

    return 0; 
} 

void initPins(){ // Inits all PINs required 
    OX(DDRK); 
    IX(DDRF); 
} 

int checkKeys(){ // Returns ID of pressed button (0 - 47) 
    for (int x = 0; x < rows; x++){ 
     S(KEYBOARD_ROW,binary[x]); 
     for (int y = 0; y < cols; y++){ 
      if (GET2(KEYBOARD_COL,binary[y])){ 
       return (x*cols + y); 
      } 
     } 
    } 
    return -1; 
} 
+0

這個數組真的是2的冪 –