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.

code problems for 4x4 keypad

Status
Not open for further replies.

darsin

New Member
hello all,
i'm now testing the 4x4 keypad, but now i just do a simple program that just to on a led when i pressing a '1'
after troubleshooting, the code has no problem but the led could not blink..
here is my simple code:

#include <p18f4550.h>

#define row1 PORTBbits.RB7
#define row2 PORTBbits.RB6
#define row3 PORTBbits.RB5
#define row4 PORTBbits.RB4
#define col1 PORTBbits.RB3
#define col2 PORTBbits.RB2
#define col3 PORTBbits.RB1
#define col4 PORTBbits.RB0

void main()
{
TRISB=0xFF;
TRISD=0x00;

while (1){
int i;
int temp;
PORTB=0;
if (row1==1 & col1==1)
{
PORTDbits.RD7=1;
temp=200;
while(temp){
temp--;
}
i=1;
}
}
}

i had pull down 10k resistor to ground but it still cannot work.
please help.
 
Last edited:
hello all,
i'm now testing the 4x4 keypad, but now i just do a simple program that just to on a led when i pressing a '1'
after troubleshooting, the code has no problem but the led could not blink..
here is my simple code:

#include <p18f4550.h>

#define row1 PORTBbits.RB7
#define row2 PORTBbits.RB6
#define row3 PORTBbits.RB5
#define row4 PORTBbits.RB4
#define col1 PORTBbits.RB3
#define col2 PORTBbits.RB2
#define col3 PORTBbits.RB1
#define col4 PORTBbits.RB0

void main()
{
TRISB=0xFF;
TRISD=0x00;

while (1){
int i;
int temp;
PORTB=0;
if (row1==1 & col1==1)
{
PORTDbits.RD7=1;
temp=200;
while(temp){
temp--;
}
i=1;
}
}
}

i had pull down 10k resistor to ground but it still cannot work.
please help.

After just a quick look at your source, I see one problem:
you have used the bitwise and (&) rather than the logical and (&&)

I'm not sure this is your only problem, but try changing the line
Code:
if (row1==1 & col1==1)
to
Code:
if (row1==1 && col1==1)
 
well, i changed it just now but the result is still the same...
am I correct that i pull down resistor to ground at the column pins before connecting to the port?
 
Hi,
For the 4x4 key pad, either the row or the column has to be output, to source and the other one as input, to read. For example:
If the rows are used to source, then set them as output. Set row1 high, then read all the columns, followed by row2, etc. Pull down resistors for the input pins are necessary.
 
If your keypad is a 4x4 matrix, then making both the row and columns inputs on the PIC won't work. Make the 4 PIC pins connected to the columns outputs and set them to the low state. Make the 4 PIC pins connected to the row inputs and use pull up resistors on them. Then wait for one of the row inputs to go low when a switch is pressed. Then scan each column output low while making the others high until you've identified which column was active. Now you'll know which key is pressed.
 
#include <p18f4550.h>

#define row1 PORTBbits.RB7
#define row2 PORTBbits.RB6
#define row3 PORTBbits.RB5
#define row4 PORTBbits.RB4
#define col1 PORTBbits.RB3
#define col2 PORTBbits.RB2
#define col3 PORTBbits.RB1
#define col4 PORTBbits.RB0

void main()
{
TRISB=0x0F;
TRISD=0x00;

while (1){
int i;
int temp;
PORTB=0;
if (row1==1)
{
PORTDbits.RD7=1;
temp=200;
while(temp){
temp--;
}
i=1;
}
}
}

is it like that??
but my led still can not work...
 
No. row1 is configured as output and you don't read from it. if (row1==1) is reading the pin.
I assume number 1 on the keypad is connected on row1 and col1. So you should set row1 high, then read from col1. Do you get what I mean?
 
#include <p18f4550.h>

#define row1 PORTBbits.RB7
#define row2 PORTBbits.RB6
#define row3 PORTBbits.RB5
#define row4 PORTBbits.RB4
#define col1 PORTBbits.RB3
#define col2 PORTBbits.RB2
#define col3 PORTBbits.RB1
#define col4 PORTBbits.RB0

void main()
{
TRISB=0x0f; //row configure as output, col is input
TRISD=0; //configure port d as output

while (1){
int i; //store i
int temp;
row1=1;

if (col1==1) //read from col1
{
PORTDbits.RD7=1; //if key 1 is press then high D7
temp=200; //delay
while(temp){
temp--;
}
i=1; //i as 1
}
}
}

is it correct?
 
Oops. Just realize your row1 is RB7 and col1 is RB3. Can you show the schematic?
RD7 doesn't go high even you have tried all the buttons?
 
=.=!
how to attach image up there?cant attach it..
i use pic18f4550.h and r1-r4 is RB7-RB4 while col1-col4 is RB3-RB0
is my program correct?
i got pull down 10k resistor to ground
but still cannot on the led...
please help me..
 
so this is my schematic but i just give you the one i just connect the keypad coz' i don't want to draw again my circuit.
do i need to connect 1k resistor at the column?
 

Attachments

  • keypad schematic.JPG
    keypad schematic.JPG
    117.9 KB · Views: 261
Hi Darsin,

To read from a matrix keyboard you need to make 1 of the row bits an output and make it high. You can then read 4 column bits.

See if this function will read your keyboard,
Code:
unsigned int ReadKeys(){
unsigned int Key;
    TRISB=0b01111111;       //make row 1 output
    LATB=0b10000000;        //and make it high
    Key=PORTB&0x0f;         //read the columns into bits 0-3
    TRISB=0b10111111;       //make row 2 output
    LATB=0b01000000;        //and make it high
    Key|=(PORTB&0x0f)<<4;   //read the columns into bits 4-7
    TRISB=0b11011111;       //make row 3 output
    LATB=0b00100000;        //and make it high
    Key|=(PORTB&0x0f)<<8;   //read the columns into bits 8-11
    TRISB=0b11101111;       //make row 4 output
    LATB=0b00010000;        //and make it high
    Key|=(PORTB&0x0f)<<12;  //read the columns into bits 12-15
    return Key;
}

When you call the function then it will return an integer with a bit set for each key that is pressed.

HTH.

Mike.
 
yes it could work already thanks!
and it is for just row4 (*,0,#,D) on at pin B3-B0 respectively
if i wan it to light up all then have to write the second ReadKeys again?
 
I don't understand your question. However, the integer returned from the function would have a bit set for each key pressed. They would be,

bit 0 = A,
bit 1 = 3,
bit 2 = 2,
.
.
bit 14 = 0,
bit 15 = *,

So, to do something if the * key was pressed you would do,
Code:
if(ReadKeys()&(1<<15)!=0){
    //do something here
}

HTH

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top