ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - KEY1934X.pbp

PICBASIC PRO program to display keypad button numbers on LAB-X1 LCD.
' Name        : KEY1934X.pbp
' Compiler    : PICBASIC PRO Compiler 2.6
' Assembler   : MPASM
' Target PIC  : 40-pin 16F877A, 18F452 or similar
' Hardware    : LAB-X1 Experimenter Board
' Oscillator  : 4MHz external crystal
' Keywords    : DCD, LCDOUT, NCD
' Description : PICBASIC PRO program to display keypad button numbers
' on LAB-X1 LCD.
'

' 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

   Gosub Init    ' Hardware initialization routine
   Lcdout $fe, 1, "Press any key"  ' Display sign on message

mainloop:
   Gosub getkey         ' Get a key from the keypad
   Lcdout $fe, 1, "You pressed #",#key  ' Display ASCII key number
   Goto mainloop        ' 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

   ' Change row and column to key number 1 - 16
gotkey: 
   key = (col * 4) + (ncd (row ^ $f))
   Return               ' Subroutine over

Init:
   ANSELA = 0    ' porta all digital
   ANSELB = 0    ' portb all digital
   ANSELD = 0    ' portd all digital
   ANSELE = 0    ' porte all digital
   
   OPTION_REG.7 = 0 ' Enable PORTB pullups
   TRISD = 0        ' PORTD all outputs
   
   Pause 100        ' Wait for LCD to start
   Return
   
   End

           

Download the program file.