a little help with keypad on 18f4520 please


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1

    Default a little help with keypad on 18f4520 please

    Hi there

    I'm at wit's end ...

    I have been battling to get my matrix keypad(s) working, I have both a 4x3 and a 4x4 keypad.
    Two nights ago I stunbled across some postings and code in the forums which looked like they may be what I am looking for (with a few changes perhaps).

    After trying out both (namely the one from mister e and Steve) I still have not come right and can't understand what I am doing wrong.

    Please see my other posts on mister e's keypad code pages here
    http://www.picbasic.co.uk/forum/show...?t=3250&page=3
    as well as Steve's on this post(hope cross posting is ok):

    http://www.picbasic.co.uk/forum/show...0999#post80999.

    With Steve's implemetation I get only a "||" character on the LCD.

    Attached is my code.

    I am almost convinced I am not setting the registers correctly or calling a variable correctly :-( I was quite happy working with a 16f628 but now that I've re-kindled PIC's as hobby again I decided to go with an 18F series and it's taking some getting used to.

    If some kind soul would help me I would really appreciate it.
    Comments and suggestion or tips and tricks would be more than welcomed too.

    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
    'TRISB = %00000000           
    'for keypad
    '// Define port pins as inputs and outputs ...
       TRISA  = %00001000
       TRISB  = %00001101
    TRISC = %00000000
    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 code here
     '// 3x3 Keypad - Rows as inputs, Cols as outputs...
       'Rows: RA3      Cols: RB1                     
       '      RB3            RA2                      
       '      RB2            RA4                      
       '      RB0                                     
       '//
       ' RB5: LED & Buzzer ...                            
       ' RA0: TX to PC's Serial Port                  
            
                        
       '// Declare Variables...
       Col_A     VAR PORTB.1
       Col_B     VAR PORTA.2
       Col_C     VAR PORTA.4
       
       Row_A     VAR PORTA.3
       Row_B     VAR PORTB.3
       Row_C     VAR PORTB.2
       Row_D     VAR PORTB.0
      
       Buzzer    VAR PORTB.5
       RX_To_PC  VAR PORTA.0
        
       Scan_Col  VAR BYTE       ' Counter - denoting current col in scan 
       Key_Press VAR BYTE       ' Contains value of key (0-9) & * + #
       Key_Down  VAR BYTE       ' Flag set true when key is depressed
       Allow_Key VAR BYTE       ' Flag - disallow multiple keys being pressed
       I         VAR byte       ' General working var
        
       Scan_Keypad:
       
       '// Scan cols 
          @ incf _Scan_Col, 1   ' Inc col pos...
        
          SELECT CASE Scan_Col  ' Col (1-3)
                 
                 CASE 1
                      Col_A = 0 ' Switch on col (active low)    
                      Col_B = 1 ' Col off    
                      Col_C = 1 ' Col off   
                               
                      '// 3 Key
                               IF Row_A = 0 THEN        ' Key down? ... 
                                  IF Allow_Key = 0 THEN ' Any other key down?
                                     Key_Press = 3      ' Load var w/value of key           
                                     Allow_Key = 1      ' Disallow other keys
                                  ENDIF   
                               ENDIF                          
                      '// 6 Key
                               IF Row_B = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 6                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// 9 Key
                               IF Row_C = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 9                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// # Key
                               IF Row_D = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 35                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      
                 CASE 2
                      Col_A = 1    
                      Col_B = 0    
                      Col_C = 1    
                               
                      '// 2 Key
                               IF Row_A = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 2                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// 5 Key
                               IF Row_B = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 5                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// 8 Key 
                               IF Row_C = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 8                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// 0 Key
                               IF Row_D = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 0                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
    
                 CASE 3
                      Col_A = 1     
                      Col_B = 1     
                      Col_C = 0
    
                      '// 1 Key
                               IF Row_A = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 1                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// 4 Key
                               IF Row_B = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 4                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                       '// 7 Key
                              IF Row_C = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 7                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                      '// * Key
                               IF Row_D = 0 THEN
                                  IF Allow_Key = 0 THEN 
                                     Key_Press = 42                 
                                     Allow_Key = 1
                                  ENDIF   
                               ENDIF
                           end select
    'end keypad code
    
    '*********************Includes******
    
    'Includes end here
    
    
    
    
       Pause 500       ' Wait for LCD to startup
    
    'loop1:   
     '  Lcdout $fe, 1   ' Clear LCD screen
      '  Lcdout "Hello"  ' Display Hello
       'Pause 1000       ' Wait .5 second
       'Lcdout $fe, $C0   ' Clear LCD screen
       'Lcdout "World"
       'pAUSE 1000
       'Lcdout $fe, 1   ' Clear LCD screen
       
       'Pause 1000       ' Wait .5 second
    
     'Goto loop1       ' Do it forever
     
    'start:
    '@ READKEYPAD _myvar
    'lcdout $fe, 1
    lcdout Key_Press
    'goto start

  2. #2


    Did you find this post helpful? Yes | No

    Default

    OKay I have even tried this example
    http://www.rentron.com/serkey16.htm
    and still get || on my lcd but the display does hange to things like ||| when I press some keys.

    I really can't see where I am messing up ... please please could someone help me with this

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Tray to use LCDout Dec Key_Press and see if you get the correct ascii value.

    Al.
    Last edited by aratti; - 20th November 2009 at 15:30.
    All progress began with an idea

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Thanks Al

    I now see numbers and not ||

    OK so this is how I am now setup ...
    http://www.rentron.com/serkey16.htm
    notice there are no pull-ups on the rows.

    I have series resistors on for the columns (100 OHM)
    In other words KEYPAD >> RESISTOR >> PIC (PortB4-7)
    Aah at least I am now seeing characters
    When Idle the LCD just displays 4 8 12 and the 16 and then it repeats :-(
    What's wrong :-(

    My wishlist
    A schematic( where to wire the pull-ups and where to wire the pull downs.
    I would prefer not to use Port.B since I have the pickit 2 setup for ICSP on PortB.6 and B.7.
    So which ports are advisable to use ? How should I TRIS each ?

    Some code shows keypad ports as input tohers as output ... some show pull-ups others show no pull-ups ?

    This is getting mega confusing

    Should I just stick with dipswitches and give up on the dream of using a keypad for input ?

    Any more help or comments would be seriously appreciated

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    OK so this is how I am now setup ...
    http://www.rentron.com/serkey16.htm
    notice there are no pull-ups on the rows.
    You don't see any pullup because they are using internal pullup on portB

    See the instruction that activate the weak pullup in the code.

    OPTION_REG.7 = 0 ' Enable PORTB pull-ups

    If you use different port then you have to use external resistors.

    Al.
    All progress began with an idea

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Hi again Al

    Thank you again for the reply :-)

    Adding OPTION_REG.7 = 0 ' Enable PORTB pull-ups
    Shows a compile error in Microstudio
    Does not compile
    I am using an 18F4520

    So I am back to square one

    Any suggestions

Similar Threads

  1. 4x4 keypad Help
    By aaliyah1 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 5th October 2010, 16:34
  2. Universal Keypad System
    By aratti in forum Code Examples
    Replies: 3
    Last Post: - 18th January 2009, 13:06
  3. Keypad input test
    By Kalind in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th October 2008, 04:00
  4. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 13:16
  5. Inconsistent output on a 4x4 matrix keypad
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 24th November 2006, 03:54

Members who have read this thread : 0

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