Incomplete detection of pressed buttons

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;
}

Answers


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;
}

Need Your Help

JAX-RS: Request validation using XSD, JAXB

spring xsd jaxb jax-rs resteasy

Tech Stack: Java 1.6, JAXB, Spring 3, JAX-RS (RESTEasy)

Accessing tag value for UIImageView in UITapGesture Handler

objective-c ios

I have a UITapGesture attached to an image view. I'd like to reuse the same gesture across a couple of images. To do that the handler method needs to be able to recognize from which image the tap...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.