Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



All about PIC microcontrollers

Within these pages, you can find many useful pieces of code mostly in assembly for the Microchip PIC micro controller family. A lot of people have spend many hours trying to put the bits and bytes together. If the code is NOT written by a member of the PCB Heaven community, then a link will be added above the code with the original website that this code was found.
Because the code is copied to our servers, you should know that:

  • The responsible web master of the website that the code is taken, has been informed and he has agreed to copy the code
  • All emails from the above contact have been kept as records but due to personal privacy cannot be shown in public.
  • The author of the code is always clearly indicated above the code. In some cases the author is unknown. If you happen to be the author of the code or you know the person who wrote it, please inform us by email and it will be added ASAP.

We would personally like to send the credits to all the people that managed to write some very interesting code and publish it, and special thanx to the people that originally hosted those code snippets and gave us the permission to copy them.


View code
24bit to BCD unpacked 8 digits (moultiple routines)
Author: Peter Hemsley - Steve Teal - Harry West - Michael McLoughlin
This code was originally located @ http://www.piclist.com


Follow this link to go back

This binary to decimal routine is neat and fast and 24-bit but easily modified to 16 or 32-bit. Execution time is constant and so can be used where timing is critical.

I got the idea from the way some processors execute a decimal adjust instruction in hardware, so did a bit of simple arithmetic and some lateral thinking. This version is my generic one, no real need for the subroutines unless they are called from elsewhere. In the 16-bit version I expanded the two inner loops, the resulting code is hardly any bigger, executes faster, uses only one loop counter and does not use the FSR. Great for the smaller PICs. +

BINDEC: CALL CLRDIG
MOVLW 24
MOVWF COUNTER1
GOTO SHIFT1
ADJBCD: MOVLW DIGIT1
MOVWF FSR
MOVLW 7
MOVWF COUNTER2
MOVLW 3
ADJLOOP: ADDWF INDF,F
BTFSS INDF,3
SUBWF INDF,F
INCF FSR,F
DECFSZ COUNTER2,F
GOTO ADJLOOP
SHIFT1: CALL SLCNT
SLDEC: MOVLW DIGIT1
MOVWF FSR
MOVLW 8
MOVWF COUNTER2
SLDLOOP:
RLF INDF,F
BTFSC INDF,4
BSF STATUS,C
BCF INDF,4
INCF FSR,F
DECFSZ COUNTER2,F
GOTO SLDLOOP
DECFSZ COUNTER1,F
GOTO ADJBCD
RETURN
SLCNT: RLF COUNT0,F
RLF COUNT1,F
RLF COUNT2,F
RETURN
CLRDIG: CLRF DIGIT1
CLRF DIGIT2
CLRF DIGIT3
CLRF DIGIT4
CLRF DIGIT5
CLRF DIGIT6
CLRF DIGIT7
CLRF DIGIT8
RETURN

;..........

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

BIN2DEC clrf DEC0 ; Clear decimal output buffer
clrf DEC1
clrf DEC2
clrf DEC3
clrf DEC4
clrf DEC5
clrf DEC6
clrf DEC7 ; NB. BINn and FSR are trashed after this routine
movlw 24 ; Initiate bit loop
movwf BIT_COUNTER
BITLOOP rlf BIN0, F ; Every iteration of this loop will copy the next
rlf BIN1, F ; bit of the bin value, starting with the MSB,
rlf BIN2, F ; to the carry flag
movlw DEC0
movwf FSR ; Initiate DECn pointer and counter
movlw 8
movwf DEC_COUNTER ; The following is executed 8 times per bit
DECLOOP rlf INDF, F ; Multiply DECn by two with carry, DECn * 2 + C
movlw -10 ; See note above - test for DECn > 9
addwf INDF, W ; W = DECn -10, if W = positive or zero, C = 1
btfsc STATUS, C ; DECn has overflowed (>>9) if carry is set
movwf INDF ; If carry is set DECn = DECn - 10
incf FSR, F ; Carry is CARRIED over to next multiply
decfsz DEC-COUNTER, F
goto DECLOOP ; Multiply next DECn
decfsz BIT-COUNTER, F
goto BITLOOP ; Do next bit
retlw 0 ; Could be RETURN on most PICs

