4x4 keypad Help


Closed Thread
Results 1 to 7 of 7

Thread: 4x4 keypad Help

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    3

    Exclamation 4x4 keypad Help

    Hi all
    i am using a 18f452 with the 4 x4 keypad connected to portB
    this is on the proton Development board and the connections are as the attachment below
    All is get displayed on my lcd is

    " Keypad Test "
    "Press any key"
    I have ran the program on micro studio ICD and the program seems to be struck
    in this loop
    ==========================================
    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 any keys down, loop

    Pause 50 ' Debounce
    ================================================== ====


    Is there something i am missing
    i have the complete code below and

    ================================================== ==============
    DEFINE LOADER_USED 1 ' uses a bootloader

    @ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H

    Define OSC 20
    clear

    DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
    DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
    ' PORTD-4 thru PORTD-7 connects to
    ' LCD DB4 thru LCD DB-7 respectively
    DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
    DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
    DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
    DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

    DEFINE LCD_BITS 4 ' Using 4-bit bus
    DEFINE LCD_LINES 2 ' Using 2 line Display
    DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
    DEFINE LCD_DATAUS 50 ' Data Delay (uS)

    ' ** Define LCD Control Constants **

    I CON 254 ' Control Byte
    Clr CON 1 ' Clear the display
    Line1 CON 128 ' Point to beginning of line 1
    Line2 CON 192 ' Point to beginning of line 2
    Line3 CON 148 ' Point to beginning of line 3
    Line4 CON 212 ' Point to beginning of line 4

    '
    ' Initialise Hardware
    ' -------------------

    TRISA=%00000000
    TRISB=%00000000
    TRISC=%10000000
    TRISD=%00000000

    INTCON2.7 = 1 ' Enable PORTB pullups
    ADCON1 = 7 ' Make PORTA and PORTE digital
    Low PORTE.2 ' LCD R/W low (write)

    Pause 1000 ' Wait for LCD to start

    lcdout I,Clr:Pause 30
    lcdout I,Line1," Keypad TEST "
    lcdout I,Line2,"..Power On.. !!"

    ' Define program variables
    col var byte ' Keypad column
    row var byte ' Keypad row
    key var byte ' Key value



    lcdout I,Clr:Pause 30
    lcdout I,Line1," Keypad Test "
    lcdout I,Line2, "Press any key" ' Display sign on message

    loop: Gosub getkey ' Get a key from the keypad
    lcdout I,Clr:Pause 30
    lcdout I,Line3, #key
    Pause 50
    Goto loop ' Do it forever


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

    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 any keys down, loop

    Pause 50 ' Debounce

    getkeyp:
    ' Wait for keypress
    For col = 0 to 3 ' 4 columns in keypad
    PORTB = 0 ' All output pins low
    TRISB = (dcd col) ^ $ff ' Set one column pin to output
    row = PORTB >> 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

    Best Regards
    Sabu
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default

    Yes it will stuck, you need some pull-up resistors in that. You can enable the PIC internal one
    Code:
    INTCON2.7=0
    Just saw you already did.. just the wrong way

    AND you WILL have erratic results with this routine... add a PAUSEUS 5 in this section
    Code:
    TRISB = (dcd col) ^ $ff ' Set one column pin to output
    PAUSEUS 5
    row = PORTB >> 4 ' Read row
    Last edited by mister_e; - 13th April 2008 at 22:46.
    Steve

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

  3. #3
    Join Date
    Apr 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Question

    Thank mister_e for your reply
    i am a bit confused by what you mean by

    just the wrong way

    Also without doing anything else to the program apart from changing the Enable PORTB pullups line when i used a 16f877a

    ================================================== =
    OPTION_REG.7 = 0 ' Enable PORTB pullups for 16f877a
    'INTCON2.7 = 1 ' Enable PORTB pullups for 18f452
    ================================================== =

    the program works fine could it be my pic?

    Best Regards
    Sabu

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


    Did you find this post helpful? Yes | No

    Default

    In your first post you used...
    INTCON2.7=1

    But this actually DISABLE the internal pull-up...

    Use
    INTCON2.7=0
    instead, and the problem will disapear
    Steve

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

  5. #5
    Join Date
    Apr 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    Oops silly me
    it works fine

    thanks a lot

    Sabu

  6. #6
    andymarrole's Avatar
    andymarrole Guest


    Did you find this post helpful? Yes | No

    Default warning unable to include keypas.bas and lcd.bas

    i using pic18f4550 and PICBASIC PRO version 1.0 as my compiler..
    while i compile my program below, there is warning which is

    unable to include file keypad.bas
    unable to include file lcd.bas..

    this is my source code...
    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/5/2010                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    Include "modedefs.bas" 
    
    DEFINE PIC18F4550
    DEFINE OSC 4
    
    
    INCLUDE "KeyPad.bas" ' ( mister_e 's great keypad include file )
    INCLUDE "lcd.bas"  
    CMCON = 7
    ADCON1 = 7
      
              
    TRISD  = %00000000
    TRISB  = %11111111
                 
    '// Declare Variables...
    Col_A     VAR PORTB.0
    Col_B     VAR PORTB.1
    Col_C     VAR PORTB.2
    Col_D     VAR PORTB.3
    Row_A     VAR PORTB.4
    Row_B     VAR PORTB.5
    Row_C     VAR PORTB.6
    Row_D     VAR PORTB.7
    
    
    
    Scan_Col  VAR BYTE       ' Counter - current col  
    Key_Press VAR BYTE       ' Value of key (0-9) & * + # + A + B + C + D
    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:
    
      @ incf _Scan_Col, 1   ' Inc col pos...
    
      SELECT CASE Scan_Col  ' Col (1-4)
             
             CASE 1
                  Col_A = 0 ' Switch on col (active low)    
                  Col_B = 1 ' Col off    
                  Col_C = 1 ' Col off 
              Col_D = 1 ' Col off
                           
                  '// 1 Key
                           IF Row_A = 0 THEN        ' Key down? ... 
                              IF Allow_Key = 0 THEN ' Any other key down?
                                 Key_Press = 1      ' Load var w/value of key           
                                 Allow_Key = 1      ' Disallow other keys
                              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
                  
             CASE 2
                  Col_A = 1    
                  Col_B = 0    
                  Col_C = 1 
                  Col_D = 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
                  Col_D = 1
    
                  '// 3 Key
                           IF Row_A = 0 THEN
                              IF Allow_Key = 0 THEN 
                                 Key_Press = 3                 
                                 Allow_Key = 1
                              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 4
                  Col_A = 1     
                  Col_B = 1     
                  Col_C = 1
                  Col_D = 0
    
                  '// A Key
                           IF Row_A = 0 THEN
                              IF Allow_Key = 0 THEN 
                                 Key_Press = 65                 
                                 Allow_Key = 1
                              ENDIF   
                           ENDIF
                  '// B Key
                           IF Row_B = 0 THEN
                              IF Allow_Key = 0 THEN 
                                 Key_Press = 66                 
                                 Allow_Key = 1
                              ENDIF   
                           ENDIF
                   '// C Key
                          IF Row_C = 0 THEN
                              IF Allow_Key = 0 THEN 
                                 Key_Press = 67                 
                                 Allow_Key = 1
                              ENDIF   
                           ENDIF
                  '// D Key
                           IF Row_D = 0 THEN
                              IF Allow_Key = 0 THEN 
                                 Key_Press = 68                
                                 Allow_Key = 1
                              ENDIF   
                           ENDIF
    
    
                  Scan_Col = 0   
    END SELECT
    
    
    LCDOUT $fe,1
     LCDOUT " Key_Press "
    
    
    
    
    
    '// Check for key release, all cols on (active low) ...
    Col_A = 0     
    Col_B = 0     
    Col_C = 0
    Col_D = 0
    
    IF Row_A = 1 THEN
      IF Row_B = 1 THEN
         IF Row_C = 1 THEN
            IF Row_D = 1 THEN
               Allow_Key = 0
               Key_Down = 0
               Key_Press = 255
            ENDIF
         ENDIF
      ENDIF
    ENDIF
               
    GOTO Scan_Keypad

    please help me...
    thank you...

Similar Threads

  1. connecting a 4X4 keypad
    By fadibasic in forum Schematics
    Replies: 6
    Last Post: - 12th November 2008, 17:52
  2. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 13:16
  3. 4x4 Keypad and LCD sharing port B
    By msnm4 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th May 2008, 08:13
  4. I2C PCF8574 4X4 Keypad routine
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th January 2007, 21:25
  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