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