Need help in matrix keypad coding


Closed Thread
Results 1 to 16 of 16
  1. #1
    Join Date
    Mar 2008
    Posts
    19

    Default Need help in matrix keypad coding

    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

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rano_zen06 View Post
    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?

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    try to change the following section
    Code:
    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
    Last edited by mister_e; - 28th March 2008 at 13:59. Reason: silly mistake
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Mar 2008
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    try to change the following section
    Code:
    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.

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rano_zen06 View Post
    THis code works.Thank you for Helping me.
    That codes works, yes.
    But! Did you learn anything from it?

  6. #6
    Join Date
    Mar 2008
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    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.

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Rano, don't worry about Skimask. This is his own way to tell you...
    Code:
    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 : !
    Last edited by mister_e; - 28th March 2008 at 19:48.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    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...
    Last edited by skimask; - 29th March 2008 at 07:30.

  9. #9
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    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

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by T.Jackson View Post
    Don't you go stealing my fish, I'm not very good at sharing
    You got fish? Sounds like a slogan for something...

    Got Fish?

  11. #11
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Got Fish?
    After studying applied reasoning I can't help but wonder if maybe I do.

  12. #12
    Join Date
    Mar 2008
    Posts
    19


    Did you find this post helpful? Yes | No

    Smile

    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.

  13. #13
    Join Date
    May 2008
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Ok, now WHY does it work

    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?

  14. #14
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    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.

  15. #15
    Join Date
    May 2008
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Thanks

    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.

  16. #16
    Join Date
    May 2008
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Recalled the issue

    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.

Similar Threads

  1. Matrix Keypad routine
    By mister_e in forum Code Examples
    Replies: 134
    Last Post: - 18th September 2022, 20:30
  2. calculator-like code entry with matrix keypad and display
    By Dennis in forum mel PIC BASIC Pro
    Replies: 35
    Last Post: - 16th December 2009, 22:58
  3. 3x4 matrix keypad
    By tazntex in forum Serial
    Replies: 3
    Last Post: - 14th October 2008, 20:13
  4. Inconsistent output on a 4x4 matrix keypad
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 24th November 2006, 03:54
  5. very basic matrix keypad question
    By kitcat in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th July 2006, 21:33

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts