Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Keypad Scanning

Status
Not open for further replies.

usif

New Member
Hi, I am relatively new to this, and I can't got my head around keypad scanning.

Now, I understand the basics (i.e setup of a HEX keypad, and general idea of how to program it), however I just can't seem to understand how to do it in BoostC. What I'm asking for is a nudge in the right direction, or a quick brief of what needs to be done. What I am having trouble with is the delay time. Currently it 'skips' buttons.

Compiler: BoostC
MCU: PIC16F877A

I will post code once I get my laptop back, currently out of order :(
 
Last edited:
Hi Mike, I do have schematics although im using a developement board that i'm learning off, so everything is connected correctly.

**broken link removed**

Basically it's all connected using PORTC.
I have found my code on my desktop, however this was before I changed some things on the file on my laptop...both however give me the same problem.

Code:
/*    

						4X4 KEY MATRIX

				       	      PORTCPIN  3  2  1  0              collumns are input pins
							   
						 4      O  O  O  O
						 5      O  O  O  O
						 6      O  O  O  O
						 7      O  O  O  O 

												


        LCD PIN ARRANGEMENT

	JHD-162A
	
	1    2   3   4  5 6   7  8   9   10  11  12  13   14  15  16
	VSS VCC VEE RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 LED+ LED-
	
*/
	

#define LCD_ARGS  0, /* Interface type: mode 0 = 8bit, 1 = 4bit(low nibble), 2 = 4bit(upper nibble) */ \
    0,               /* Use busy signal: 1 = use busy, 0 = use time delays */                          \
    PORTD, TRISD,    /* Data port and data port tris register */                                       \
    PORTA, TRISA,    /* Control port and control port tris register */                                 \
    1,               /* Bit number of control port is connected to RS */                               \
    2,               /* Bit number of control port is connected to RW */                               \
    3                /* Bit number of control port is connected to Enable */




#include <system.h>
#include <lcd_driver.h>
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF
//#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF

#pragma CLOCK_FREQ 20000000

#define col1 portc.3
#define col2 portc.2
#define col3 portc.1
#define col4 portc.0
//#define row portc

	
void main()
{

		adcon1 = 0b00000110; //Digital
		trisa = 0x00; // A Output
		trisb = 0xff; // B Input
		trisc = 0x0f; // Portc pin 0-3 input only
	  
	    
		trisd = 0x00; // D Output
	//initialise LCD
		lcd_setup();
		lcd_clear();
	
		//type in default writing
		lcd_gotoxy(0,0);
		lprintf("PRESS A BUTTON");
		
		char row;
		
while(1 == 1)
{
	/*if(col1 == 0)
	{
	lprintf("tstin123");
		}*/
		
	for(row=0x10;row != 0x80;row<<=1)
	{
	portc = row;
	delay_ms(4);
	
		}
		//portc = 0;
		
		if(row = 0x10 && col1 == 0)
		{
	delay_ms(2);
		lcd_clear();
		lprintf("1");
		}
			if(row = 0x10 && col2 == 0)
		{
		
		delay_ms(2);
		lcd_clear();
		lprintf("2");
		}
		
			if(row = 0x20 && col1 == 0)
		{
		delay_ms(2);
		
		lcd_clear();
		lprintf("3");
		}
		
		if(row = 0x20 && col2 == 0)
		{
		delay_ms(2);
		
		lcd_clear();
		lprintf("4");
		}
		
		delay_ms(20);
		
		
		}
	}
 
Last edited:
Can you please post some schematic..?
We need the schematic to help you because there are many ways a person can connect and use the keypad and we need to know how YOU connect it..

In your post above, you said it skipped button, which button did it skip..?
 
Last edited:
From looking at your code, your keypad's button layout seems to be

1 2 X X
3 4 X X
X X X X
X X X X

X = not used.

Is that what you intended or is there some error in your coding?
 
Hi,

I do believe I have posted schematics in my above post. I have used PORTC: pin 4, 5, 6 , 7 are used for the rows, and pins 0, 1, 2, 3 are used for he columns.

Yes that is what I have coded so far (I intended to have all 16 keys to be used). What I meant by buttons being skipped is that if I were to press either button '3' or button '4' it would instead display '1' or '2' on the LCD respectively. I think this is a problem with the delay (as far as I know).

The reason why I haven't coded all 16 keys is that I'm looking for a way to simplify the code, rather than just have 16 'if' statements.

Thanks in advance
 
Sorry, my wrong.. I was at work jst now when I posted the reply above and I din realise that my company blocked those images..

Can you try to switch J16 to connect between the resistors and ground? I'm not sure how it can work when all the PIC input pin is connected to Vdd through the resistors.

Also try to use

if...
else if..
else if..

instead of

if..
if..
if..
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top