Quick 4x4 keypad matrix question


Closed Thread
Results 1 to 16 of 16
  1. #1
    Join Date
    Jun 2011
    Location
    St. Louis, MO
    Posts
    13

    Default Quick 4x4 keypad matrix question

    hello, quick question about keypad matrices.

    I aquired a 4x4 matrix keypad for free and was trying to understand its operation in PBP. I started by using Bruce Reynolds code at: http://www.rentron.com/serkey16.htm.

    All i am doing is waiting for a key to be pressed, then output the pressed key using HSEROUT on a PIC16F877@20Mhz.

    I am using the schematic from rentron.



    ** The only change on my breadboard are resistors R1-R4 are 1k, not 270. **

    The program kinda of works....sometimes when a button is pressed, the number sent via HSEROUT is actually the number below it.

    i.e. Say the number "3" is pressed...most of the time the number "3" is displayed...but sometimes the number displayed will be "6", which is the button just below the "3" on the keypad.

    Here is my slightly modified code...
    Code:
    '************************************************************
    '*  Name    : serkey16.bas                                  *
    '*  Author  : Reynolds Electronics                          *
    '*  Date    : 07/12/2001                                    *
    '*  Version : 1.0                                           *
    '*  Notes   : PicBasic Pro program for 16-key serial keypad *
    '************************************************************
    DEFINE OSC 20            '// Define Oscillator @ (X)mHz
    include "ansi.inc"
    Include "modedefs.bas"
    
    
    '---------------------------------------------------------------------
    '---------- Define LCD Options ---------------------------------------
    '---------------------------------------------------------------------
    Define LCD_DREG  PORTD         '// Define the data port
    define LCD_DBIT  0             '// Select port bit [0-lower or 4-upper]
    define LCD_RSREG PORTC         '// Select RS port
    DEFINE LCD_RSBIT 4             '// Set RS bit (port number)
    define LCD_EREG  PORTC         '// Set E port
    define LCD_EBIT  5             '// Set E bit
    define LCD_BITS  4             '// 4 or 8 bit LCD
    
    
    '---------------------------------------------------------------------
    '----------[Define HSEROUT Options]-----------------------------------
    '---------------------------------------------------------------------
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_BAUD 9600
    'DEFINE HSER_SPBRG 25       '// commented out to get 9600baud working
    
    
    col         VAR   BYTE       '// Keypad column
    row         VAR   BYTE       '// Keypad row
    key         VAR   BYTE       '// Key value
    'baud        VAR   PORTA.0    '// Baud select pin
    baud        VAR   bit         '// let baud be compiler controlled
    trx_pin     VAR PORTA.0         '// serial output pin
    mode        var word
    
    'CMCON       =     7         '// PortA = digital I/O
    'VRCON       =     0          '// Voltage reference disabled
    'TRISA       =     %00000001  '// PortA.0 = baud select pin
    OPTION_REG.7 =    0          '// Enable PORTB pull-ups
    mode = 16780
    baud = 1                     '// set initial baud rate. for testing
    
    pause 300                   '// let LCD initialize
    
    @ ClearScr                     '// clear screen
    @ CurHome                      '// return cursor to home
    
    startup:
      lcdout $FE,1
      lcdout "-Serial Keypad-"
      hserout ["------------------------------",10,13]
      hserout ["-----[Serial Keypad Demo]-----",10,13]
      hserout ["------------------------------",10,13,13,13,13]
      
    
    loop:
            GOSUB getkey          'Get key from keypad
    send:
            IF baud = 1 THEN fast'If baud = 1 then N9600,else N2400
    	    hserout [10,13,dec key,10,13]
            'SEROUT2 trx_pin,N2400,[key]'Send key value out PortA.1
            GOTO loop
    fast:
    	hserout [dec key,10,13]
        'SEROUT serpin,N9600,[key]
            GOTO loop               'Do it forever
    getkey: 
            PAUSE 100                'Debounce key-input
    getkeyu:' Wait for all keys up
            PORTB = 0            ' All output-pins low
            TRISB = $f0          ' Bottom 4-pins out, top 4-pins in
            IF ((PORTB >> 4) != $f) THEN getkeyu'If keys down, loop
            PAUSE 50             ' Debounce key-input
    
    getkeyp:' Wait for keypress
            FOR row = 0 TO 3        ' 4 rows in keypad
                PORTB = 0           ' All output-pins low
                TRISB = (DCD row) ^ $ff ' Set one row pin to output
                col = PORTB >> 4    ' Read columns
                IF col != $f THEN gotkey' If any keydown, exit
            NEXT row
            GOTO getkeyp            ' No keys down, go look again
    
    gotkey: ' Change row and column to key number 1 - 16
            key = (row * 4) + (NCD (col ^ $f))
            'NOTE: for 12-key keypad, change to key = (row * 3)
            RETURN                  ' Subroutine over
            
            END
    Any help would be greatly appreciated. I have a strong feeling it is something simple. Thanks in advance.

    Sean

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Hi Sean,
    You have a flair for understatement, "slightly modified" It looks modified for all get out to me.
    Question1. Why are you putting in LCD defines if you are using the USART, Do you want dual display?

    Question 2. Since you did not specify which PIC you are using, which port is your PIC equipped to use the USART ? Is it the same as the keypad uses?

    Question 3. Why did you change the value of the resistors ? I suspect Bruce proved out his design before posting it on his website.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Yup, I'd start with using the same resistor values and logic as Bruce and get that working first. Then I'd tweak the code to my liking/needs and changing hardware.

    Archangel is right. Bruce adds little comments like this one for a reason:

    The original prototype was designed using the PIC16F628, but the code shown below can easily be modified to work on any PIC with sufficient I/O capacity for the keypad interface, and the two lines required for the serial output-pin, and baud-rate selection pin.

    CMCON is the comparator mode selection register. Loading a value of 7 into CMCON sets-up the PortA I/O-pins as digital, while turning OFF the comparators.

    If you prefer to use another PIC that has A/D on PortA, simply replace CMCON = 7 with ADCON1 = 7 to turn OFF the A/D peripheral, and set PortA to digital.
    Last edited by Demon; - 19th June 2011 at 03:42.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  4. #4
    Join Date
    Jun 2011
    Location
    St. Louis, MO
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Thanks for the reply Archangel. In response to your questions...

    1. Why are you putting in LCD defines if you are using the USART, Do you want dual display?
    The LCD is used just for quick on-the-spot feedback. It was part of another program, so I usually just cut-and-paste it into other programs. The USART is just for debugging purposes. Plus it remindes me of the old BBS days .

    2. Since you did not specify which PIC you are using, which port is your PIC equipped to use the USART ? Is it the same as the keypad uses?
    At the beggining I stated I was using an PIC 16F877 @ 20 mhz. The hardware USART TX/RX are ports C6/C7 on the 16F877 which are only being used for serial communication. The keypad is is connected to PORTB.0 to PORTB.7.

    3. Why did you change the value of the resistors ? I suspect Bruce proved out his design before posting it on his website.
    I was under the impression that the value of the resistors were not that important to a certain extent. While researching keypad matrices, a few people stated that any value <= 10k would be sufficient. I had 1k resistors with me at the time so i gave it a shot. I just replaced the 1k resistors with 270-ohm. It seemed to make the problem occur only on one button (row3-col1). While pressing the button repeatedly, the value displayed will sometimes be the value of the button directly below it, but only intermittently.

    By changing the 1k resistors for 270 helped eliminate the problem on all the buttons except the one (row3-col1). I still cant figure what is causing this.

    thanks again for the help.

    Sean

  5. #5
    Join Date
    Jun 2011
    Location
    St. Louis, MO
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Thanks for the reply Demon.
    I didnt think these comments from Bruce really applied to my problem.

    PortA's pins were used for SEROUT and the baud selection portion of the original program. I changed the SEROUT to HSEROUT thinking it might change the end result of displaying the correct value and not of the button below it.

    The other port was for baud rate selection for the SEROUT command. By using the HSEROUT and defining its properties at the begining, I thought it would be easier to narrow down the problem.

    I still am using PORTB for the keypad just like in Bruce's code. By changing the resistors it worked better, but not perfect. I just cant seem to understand why it is doing it only on the one button.

    Thanks again,

    Sean

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Quote Originally Posted by triton99 View Post
    Thanks for the reply Archangel. In response to your questions...

    1. Why are you putting in LCD defines if you are using the USART, Do you want dual display?
    The LCD is used just for quick on-the-spot feedback. It was part of another program, so I usually just cut-and-paste it into other programs. The USART is just for debugging purposes. Plus it remindes me of the old BBS days .

    2. Since you did not specify which PIC you are using, which port is your PIC equipped to use the USART ? Is it the same as the keypad uses?
    At the beggining I stated I was using an PIC 16F877 @ 20 mhz. The hardware USART TX/RX are ports C6/C7 on the 16F877 which are only being used for serial communication. The keypad is is connected to PORTB.0 to PORTB.7.

    3. Why did you change the value of the resistors ? I suspect Bruce proved out his design before posting it on his website.
    I was under the impression that the value of the resistors were not that important to a certain extent. While researching keypad matrices, a few people stated that any value <= 10k would be sufficient. I had 1k resistors with me at the time so i gave it a shot. I just replaced the 1k resistors with 270-ohm. It seemed to make the problem occur only on one button (row3-col1). While pressing the button repeatedly, the value displayed will sometimes be the value of the button directly below it, but only intermittently.

    By changing the 1k resistors for 270 helped eliminate the problem on all the buttons except the one (row3-col1). I still cant figure what is causing this.

    thanks again for the help.

    Sean

    Hi Sean, Ok . . .

    1.OK . . . had to ask . .
    2. Sorry old eyes, I missed that. Ok that's good.
    3. Looked at Bruce's page again, his statement proves you right . . . non critical.
    This code is floating around the forum in many threads, with many variations, and the only thing I see is no TRIS settings for your ports B,C, & D. In the 1 Mr._E did he set the portb Tris as follows: TRISB = %11110000 , he also used an interrupt to detect the keypress, it maybe your finger is faster than your loop, especially given you are using 3 different display protocols. I would comment out any code you are not pressed to use and see if it improves, also you might consider debug over serout as it executes faster using less code space ( or so they tell me) I have not yet proven that last statement to myself, but it is a bit faster, in terms of how long a loop takes to execute,
    oh and try these defines (from mister E's famous picmulti calc)
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    Last edited by Archangel; - 19th June 2011 at 11:39. Reason: I really miss the Old Forum's text format
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    The program kinda of works....sometimes when a button is pressed, the number sent via HSEROUT is actually the number below it.

    i.e. Say the number "3" is pressed...most of the time the number "3" is displayed...but sometimes the number displayed will be "6", which is the button just below the "3" on the keypad.
    Double check your hardware.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    What's in the INCLUDE "ansi.inc" file ?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  9. #9
    Join Date
    Jun 2011
    Location
    St. Louis, MO
    Posts
    13


    Did you find this post helpful? Yes | No

    Smile Re: Quick 4x4 keypad matrix question

    I just assembled the same circuit using an 16f88 instead of the 16F877. Using the same circuit and code[plus changes for the 16F88], the program works perfect.

    These are the register setting I use on the 16F88.

    Code:
    CMCON =     7                   '// PortA = digital I/O
    OPTION_REG.7  =  0              '// Enable PORTB pull-ups
    ADCON1  =  7 
    ANSEL=%00000000                 '// set all analog pins to digital
    ANSEL = 0                       '// disable ADC
    With the circuit working on the 16F88, I resumed working on the 16F877.

    I rebuilt and re-checked the wiring on the 16F877. I also got rid of all the extra LCDOUT and HSEROUT commands, while using only the SEROUT command, just like the Bruce's original example. It still has the weird problem. I just cant seem to pinpoint what is causing the error. Its weird too, because I dont have to be pressing the button fast....it does it even if when I press the button slowly. Every once in a while it will display the number below instead of the actual number. Arghhhhhh!!!! I love this stuff .

    *Archangel, the "ANSI.INC" was from Darrel Taylors easy ANSI module for PBP.
    It is the main reason I wanted to use HSEROUT. The ansi.inc only works with the hardware USART, and not with SEROUT(2).

    Thanks for the help, Im not giving up on this.

    Sean
    Last edited by triton99; - 20th June 2011 at 04:03.
    "Reality is merely an illusion, albeit a very persistent one." , Albert Einstein

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Ok, just wanted to know, oh and I do see where you set TrisB in your orig modified code. Maybe as Dave mentioned there is a hardware problem, bad breadboard or the pic it'self. Those breadboards get corrosion inside them and then the fun begins.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Internal pull-up may sucks, check OPTION reg in the datasheet and see what happen when you set a pin to output... probably the internal pull-up get disabled 'till you set them again... use external one and post your results.
    Steve

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

  12. #12
    Join Date
    Jun 2011
    Location
    St. Louis, MO
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    I finally traced the problem down.

    Im ashamed to post this, but it was just the ICSP cable still connected to my U2 programmer. Usually I leave it connected when programming with pics. On the 16F877, the PGD and PGC pins on the 16F877 are located on PORTB (B7 & B6). As soon as the cable is removed, "boom goes the dynamite." Ive never had a problem like that before, but I also never tried using a keypad with the 16F877.

    Thanks again everyone for all the help, it was greatly appreciated. Sorry I wasted everyones time.

    Lesson learned, RTFM.

    Also is there anything I can do to allow the ICSP adapter to be connected and still be able to have the keypad on PORTB. Or is it just good practice to remove the cable after programming the PIC?
    "Reality is merely an illusion, albeit a very persistent one." , Albert Einstein

  13. #13
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Problems are nearly always simple, figuring out what they are . . . hard.

    Jumpers, DIP switch maybe resistors and diodes . . .
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  14. #14
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    Quote Originally Posted by triton99 View Post
    ...
    Also is there anything I can do to allow the ICSP adapter to be connected and still be able to have the keypad on PORTB. Or is it just good practice to remove the cable after programming the PIC?

    Unless I am mistaken, you can also use the ICSP cable to debug your program. So yes, in that case you'd have to leave the cable connected after programming the PIC. You just have to make sure your program does not interfere with Port B pins 6 and 7.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  15. #15
    kamranforu's Avatar
    kamranforu Guest


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    plz tell me if i want to connect the keypad to portc
    with diagrame of pull up resistor and values of resisters

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


    Did you find this post helpful? Yes | No

    Default Re: Quick 4x4 keypad matrix question

    the "diagram" is shown in post #1, pull-up of 10K or so are good enough
    Steve

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

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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