Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f877a- keypad not working

Status
Not open for further replies.

navintiwari08

Newbie level 2
Joined
Jul 27, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
20
hello everyone..
I'm trying to interface a 4x4 matrix keypad to 16f877a.. the following code written in hi tech c is not working.. the program is printing characters of the array randomly without a keypress.. please help.. i'm using mplab 8.85.. using lcd 16x2..
NOTE: if I change the value of TRISC to 0x00, the code works fine.. but then i'm not making any input ports.. so how is it scanning a key press..?
thanks in advance..
Code:
#include<htc.h>
#define _XTAL_FREQ 20000000
#define lcd PORTB
#define key PORTC
#define rs RE0
#define rw RE1
#define en RE2

__CONFIG(FOSC_HS&LVP_OFF&BOREN_OFF&CP_OFF&PWRTE_OFF&WRT_OFF&WDTE_OFF);

void lcd_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);

void main()
{
        unsigned char col,row;
        unsigned char i[4][4]={'0','1','2','3',
                                                   '4','5','6','7',
                                           '8','9','A','B',
                                                   'C','D','E','F'};
        TRISB=0x00;
        TRISE=0X00;
        __delay_ms(2);
        ADCON1=0x07;
        lcd_init();
        while(1)
        {
                do
                {
                        TRISC=0x0f;
                        key=0x0f;
                        col=key;
                        col=col&0x0f;
                }
                while(col!=0x0f);
               
                do
                {      
                        do
                        {
                                __delay_ms(5);
                                col=key;
                                col&=0x0f;
                        }
                        while(col==0x0f);
                        __delay_ms(10);
                        col=key;
                        col&=0x0f;
                }
                while(col==0x0f);
                       
                while(1)
                {
                        key=0x7f;
                        col=key;
                        col&=0x0f;
                        if(col!=0x0f)
                        {
                                row=3;
                                break;
                        }
                       
                        key=0xbf;
                        col=key;
                        col&=0x0f;
                        if(col!=0x0f)
                        {
                                row=2;
                                break;
                        }

                        key=0xdf;
                        col=key;
                        col&=0x0f;
                        if(col!=0x0f)
                        {
                                row=1;
                                break;
                        }

                        key=0xef;
                        col=key;
                        col&=0x0f;
                        if(col!=0x0f)
                        {
                                row=0;
                                break;
                        }
               
                }
                if(col==0x0e)
                        lcd_data(i[row][0]);
                else if(col==0x0d)
                        lcd_data(i[row][1]);
                else if(col==0x0b)
                        lcd_data(i[row][2]);
                else if(col==0x07)
                        lcd_data(i[row][3]);
        }
}

void lcd_init()
{
        unsigned char cmd[]={0x38,0x0e,0x01,0x06,0x80};
        unsigned char i;
        for(i=0;i<=4;i++)
        {
                lcd_cmd(cmd[i]);
                __delay_ms(10);
        }
}

void lcd_cmd(unsigned char x)
{
        rs=0;
        rw=0;
        lcd=x;
        en=1;
        __delay_us(2);
        en=0;
}

void lcd_data(unsigned char x)
{
        rs=1;
        rw=0;
        lcd=x;
        en=1;
        __delay_us(2);
        en=0;
}
 

if not using, make use of the pull up and pull down resistors so that the ports would keep at constant default values.
 

this's code I used to scan keypad 4x4

Code:
#define KEY		PORTC

#define col_0	RC4
#define col_1	RC5
#define col_2	RC6
#define col_3	RC7


	char key_tab[4][4]={ '1', '2', '3', 'A',
				     '4', '5', '6', 'B',
				     '7', '8', '9', 'C',
				     '*', '0', '#', 'D'};
						 
	char scan_row[4]={0b00001110,0b00001101,0b00001011,0b00000111};

TRISC=0xf0;
//-------------------------------------------------------//
// READ KEYPAD 4x4 FUNCTION				       //
//-------------------------------------------------------//
char scan_key()
{
	char row=0;
	
	for(row=0;row<4;row++)
	{
		KEY=scan_row[row];
		
		if(col_0==0){while(col_0==0);return key_tab[row][0];};
		if(col_1==0){while(col_1==0);return key_tab[row][1];};
		if(col_2==0){while(col_2==0);return key_tab[row][2];};
		if(col_3==0){while(col_3==0);return key_tab[row][3];};
	}
	return '\0';
}
 

I am talking about the hardware !
Rows if you are taking them as input add up a pullup resistor so that the defualt stays 1 and coloumns as output add up pull down resistor so that the defualt value is at 0.
Normally there is no constant current flow, it varies up +/- 1.5v ac, and when u r stepping down there will lot of flucations and keypad doesnt recoginse properly.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top