4 x 4 Keypad on Port B (18F4550 with Lab X1)


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    RHKeypad.zip

    Code:
    '***************************************************************************
    '*  Name     : RHKeypad.pbp                                                *
    '*  Author   : Demon                                                       *
    '*  Date     : Apr 14 2015                                                 *
    '*  Version  : 1.0                                                         *
    '*  Notes    : 4 x 4 Keypad on Port B                                      *
    '*                                                                         *
    '*       B0 = Row 1                                                        *
    '*       B1 = Row 2                                                        *
    '*       B2 = Row 3                                                        *
    '*       B3 = Row 4                                                        *
    '*       B4 = Column 1                                                     *
    '*       B5 = Column 2                                                     *
    '*       B6 = Column 3                                                     *
    '*       B7 = Column 4                                                     *
    '*                                                                         *
    '*           Pull-up resistors are enabled.                                *
    '*           Set rows to output.                                           *
    '*           Set columns to input.                                         *
    '*           Process each row one at a time:                               *
    '*               Set selected row to 0 (VSS), other rows to 1 (VDD).       *
    '*               Pressing a button in that row connects the pin to VSS.    *
    '*               Check columns if a pin is connected to VSS.               *
    '*                                                                         *
    '*  Hardware : PIC 18F4550, 20mhz crystal                                  *
    '*             MeLabs U2 Programmer v4.32                                  *
    '*             Lab X1 board                                                *
    '*  Software : PIC Basic Pro v2.60C                                        *
    '*             MicroCode Studio Plus v2.2.1.1                              *
    '*             MPASM v5.46                                                 *
    '***************************************************************************
    '--- if you use these, you must comment the ones in the .inc file ---
    @   __CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
    @   __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
    @   __CONFIG    _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_1_2L & _VREGEN_ON_2L
    @   __CONFIG    _CONFIG2H, _WDT_OFF_2H
    @   __CONFIG    _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L
    
    ;--- Oscillator speed ----------------------------------------------------------
    
    DEFINE OSC 48
    
    ;--- Setup Registers -----------------------------------------------------------
    
    ADCON1 = %00001111                  ' A/D CONTROL REGISTER 1
    '   Bit 7-6 Unimplemented: Read as ‘0’
    '   Bit 5 VCFG1: Voltage Reference Configuration bit (VREF- source)
    '               1 = VREF- (AN2)
    '               0 = VSS
    '   Bit 4 VCFG0: Voltage Reference Configuration bit (VREF+ source)
    '               1 = VREF+ (AN3)
    '               0 = VDD
    '   Bit 3-0 PCFG3:PCFG0: A/D Port Configuration Control bits:
    '               1111 = All digital on Port A
    '                       See datasheet for table
    
    INTCON2 = %00000000                 ' INTERRUPT CONTROL REGISTER 2
    '   Bit 7 RBPU: PORTB Pull-up Enable bit
    '               1 = All PORTB pull-ups are disabled
    '               0 = PORTB pull-ups are enabled by individual port latch values
    '   Bit 6 INTEDG0: External Interrupt 0 Edge Select bit
    '               1 = Interrupt on rising edge
    '               0 = Interrupt on falling edge
    '   Bit 5 INTEDG1: External Interrupt 1 Edge Select bit
    '               1 = Interrupt on rising edge
    '               0 = Interrupt on falling edge
    '   Bit 4 INTEDG2: External Interrupt 2 Edge Select bit
    '               1 = Interrupt on rising edge
    '               0 = Interrupt on falling edge
    '   Bit 3 Unimplemented: Read as ‘0’
    '   Bit 2 TMR0IP: TMR0 Overflow Interrupt Priority bit
    '               1 = High priority
    '               0 = Low priority
    '   Bit 1 Unimplemented: Read as ‘0’
    '   Bit 0 RBIP: RB Port Change Interrupt Priority bit
    '               1 = High priority
    '               0 = Low priority
    
    ;--- Setup Port directions -----------------------------------------------------
    
        TRISA = %00000000
        TRISB = %11110000               '  Set columns to input, rows to output
        TRISC = %00000000
        TRISD = %00000000
        TRISE = %00000000
    
    ;--- Pins ----------------------------------------------------------------------
    
    Column1 VAR PORTB.4
    Column2 VAR PORTB.5
    Column3 VAR PORTB.6
    Column4 VAR PORTB.7
    
    ;--- Setup LCD -----------------------------------------------------------------
    
    DEFINE  LCD_DREG      PORTD         ' Set LCD data port
    DEFINE  LCD_DBIT      4             ' Set starting data bit
    DEFINE  LCD_RSREG     PORTE         ' Set LCD register select port
    DEFINE  LCD_RSBIT     0             ' Set LCD register select bit
    DEFINE  LCD_EREG      PORTE         ' Set LCD enable port
    DEFINE  LCD_EBIT      1             ' Set LCD enable bit
    DEFINE  LCD_BITS      4             ' Set LCD bus size
    DEFINE  LCD_LINES     2             ' Set number of lines on LCD
    DEFINE  LCD_COMMANDUS 1000          ' Set command delay time in microseconds
    DEFINE  LCD_DATAUS    50            ' Set data delay time in microseconds
    
    ;--- Variables (Constants) -----------------------------------------------------
    
    Row1    CON %00001110               ' Set selected row to VSS, others to VDD
    Row2    CON %00001101
    Row3    CON %00001011
    Row4    CON %00000111
    
    ;--- Program Start -------------------------------------------------------------
    
    Start:
    
        PAUSE 1500                      ' wait for PIC and LCD to stabilize
        LCDOUT  $FE,1,"18F4550 Keypad   "
    
    ;--- Main Program --------------------------------------------------------------
    
    ProgramMain:
    
        PORTB = Row1                    ' select row 1
        IF Column1 = 0 THEN LCDOUT  $FE,$C0,"Row 1, Column 1"
        IF Column2 = 0 THEN LCDOUT  $FE,$C0,"Row 1, Column 2"
        IF Column3 = 0 THEN LCDOUT  $FE,$C0,"Row 1, Column 3"
        IF Column4 = 0 THEN LCDOUT  $FE,$C0,"Row 1, Column 4"
    
        PORTB = Row2                    ' select row 2
        IF Column1 = 0 THEN LCDOUT  $FE,$C0,"Row 2, Column 1"
        IF Column2 = 0 THEN LCDOUT  $FE,$C0,"Row 2, Column 2"
        IF Column3 = 0 THEN LCDOUT  $FE,$C0,"Row 2, Column 3"
        IF Column4 = 0 THEN LCDOUT  $FE,$C0,"Row 2, Column 4"
    
        PORTB = Row3                    ' select row 3
        IF Column1 = 0 THEN LCDOUT  $FE,$C0,"Row 3, Column 1"
        IF Column2 = 0 THEN LCDOUT  $FE,$C0,"Row 3, Column 2"
        IF Column3 = 0 THEN LCDOUT  $FE,$C0,"Row 3, Column 3"
        IF Column4 = 0 THEN LCDOUT  $FE,$C0,"Row 3, Column 4"
    
        PORTB = Row4                    ' select row 4
        IF Column1 = 0 THEN LCDOUT  $FE,$C0,"Row 4, Column 1"
        IF Column2 = 0 THEN LCDOUT  $FE,$C0,"Row 4, Column 2"
        IF Column3 = 0 THEN LCDOUT  $FE,$C0,"Row 4, Column 3"
        IF Column4 = 0 THEN LCDOUT  $FE,$C0,"Row 4, Column 4"
    
        PAUSE 50                        ' Simple debounce delay
    
        Goto ProgramMain                ' Loop
    END
    Robert

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    Where is the bargraph display then? It’s too big for one port.

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


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    Quote Originally Posted by Art View Post
    Where is the bargraph display then? It’s too big for one port.
    What do you mean? I'm displaying on the LCD.

    Robert

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    Sorry, where is the 10 segment LEDs connected to?

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    The Lab X1 has the LCD and LED bargraph sharing port D.

    Robert

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    That’s what I mean, Port D is only eight bit port, but there are ten LEDs in the bargraph.
    Would you mind telling me what pins the bargraph is connected?
    I can Google pictures of the board, but can’t see that close.

    Quote Originally Posted by Demon View Post
    The Lab X1 has the LCD and LED bargraph sharing port D.

    Robert

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    Art,

    Here is the link to the schematic.
    http://melabs.com/downloads/labx1sch.pdf
    Regards,
    TABSoft

  8. #8
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 4 x 4 Keypad on Port B (18F4550 with Lab X1)

    Ok thanks, they only connected 8 of them to the port.
    Looking for ideas with a kind of dev board of my own.
    It might be a good idea to make something somewhat compatible with something commercial out there.

Similar Threads

  1. Keypad port problems
    By lockjawz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 13th March 2011, 22:20
  2. LAB-X1 keypad/timer
    By Bruce in forum Code Examples
    Replies: 0
    Last Post: - 25th July 2010, 02:18
  3. Making the Lab X1 communicate with the Lab X2
    By MatthewM in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 19th June 2008, 23:26
  4. 4x4 Keypad and LCD sharing port B
    By msnm4 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th May 2008, 09:13
  5. 18F4550 and keypad
    By mpardinho in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th April 2007, 23:26

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