0

I wrote a short function which detects a pressed button at matrix keyboard and write his id (48 buttons, so ID are 0 - 47).

The problem is, that it is only detecting ButtonID 0 right, others are detected very ugly, like ButtonID 1 lays on place for ButtonID 3;

I can't find any problem in that code ... I initialized all right (I hope), I am detecting them ... but they still have bad IDs. Thanks for any response or solution.

Actually I am testing for ButtonID10 in code There is my code which I written for my 6(cols)x8(rows) matrix keyboard:

#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;
}
user3674256
  • 5
  • 1
  • 5
  • 2
    Make a map of what button output is detected against which physical button was pushed. Look at the data. Work out how your port/kepad is scanned/wired incorrectly. – Martin James Jun 28 '14 at 16:57
  • Everything is wired as it have to be. I made a map but it looks like some IDs are undetectable ... I can detect only about half of them. – user3674256 Jun 28 '14 at 17:02
  • 4
    `00000010` == `8` : Numbers beginning with 0 is octal constant. not binary number. – BLUEPIXY Jun 28 '14 at 17:15
  • BLUEPIXY: Thanks, but it still doesnt solve my problem, but it is better now, much better. – user3674256 Jun 28 '14 at 17:19
  • why do you use binary array when `binary[i] == i + 1` anyway? – phuclv Jun 30 '14 at 09:33
  • No, it will be 1;2;3;4;... but I need this: 1;2;4;8;... That was problem which I previously repaired. I wrote that in answer to this problem. I dont need counting, but shifting the binary value. – user3674256 Jun 30 '14 at 09:42
  • Your array above has its values as `1, 2, 3, 4...`. The powers of 2s `1, 2, 4, 8...` have only one `1` bit and would be `1b1, 1b10, 1b100, 1b1000...`. But even if that's a power of 2 you should use `1 << i` instead – phuclv Jul 04 '14 at 13:48

1 Answers1

0

Ok, I finally figured why it did't worked. The code was right, but I did a mistake in int type array 'binary' - First mistake was that they were octals, and second that I was counting up, not shifting the values.

But now there is another problem ... The program doesn't detect last 2 (0,1,2,3 are detectable and 4,5 are not).

Trust me - i tried to solve this problem, I even resoldered whole mcu and all buttons & zero resistors but anything worked. 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;
}
user3674256
  • 5
  • 1
  • 5