;.............

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

BINDEC:
CALL CLRDIG ; Clear decimal digits
MOVLW 24 ; Decimal count
MOVWF BINCNT

BITLP:
RLF BIN0,F ; Shift binary left
RLF BIN1,F
RLF BIN2,F
MOVLW DIGIT0
MOVWF FSR
MOVLW 8 ; Count for the decimal digits
MOVWF DECCNT
MOVLW 6 ; The Working Register holds 6 throughout. For each bit the inner loop is repeated 8 times, with shift in of the next bit, "times 2" and DecAdj of each digit

ADJLP:
RLF INDF,F ; 2*digit, then shift in "next bit¢¢ for DIGIT0 or else the carry from the previous digit
ADDWF INDF,F ; Add 6, clears Cf and gives 1 in bit 4 if the
BTFSS INDF,4 ; addition is needed; zero if not, when
SUBWF INDF,F ; we subtract it again. Sets Cf
BSF STATUS,C ; Cf could be 0 or 1, so make it 1 as default
BTFSS INDF,4 ; Bit 4 is the carry to the next digit
BCF STATUS,C ; Reset Cf to zero if bit 4 is clear
BCF INDF,4 ; For BCD clear bit 4 in case it¢s one
INCF FSR,F ; Go to next digit, (Cf not affected)
DECFSZ DECCNT,F ; End of inner loop. check digit count and
GOTO ADJLP ; round again if it¢s not zero
DECFSZ BINCNT,F ; End of outer loop, one pass through digits,
GOTO BITLP ; check bit count and repeat if necessary.
RETURN


;..........

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

bin2dec:
clrf dec0
clrf dec1
cIrf dec2
cIrf dec3
cIrf dec4
cIrf dec5
cirf dec6
cIrf dec7
; movlw 18 ; deleted
clrf cycles ; new
movlw 08 ; new
movwf octcnt ; new

ctloop: ; new
incf cycles ; new
movlw 03 ; new
movwf bitcnt

bitloop:
rlf bin0
rlf bin1
rlf bin3
movlw dec0
movwf FSR
; movlw 08 ; deleted
movfw cycles ; new
movwf deccnt

decloop:
rlf INDF
movlw 0xF6
addwf INDF,0
btfsc STATUS,0
movwf INDF
incf FSR
decfsz deccnt
goto decloop
decfsz bitcnt
goto bitloop
decfsz octcnt ; new
goto octloop ; new
return

;............

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

;Binary to decimal
;Convert 24 bit binary (count0,1,2) to decimal digits 1 to 8
;Revised version. My thanks to all who contributed to the bin to dec
;discussion in EPE.

processor 16f84
include p16f84.inc
radix dec

;Ram equates
count0 equ 0x0C ;lsb
count1 equ 0x0D
count2 equ 0x0E ;msb
digit1 equ 0x11 ;lsd
digit2 equ 0x12
digit3 equ 0x13
digit4 equ 0x14
digit5 equ 0x15
digit6 equ 0x16
digit7 equ 0x17
digit8 equ 0x18 ;msd
bitcnt equ 0x19
digcnt equ 0x1A


org 0 ;test code for MPLAB simulator
movlw 0xFF
movwf count2
movlw 0xFF
movwf count1
movlw 0xFF
movwf count0
call bin2dec
brk1 return


bin2dec call clrdig
movlw 24 ;24 bits to do
movwf bitcnt

bitlp rlf count0 ;Shift msb into carry
rlf count1
rlf count2

movlw digit1
movwf fsr ;Pointer to digits
movlw 8 ;8 digits to do
movwf digcnt
adjlp rlf indf ;Shift digit 1 bit left
movlw -10
addwf indf,w ;Check and adjust for decimal overflow
skpnc
movwf indf

incf fsr ;Next digit
decfsz digcnt
goto adjlp
decfsz bitcnt ;Next bit
goto bitlp
return

clrdig: clrf digit1
clrf digit2
clrf digit3
clrf digit4
clrf digit5
clrf digit6
clrf digit7
clrf digit8
return

end

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!