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
Binary to BCD packed and ASCII, 32 bit to 10 digits
Author: Ron Kreymborg - Mike Keitz
This code was originally located @ http://www.piclist.com


Follow this link to go back

;******************************************************************
;
; Test program for 32-bit unsigned binary to BCD and BCD to ASCII.
;
;******************************************************************
title "32-bit binary to ascii"
list p=pic16f84,r=dec,n=80,x=off,st=off
include
errorlevel -302 ; no bank warnings
errorlevel -305 ; no default dest warnings

#define number 123456789

loadd macro
local m,n,p
p = number / 16777216
movlw p
movwf bin
m = (number - p * 16777216) / 65536
movlw m
movwf bin+1
n = (number - p * 16777216 - m * 65536) / 256
movlw n
movwf bin+2
movlw number - p * 16777216 - m * 65536 - n * 256
movwf bin+3
endm

CBLOCK 0x0c
bin:4 ; 32-bit binary number (unsigned)
bcd:10 ; 10 BC digits or 10 ascii chars
pti,pto ; pointers
ii
temp
cnt
ENDC

loadd ; load test value
call b2bcd ; convert to 32-bit binary to 10 bcd
call bcd2a ; convert 10 bcd to 10 ascii
goto $

;******************************************************************
; Convert the 10 binary coded digits (5 bytes) starting at
; into an ascii string also starting at . Original
; bcd digits are lost.

bcd2a movlw bcd+9
movwf pto ; destination pointer
movlw bcd+4
movwf pti ; source pointer
movlw 5 ; 5 bytes to process
movwf cnt

bcd2a1 movf pti,w ; get current input pointer
movwf fsr
decf pti,f ; prepare for next
movf indf,w ; get 2 bcds
movwf temp ; save for later
movf pto,w ; get current output pointer
movwf fsr
decf pto,f ; prepare for next
decf pto,f
movf temp,w ; get digits back
andlw 0x0f ; process lsd
addlw "0"
movwf indf ; to output
decf fsr,f
swapf temp,w ; process msd
andlw 0x0f
addlw "0"
movwf indf ; to output
decfsz cnt ; all digits?
goto bcd2a1
return ; yes


;******************************************************************
; Convert 32-bit binary number at into a bcd number
; at . Uses Mike Keitz's procedure for handling bcd
; adjust; Modified Microchip AN526 for 32-bits.

b2bcd movlw 32 ; 32-bits
movwf ii ; make cycle counter
clrf bcd ; clear result area
clrf bcd+1
clrf bcd+2
clrf bcd+3
clrf bcd+4

b2bcd2 movlw bcd ; make pointer
movwf fsr
movlw 5
movwf cnt

; Mike's routine:

b2bcd3 movlw 0x33
addwf indf,f ; add to both nybbles
btfsc indf,3 ; test if low result > 7
andlw 0xf0 ; low result >7 so take the 3 out
btfsc indf,7 ; test if high result > 7
andlw 0x0f ; high result > 7 so ok
subwf indf,f ; any results <= 7, subtract back
incf fsr,f ; point to next
decfsz cnt
goto b2bcd3

rlf bin+3,f ; get another bit
rlf bin+2,f
rlf bin+1,f
rlf bin+0,f
rlf bcd+4,f ; put it into bcd
rlf bcd+3,f
rlf bcd+2,f
rlf bcd+1,f
rlf bcd+0,f
decfsz ii,f ; all done?
goto b2bcd2 ; no, loop
return ; yes

end

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!