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 - BCD.pbp

PICBASIC PRO program to demonstrate conversion to and from BCD.
' Name        : BCD.pbp
' Compiler    : PICBASIC PRO Compiler 2.6
' Assembler   : PM or MPASM
' Target PIC  : 16F, 18F types
' Hardware    : Non specific
' Oscillator  : internal or external
' Keywords    : SEROUT2
' Description : PICBASIC PRO program to demonstrate conversion
' to and from BCD.
'

pinout  VAR PORTC.6  ' Define serial output pin
pinin   VAR PORTC.7  ' Define serial input pin
bcdin   VAR BYTE     ' BCD data in
bcdout  VAR BYTE     ' BCD data out
binhold VAR BYTE     ' Binary data
addamt  CON 12       ' Set value to add

   ADCON1 = 7        ' Set ports A and E to digital

mainloop:
   Serout2 pinout,396,["Enter 2-digit decimal number:",10,13] ' Send prompt to user
   Serin2 pinin,396,[HEX2 bcdin] ' Receive 2 characters as BCD data with the HEX2 modifier

   binhold = ((bcdin >> 4) * 10) + (bcdin & $0f) ' Convert bcdin to binary
   binhold = binhold + addamt    ' Add addamt to binary value
                
   If binhold>99 Then errmess    ' Make sure result is less than 100
   bcdout = ((binhold / 10) << 4) + (binhold // 10) ' Convert binhold to BCD
                
   ' Send data as BCD with the HEX2 modifier
   Serout2 pinout,396,[HEX2 bcdin, " + ", DEC addamt, " = ", HEX2 bcdout,10,13]
   Goto mainloop                 ' Do it forever

errmess:                         ' Send error message
   Serout2 pinout,396,[10,13,"ERROR: Result greater than 99",10,13,10,13]
   Goto mainloop                 ' Begin again
   
   End

           

Download the program file.