Log in

View Full Version : Need help in matrix keypad coding



rano_zen06
- 28th March 2008, 13:10
hi everyone,
i'm a newbie here and have a problem with the coding for my 4x4 keypad.I'm using PIC16F877A and connect my keypad to port D. What i want to do is:-
when I pressed switch '1' at keypad the character display on lcd is BUS01,
when I pressed switch '2' at keypad the character display on lcd is BUS02;
when I pressed switch '3' at keypad the character display on lcd is BUS03

I've use some keypad coding which i found from internet and it works perfectly with my keypad.The problem now, I don't know how to contunie this coding to get what i really want because i'm a new user for PIC BASIC pro. anyone kindly please help me with this coding.THanx for your help and support.
below is the code:

Define LOADER_USED 1

' Define LCD connections
define OSC 20

DEFINE LCD_DREG PORTC 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTB 'LCD register select port
DEFINE LCD_RSBIT 7 'LCD register select bit
DEFINE LCD_EREG PORTB 'LCD enable port
DEFINE LCD_EBIT 5 'LCD enable bit
DEFINE LCD_RWREG PORTB 'LCD read/write port
DEFINE LCD_RWBIT 6 'LCD read/write bit
DEFINE LCD_BITS 8 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD
DEFINE LCD_COMMANDUS 2000 'Command delay time in us
DEFINE LCD_DATAUS 50 'Data delay time in us

TRISC = %00000000 'Set port B as output
TRISB = %00000000 'Set port D as output
low PORTB.6 'Set the R/W bit to low

pause 1000 'wait until the LCD initializes

' Define program variables
col var byte ' Keypad column
row var byte ' Keypad row
key var byte ' Key value
Value var byte
ADCON1 = 7 ' Make PORTA and PORTE digital


Pause 100 ' Wait for LCD to start

Lcdout $fe, 1, "Key In Bus ID" ' Display sign on message

loop: Gosub getkey ' Get a key from the keypad
Lcdout $fe, 1, #key ' Display ASCII key number
Goto loop ' Do it forever

' Subroutine to get a key from keypad
getkey:
Pause 50 ' Debounce

getkeyu:
' Wait for all keys up
PORTD = 0 ' All output pins low
TRISD = $f0 ' Bottom 4 pins out, top 4 pins in
If ((PORTD >> 4) != $f) Then getkeyu ' If any keys down, loop
Pause 50 ' Debounce
getkeyp:
' Wait for keypress
For col = 0 to 3 ' 4 columns in keypad
PORTD = 0 ' All output pins low
TRISD = (dcd col) ^ $ff ' Set one column pin to output
row = PORTD >> 4 ' Read row
If row != $f Then gotkey ' If any keydown, exit
Next col

Goto getkeyp ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
key = (col * 4) + (ncd (row ^ $f))
Return ' Subroutine over
END

skimask
- 28th March 2008, 13:27
D. What i want to do is:-
when I pressed switch '1' at keypad the character display on lcd is BUS01,
when I pressed switch '2' at keypad the character display on lcd is BUS02;
when I pressed switch '3' at keypad the character display on lcd is BUS03
...........................................
The problem now, I don't know how to contunie this coding to get what i really want because..................


So what is it that you really want?
Is it what's at the beginning of your post?
What have you tried so far?
What works?
What doesn't work?

mister_e
- 28th March 2008, 13:41
try to change the following section


loop:
Gosub getkey ' Get a key from the keypad
Lcdout $FE, $C0, "BUS",DEC2 key ' Display ASCII key number on 2nd line
PAUSE 200
Goto loop ' Do it forever

rano_zen06
- 28th March 2008, 16:08
try to change the following section


loop:
Gosub getkey ' Get a key from the keypad
Lcdout $FE, $C0, "BUS",DEC2 key ' Display ASCII key number on 2nd line
PAUSE 200
Goto loop ' Do it forever

THis code works.Thank you for Helping me.

skimask
- 28th March 2008, 16:22
THis code works.Thank you for Helping me.
That codes works, yes.
But! Did you learn anything from it?

rano_zen06
- 28th March 2008, 16:57
That codes works, yes.
But! Did you learn anything from it?

of course, I will learn something from it.Although I'm new with PIC and the coding but I will give a try.

mister_e
- 28th March 2008, 19:44
Rano, don't worry about Skimask. This is his own way to tell you...

Hi : and: welcome : on : this : forum : . : We : are : glad : to : see : you : here : and : help : you : when : you : need : us : . : This : said : never : hesitate : to : ask : questions : . : Good : luck : !

skimask
- 29th March 2008, 07:25
Rano, don't worry about Skimask. This is his own way to tell you...
Good one... :)
What I was getting at was the fact that the O/P said he 'found the code on the internet' and didn't know how to make it do what he needed it to do...suggesting that maybe the O/P dove in head first, maybe without so much as first creating a 'blinky' and working up from there. 'cause quite frankly, that fix up there a couple of posts back is a very basic fix (change should be the operative word here).
In other words, I think a fish has been given...and more fish might soon be handed out...

T.Jackson
- 29th March 2008, 08:36
In other words, I think a fish has been given...and more fish might soon be handed out...

Don't you go stealing my fish, I'm not very good at sharing :D

skimask
- 29th March 2008, 08:43
Don't you go stealing my fish, I'm not very good at sharing :D

You got fish? Sounds like a slogan for something...

Got Fish?

T.Jackson
- 29th March 2008, 08:51
Got Fish?

After studying applied reasoning I can't help but wonder if maybe I do.

rano_zen06
- 29th March 2008, 11:40
It's ok skimask, I understand what you're trying to say. I'm just new in the world of PIC and know nothing about it.Now I need to deal with PIC to finish my project. So, I just do reverse Engineering..study the codes given and learn how it function.

Probotics
- 22nd May 2008, 09:53
All right I've seen this code a bit now and I have to admit I don't know WHY it works. I do know that may or may not work if you change the port (that's informative I know) which might be related to some weak pull up issue (?) or some other circuit design point I missed.

Here's the question - This code sets all values of the portD to zero

' Wait for keypress
For col = 0 to 3 ' 4 columns in keypad
PORTD = 0 ' All output pins low
TRISD = (dcd col) ^ $ff ' Set one column pin to output
row = PORTD >> 4 ' Read row
If row != $f Then gotkey ' If any keydown, exit


then changes the direction of one pin/column at a time to output instead of input. Great. The question is: why by simply changing the pin to output does the value of that pin go from low (portd=0) to high?

tenaja
- 22nd May 2008, 15:33
The output state of the pin is stored, even when it is an input. When you make it an input, it automatically goes wherever the input is pulling it. As soon as you make it an output, the old output value is restored.

The way to ensure it goes to the desired state is to set the value prior to TRISing it to an output.

Probotics
- 23rd May 2008, 15:41
Ah gotcha. So the column is switched to output, then the rows (which are 4 bits over) are examined to see if that row is linked to the column. Thanks - I don't know how I missed it now that I see it. This should help with a keypad with a different number of buttons I've been working on.

Probotics
- 24th May 2008, 13:16
So I remember now why I was having trouble following this. The value of the D port is 0. When set to output it draws the input port low. I didn't realize you had to have pull up resistors in the keypad circuit to keep the input port high whenever not being pulled low. The original circuit using this code that I saw had weak pull up resistors enabled and no external resistors, but the port I was using did not have weak pull up resistors associated with it.