Page 1 of 1

Problem with C and kaypad matrix

Posted: Sun Sep 08, 2013 9:05 am
by Fabiux
Hy,
I'm trying to write a C program to drive a 4x4 keypad matrix. the program must read which key is pressed and print it to the screen.

the code is as follows:

Code: Select all

#include <wiringPi.h>
#include <mcp23017.h>

#define AF_BASE 100
#define I2C_ADD 0x20

#define ROW1	1
#define ROW2	2
#define ROW3	3
#define ROW4	4

#define COL1	115
#define COL2	114
#define COL3	107
#define COL4	108

int main(int argc, char** argv) {

	wiringPiSetup();
	mcp23017Setup(AF_BASE, I2C_ADD);
	
	/* Inizializzo gli array per le righe e le colonne */
	int rows[4] = {ROW1, ROW2, ROW3, ROW4};
	int cols[4] = {COL1, COL2, COL3, COL4};
	int keypad[4][4] = {{'1', '2', '3', 'A'}, {'4', '5', '6', 'B'},	{'7', '8', '9', 'C'}, {'*', '0', '#', 'D'}};
	
	
	int i, j;
	
	/* imposto le colonne come uscite e le imposto a livello alto */
	for(i=0; i<4; i++){
		pinMode(cols[i], OUTPUT);
		digitalWrite(cols[i], HIGH);
	}
	
	/* Imposto le righe come ingressi con PULL_UP */
	for(j=0; j<4; j++){
		pinMode(rows[i], INPUT);
		pullUpDnControl(rows[i], PUD_UP);
	}
	
	printf("Premi un pulsante sulla tastiera:\n");

	/* Inizio il ciclo per vedere che bottone viene premuto */
	while(1){
	
		for(i=0; i<4; i++){
			/* Metto la colonna corrente a 0 e vedo se qualche riga va a zero */
			digitalWrite(cols[i], LOW);
			
			/* Controllo lo stato delle righe. Se una va a zero, un bottone รจ stato premuto */
			for(j=0; j<4; j++){
			
				if(digitalRead(rows[j])==LOW){
					printf("%c\n", keypad[j][i]);
					/* Attendi che il pulsante sia rilasciato */
					while(digitalRead(rows[j])==0){
					}
				}
			}
			/* Rimetto la colonna corrente a 1 */
			digitalWrite(cols[i], HIGH);
		}
	}

}
Columns PINs of the keypad are connected to 4 PINs of a mcp23017 expander and are used as OUTPUT, while rows PINs are connected to 4 GPIO PINs through 1K resistors, and used as INPUT with internal pull up resistor.

The problem is that if I run the program, without pushing any buttons, the program detects some input.

Can anyone help me solve the problem?

thanks

Re: Problem with C and kaypad matrix

Posted: Sat Sep 21, 2013 1:00 pm
by lcawley
You probably need to de-bounce the switches/key presses. Try looking through these examples...
http://hackaday.com/2010/11/09/debounce ... -them-all/

Re: Problem with C and kaypad matrix

Posted: Tue Sep 24, 2013 8:34 am
by IanH2
I can't see an obvious error in the code. Which input does it report as low? Can you measure the voltage on the corresponding 'row' input pin?

Re: Problem with C and kaypad matrix

Posted: Tue Sep 24, 2013 9:42 am
by ame
I suggest you write some smaller code.

Write a program which sets the 4 outputs high, one at a time, for maybe 1 second, and prints the output number on the screen. Then hook up a voltmeter and verify that each output goes high when the number is printed, and low when then next number is printed.

Next, write a program that reads the inputs and prints the value that was read. With a spare piece of wire try grounding each input and verify that the input value changes correctly. Don't do this on the GPIO header, because you might short out a pin next to a GPIO pin, but maybe you could do it on the pins of your keypad.

If you cannot get these tests working then you cannot make your keypad work. These tests might help you figure out where to look for the problem.

Do you have another program running which uses the GPIOs for another function?