Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PicBasic Keypad program

Status
Not open for further replies.

Sora

New Member
I found this Keypad and lcd program on a ME wesite

Code:
===========================================
' PicBasic Pro program to display key number on LCD

' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.
Define    LOADER_USED    1

' Define LCD connections
Define  LCD_DREG        PORTD
Define  LCD_DBIT        4
Define  LCD_RSREG       PORTE
Define  LCD_RSBIT       0
Define  LCD_EREG        PORTE
Define  LCD_EBIT        1
' Define program variables
col     var     byte            ' Keypad column
row     var     byte            ' Keypad row
key     var     byte            ' Key value

        OPTION_REG.7 = 0        ' Enable PORTB pullups

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

        Pause 100               ' Wait for LCD to start

        Lcdout $fe, 1, "Press any key"  ' Display sign on message

loop:   Gosub getkey            ' Get a key from the keypad
        Lcdout $fe, 1, #key     ' Display ASCII key number
        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
==========================================

it uses portB for key pad and portD for LCD. i was trying to play around with it by changing the key pad port to portD, and the lcd to portB. Since i only changing ports, so i thought it would be easy. I modified the program alittle (below)

Code:
===========================================
' PicBasic Pro program to display key number on LCD
'PORTB and PORTE for LCD
'PortD for keypad

' Define LOADER_USED to allow use of the boot loader.
' This will not affect normal program operation.
Define    LOADER_USED    1

' Define LCD connections
Define  LCD_DREG        PORTB    'set LCD data port
Define  LCD_DBIT        4    'set starting databit (0 or 4) if 4-bit bus
Define  LCD_RSREG       PORTE    'set LCD register select port
Define  LCD_RSBIT       0    'set RE0 as RS bit (pin4 LCD)
Define  LCD_EREG        PORTE    'set LCD enable port
Define  LCD_EBIT        1    'set RE1 as enable bit (pin6 LCD)
                     ' All output pins low

' Define program variables
col     var     byte            ' Keypad column
row     var     byte            ' Keypad row
key     var     byte            ' Key value
      
        ADCON1 = 7              ' Make PORTA and PORTE digital
    '   OPTION_REG.7 = 0        ' Enable PORTb pullups  
  
        Low PORTE.2             ' LCD R/W low (write)

        Pause 100               ' Wait for LCD to start

        Lcdout $fe, 1, "Press any key"  ' Display sign on message
loop:   Gosub getkey            ' Get a key from the keypad
        Lcdout $fe, 1, #key     ' Display ASCII key number
        Goto loop               ' Do it forever
'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
' Subroutine to get a key from keypad
getkey:
        Pause 50                ' Debounce
getkeyu:
        ' Wait for all keys up
        PORTD = 0                       ' All output pins low
       TRISD = $f0                     ' Bottom 4 pins out, top 4 pins in
        If ((PORTD >> 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
            PORTD = 0               ' All output pins low
            TRISD = (dcd col) ^ $ff ' Set one column pin to output
            row = PORTD >> 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
==============================================

OPTION_REG.7 = 0 ' Enable PORTb pullups
what is this option for? why does it need to be 'pullups'

but anyway, the program i modified, the keypad doen't work. could anyone let me know what else i need to modify inorder for it to work. I appreciat it!

p.s. i use PIC16F877

"knowledge is POWER"
 
Last edited by a moderator:
The reason the keypad does not work is because you changed the keypad port from port B to port D which does not have pull up resistors
If you want to use port D you will have to add your own 10K resistors to the 4 input pins of the D port to which the Keypad is connected.
Without the pullup resistors, when the port reads the keypad, unless the port bit is being pulled low or high by the key being pressed,
the other 3 port D inputs will float to a random level between 0 and 5 volts.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top