Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Forums > General Electronics Chat


General Electronics Chat This forum is for general chat about electronics, eg: Dont know what a part does? Dont know how to read a circuit? Want to get an opinion?

Reply
 
Tools
Old 14th July 2008, 05:56 PM   #1
Default code problems for 4x4 keypad

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 by darsin; 14th July 2008 at 05:58 PM.
darsin is offline  
Old 14th July 2008, 06:05 PM   #2
Default

Quote:
Originally Posted by darsin View Post
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)
BeeBop is offline  
Old 15th July 2008, 03:00 AM   #3
Default

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?
darsin is offline  
Old 15th July 2008, 03:15 AM   #4
Default

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.
__________________
bananasiong
bananasiong is offline  
Old 15th July 2008, 03:17 AM   #5
Default

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.
__________________
Inside every little problem, is a big problem trying to get out.
kchriste is offline  
Old 15th July 2008, 03:25 AM   #6
Default

#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...
darsin is offline  
Old 15th July 2008, 03:29 AM   #7
Default

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?
__________________
bananasiong
bananasiong is offline  
Old 15th July 2008, 03:39 AM   #8
Default

okay how to read from col1? pressing the "1"?
darsin is offline  
Old 15th July 2008, 03:40 AM   #9
Default

Read from col1 means
Code:
if (col1 == 1)
__________________
bananasiong
bananasiong is offline  
Old 15th July 2008, 03:51 AM   #10
Default

#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?
darsin is offline  
Old 15th July 2008, 03:55 AM   #11
Default

why the portD7 can't be high??although i already set it as output high
darsin is offline  
Old 15th July 2008, 04:04 AM   #12
Default

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?
__________________
bananasiong
bananasiong is offline  
Old 15th July 2008, 06:58 AM   #13
Default

=.=!
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..
darsin is offline  
Old 15th July 2008, 07:33 AM   #14
Default

It is in the "additional options" when you're posting the reply. Click on manage attachment.
__________________
bananasiong
bananasiong is offline  
Old 15th July 2008, 11:30 AM   #15
Default

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?
Attached Thumbnails
code problems for 4x4 keypad-keypad-schematic.jpg  
darsin is offline  
Reply

Tags
4x4, code, keypad, problems

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Problems with 4x4 keypad interact with LCD.. chunei General Electronics Chat 17 10th April 2008 02:23 PM
Problem with keypad code pic_programm3r Micro Controllers 20 2nd April 2008 07:37 AM
what wrong with my keypad code eleceyes Micro Controllers 0 20th February 2008 03:08 AM
Keypad Code gregmcc Micro Controllers 5 11th May 2006 09:01 AM
code lock keypad questions.... eyevancsu Electronic Projects Design/Ideas/Reviews 7 10th April 2003 08:17 AM



All times are GMT. The time now is 07:07 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker