我写了一个简短的函数,它检测矩阵键盘上按下的按钮并写下他的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;
}
答案 0 :(得分:0)
好的,我终于弄明白为什么它不起作用。代码是正确的,但我在int类型数组'binary'中犯了一个错误 - 第一个错误是它们是octals,第二个是我在计算,而不是移动值。
但是现在又出现了另一个问题......程序没有检测到最后2个(0,1,2,3是可检测的,4,5不是)。
相信我 - 我试图解决这个问题,我甚至重新整理了所有按钮&amp;零电阻,但任何工作。 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;
}