Matrix Keypad routine


Closed Thread
Results 1 to 40 of 135

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Try something by yourself first.
    1. Download the Keypad Include file,
    2. Find LCDOUT in your manual,
    3. Use the example I gave in POST #1 and you're almost half done if you replace HSEROUT with LCDOUT.
    Steve

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

  2. #2


    Did you find this post helpful? Yes | No

    Default 4x4 keypad on an 184520 - newbie help

    Hi all

    Please would somebody be so kind as to shed some light on the problem I am experiencing.

    I am busy re-kindling my hobby and interest in PIC programming, I think these forums are an excellent resource, somehow all roads lead back here after many a google search and reading post after post and numerous articles.
    After 3 weeks of late nights and sore eyes and a lot of reading later I have finally gotten to a stage where I have a functional (perhaps a very under-utilized PIC 18F4530).
    After having blinked LED's and read switches and getting my LCD working I would like to implement a keypad as opposed to a bank of dip switches.

    I am trying to implement the keypad.bas file submitted by mister_e (Thanks mister e ... your code looks fantastic, I'm dying to use it properly :-)

    So here's where I'm at now....

    I am trying to connect a 4 x 4 matrix keypad to my 18F4520 PIC.

    Ultimately I would like to be able to be able to type in a number, end it with a # to signify completed input and then transmit the inputted number to another pic either wired or wireless.

    The keypad layout is as follows.
    my keypad matrix

    1 2 3 A
    4 5 6 B
    7 8 9 C
    * 0 # D


    I have connected the keypad to the pic as follows.

    Keypad PIC

    Column 0 PortA.0
    Column 0 PortA.1
    Column 0 PortA.2
    Column 0 PortA.
    Row 0 PortC.0
    Row 1 PortC.1
    Row 2 PortC.2
    Row 3 PortC.3

    10K pullups set on column inputs
    100 Ohm resistors in series with rows

    When I have the above configured my LCD just produces blocks

    If I remove either the pullups and/or the series resistors I just get "Key Value" 20 continuously appearing on the LCD and if I press a key on the keypad I get the "key Value" changing on the LCD but it is not the correct value according to my keypad.

    I could also use the whole of Port A but when I tried it but had the same results :-(

    Initially I tried to connect on PortB and had the same results, so I assumed it was because I was getting inputs from pin 39 and pin 40 of the pic where the PICkit 2 is connected for ICSP.

    I have attached my code below.

    Please could someone advise as to where I am going wrong or what I could look at to fix the problem !

    I think it could be a possible CONFIG problem or coding issue in my code.

    Any help would be greatly appreciated.

    Kind regards

    Dennis

    Code:
    '*************************************
    'LCD code for 16 X 2  HD4x lcd
    '*************************************
    
    'Ocsillator selections here
    OSCCON = $70            'Int CLK 8MHz
    OSCTUNE.6 = 1           'PLL 4x
    ADCON1= %00001111       '$0F = disable A/D converter
    'END of oscillator selections
    
    
    'Port IO directions and presets for port pins begin here
    'TRISA = %11111111       'All pins are outputs
    TRISA.0 = 1
    TRISA.1 = 1
    TRISA.2 = 1
    TRISA.3 = 1
    TRISB = %00000000           
    'TRISC = %00000000
    TRISC.0 = 1           
    TRISC.1 = 1
    TRISC.2 = 1
    TRISC.3 = 1
    
    TRISD = %00000000
    TRISE.0 = 0
    TRISE.1 = 0
    TRISE.2 = 0
    'End of Port IO directions and presets for port pins begin here
    
    'timer/oscillator defines 
    DEFINE OSC 32            '4x 8MHz
    'END of timer/oscillator defines
                          
    'variables begin here
    myvar var byte
    
    'LCD defines begin here   
    DEFINE LCD_BITS 4 	'defines the number of data interface lines (4 or 8) 
    DEFINE LCD_DREG PORTD 	'defines the port where data lines are connected to
    DEFINE LCD_DBIT 4 	'defines the position of data lines for 4-bit interface (0 or 4)
    DEFINE LCD_RSREG PORTD 	'defines the port where RS line is connected to
    DEFINE LCD_RSBIT 2 	'defines the pin where RS line is connected to 
    DEFINE LCD_EREG PORTD 	'defines the port where E line is connected to 
    DEFINE LCD_EBIT 3 	'defines the pin where E line is connected 
    DEFINE LCD_RWREG 0 	'defines the port where R/W line is connected to (set to 0 if not used)
    DEFINE LCD_RWBIT 0 	'defines the pin where R/W line is connected to (set to 0 if not used)
    DEFINE LCD_COMMANDUS 2000 	'defines the delay after LCDOUT statement 
    DEFINE LCD_DATAUS 200 		'delay in micro seconds
    'END of LCD DEFINES
    
    'KEYPAD 4x4 defines begin here
          DEFINE KEYPAD_ROW        4        ' 4 ROW keypad       
          DEFINE KEYPAD_ROW_PORT   PORTC    ' ROW port = PORTB
          DEFINE KEYPAD_ROW_BIT    0        ' ROW0 = PORTB.4
          DEFINE KEYPAD_COL        4        ' 4 COL keypad
          DEFINE KEYPAD_COL_PORT   PORTA    ' COL port = PORTB
          DEFINE KEYPAD_COL_BIT    0        ' COL0 = PORTB.0
          DEFINE KEYPAD_DEBOUNCEMS 200      ' debounce delay = 200 mSec
          DEFINE KEYPAD_AUTOREPEAT 1        ' use auto-repeat feature  
    'End of KEYPAD defines
    
    '*********************Includes******
    
    include "C:\PBP\SAMPLES\KeyPad.bas"
    'Includes end here
    
    'Code begins here
       Pause 500       ' Wait for LCD to startup
     
    start:
    @ READKEYPAD _myvar
    lcdout $fe, 1
    lcdout " Key Value = ",DEC2 myvar
    goto start

  3. #3


    Did you find this post helpful? Yes | No

    Default Solved !!

    My problem in the previous post here is now solved.
    The keypad is working.

    But I now am trying to solve 2 more problems

    1. I need the keypresses to reflect the correct label
    My matrix is
    1 2 3 A
    4 5 6 B
    7 8 9 C
    * 0 # D

    At the moment I get the following 1,2,3,4 as opposed to 1,2,3,A

    2. I would like to capture a number of keystrokes as I type them in for example 135 completing input with a #
    So the number stored will be the number decimal 135

    Any help or suggestions will be most appreciated.

    Kind regards

    Dennis
    For example keypress1

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    An idea would be to use the lookup command for your translation from numbers to numbers-ASCII conversion.

    Now about the numbers to assebly in a byte. What is the range of your input?

    0-255 or 0-999?

    Ioannis

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Hi Ioannis

    Thanks for the reply :-)
    Lookup is already implemented and working .

    My range would be anything really (inifinity ??) but I guess I could use 0 to 999

    It's like dialling on a phone ... you dial the numbers and then press the dial button :-)

    I want to type numbers in and then press the # key and * to backspace or clear last entry ...

    Kind regards

    Dennis

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    Well, if you go for the 999 then a 0-65535 is possible as the final result needs a word variable.

    One idea untested is this:

    Code:
    array   var byte[5]
    index   var byte
    i       var byte
    word_v  var word
    
    'Get keypress
    index=0
    
    loop:
    gosub key_read
    if mykey=32 then loop 'No key press returns 32
    array[index]=myvar
    index=index+1
    if index=5 then calculate_result
    goto loop
    ...
    
    caculate_result:
    word_var=0:mult=10000
    for i=index-1 to 0 step -1
        word_var=word_var*mult+array[i]
        mult=mult/10
    next i
    index=0
    goto display_result
    As there is the index counter, there is no need to press '#'. Numbers must be entered in 5 digit format, e.g. 00123.

    Also there is no filter if the user keys in a number greater than 65535, in which case result will be of course wrong due to overflow.

    Ioannis
    Last edited by Ioannis; - 23rd November 2009 at 22:02.

  7. #7


    Did you find this post helpful? Yes | No

    Default ooops

    Hi Ioannis

    Thank you for the code example... I'm just thinking that perhaps it would indeed be more practical to use 255 as the max number, in which case I would need to declare a byte as opposed to a word ...not so ?

    Kind regards

    Dennis

Similar Threads

  1. calculator-like code entry with matrix keypad and display
    By Dennis in forum mel PIC BASIC Pro
    Replies: 35
    Last Post: - 16th December 2009, 23:58
  2. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 14:16
  3. I2C PCF8574 4X4 Keypad routine
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th January 2007, 22:25
  4. Inconsistent output on a 4x4 matrix keypad
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 24th November 2006, 04:54
  5. very basic matrix keypad question
    By kitcat in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th July 2006, 22: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