Searching \ for '32 bits binary to BCD convert?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=bits+binary+bcd
Search entire site for: '32 bits binary to BCD convert?'.

Truncated match.
PICList Thread
'32 bits binary to BCD convert?'
1998\03\16@150008 by Malone

flavicon
face
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Does anyone know how to convert 32 bits binary to BCD format?

TNX in advance.

Manuel Castro Caamaqo.
Investimento e Investigacisn Industrial, S.L.
Av. Galicia 51-53
32300 Barco de Valdeorras (Orense)
Spain
TLF.   : (+34)(9) 88 327370
FAX    : (+34)(9) 88 327338
E-MAIL: spam_OUTmanuel.castroTakeThisOuTspammx3.redestb.es

-----BEGIN PGP SIGNATURE-----
Version: PGP 5.5.5
Comment: The Digital Co. Security System. 1998

iQA/AwUBNQ1dfegd1caa44t6EQKLtQCg5r2k+8jsocaz9vTu+Vsyhnc5CA4AoNU9
d7jYDgC1VoZdOxol6yNxV9Ky
=FaQV
-----END PGP SIGNATURE-----

1998\03\16@160344 by Bob Fehrenbach

picon face
Malone <.....maloneKILLspamspam@spam@MX2.REDESTB.ES> wrote:
>Does anyone know how to convert 32 bits binary to BCD format?

There probably faster routines but this should do the job.



   ;Binary - BCD   32 bits
   ;Input in buff_4|buff_3|buff_2|buff_1,
   ;
   ;Converts to *packed* bcd in temp_a, temp_b, temp_c, temp_d and temp_e
   ;with the MSD temp_a.

   ;Handles full range:  ff ff ff ff -> 4,294,967,296

   ;Also uses temp_f and count.

   ;2940 cycles including call and return.

bin2bcd:
  bcf     STATUS, C
  movlw   32
  movwf   count
  clrf    temp_a
  clrf    temp_b
  clrf    temp_c
  clrf    temp_d
  clrf    temp_e

bin2bcd_loop:
  rlf     buff_1, f
  rlf     buff_2, f
  rlf     buff_3, f
  rlf     buff_4, f
  rlf     temp_e, f
  rlf     temp_d, f
  rlf     temp_c, f
  rlf     temp_b, f
  rlf     temp_a, f

  decfsz  count, f
  goto    adj_dec
  goto    done

adj_dec:
  movlw   temp_e
  movwf   FSR
  call    adj_bcd

  movlw   temp_d
  movwf   FSR
  call    adj_bcd

  movlw   temp_c
  movwf   FSR
  call    adj_bcd

  movlw   temp_b
  movwf   FSR
  call    adj_bcd

  movlw   temp_a
  movwf   FSR
  call    adj_bcd

  goto    bin2bcd_loop

adj_bcd:
  movlw   h'3'
  addwf   INDF, w
  movwf   temp_f
  btfsc   temp_f, 3
  movwf   INDF
  movlw   h'30'
  addwf   INDF, w
  movwf   temp_f
  btfsc   temp_f, 7
  movwf   INDF
  return

done:
  return



--
Bob Fehrenbach    Wauwatosa, WI     bfehrenbspamKILLspamexecpc.com

1998\03\16@182836 by Mike Keitz

picon face
On Mon, 16 Mar 1998 14:18:12 -0600 Bob Fehrenbach <.....bfehrenbKILLspamspam.....EXECPC.COM>
writes:
>Malone <EraseMEmalonespam_OUTspamTakeThisOuTMX2.REDESTB.ES> wrote:
>>Does anyone know how to convert 32 bits binary to BCD format?
>
>There probably faster routines but this should do the job.

The method Bob presented, which I describe as BCD multiplication by 2 is
all-around the best for large (>16 bits) numbers.  A 16-bit version was
presented in a Microchip application note without much explanation.  It
is easy to expand to more bits/digits.

{Quote hidden}

I think this is the tired old adj_BCD from the original app note.  I've
rewritten it to this shorter equivalent which also doesn't require
temp_f.

       movlw   h'33'           ;Possible correction value.
       addwf   INDF,f          ;Add to low and high nybbles
       btfsc   INDF,3
       andlw   h'f0'           ;Low result >7 . OK (take the 3
out)
       btfsc   INDF,7
       andlw   h'0f'           ;Hi > 7 OK.
       subwf   INDF,f          ;any results <=7, subtract
back.

Another viable binary to decimal conversion is to repeatedly divide the
binary number by 10, using the remainders found as decimal digits (from
right to left order).  Though probably more time-consuming, it can save
some code space if the program already has a division routine.  Using a
HD44780-type LCD panel, it is unnecessary to save the BCD digits before
output as the LCD can be set up to write them right to left.  For serial
output, they would need to be saved then output in the proper left to
right order.

Here is a highly optimized (for program space) 24-bit -> 8 digit
conversion routine based on the BCD multiply by 2.  It uses an inner loop
for the adjbcd process and also inlines everything.  Note that by adding
a couple of instructions to setup the C bit to the MSB of bin before each
shift, bin could be left unchanged after the conversion rather than
erased.

; Converts a 24-bit unsigned binary number into 8 BCD digits.
; Input: Number in bin to bin+2.  Will be destroyed.
; Output : 8 chars in bcd to bcd+3
; RAM: bcd to bcd+3, ii, FSR
; Variable 'bcd' must occupy 0C to 0F (or other so FSR.4 goes to 1 when
done)
seroutbcd7
; Rewrite of b2bcd for less space; inlined it.
;b2bcd
       movlw   d'24'
       movwf   ii
       clrf    bcd             ;clear result to all 0.
       clrf    bcd+1
       clrf    bcd+2
       clrf    bcd+3
b2bcdl
       movlw   bcd             ;Point at first bcd
       movwf   FSR
; Copy of 'adjbcd'.  OK to adjust the first time before shifting.
b2bcdil
       movlw   h'33'
       addwf   INDF,f          ;Add to low and high nybbles
       btfsc   INDF,3
       andlw   h'f0'           ;Low result >7 . OK (take the 3
out)
       btfsc   INDF,7
       andlw   h'0f'           ;Hi > 7 OK.
       subwf   INDF,f          ;any results <=7, subtract
back.
       incf    FSR,f           ;Inc. pointer for next time
; bcd placed at known address, so bits in FSR could be used for loop
control
       btfss   FSR,4           ;When FSR reaches 0x10, done.
       goto    b2bcdil         ;If not done, do the inner
loop again.

       rlf     bin+0,f
       rlf     bin+1,f
       rlf     bin+2,f         ;Get another bit out of bin.
       rlf     bcd+0,f         ;Put bit into bcd.
       rlf     bcd+1,f
       rlf     bcd+2,f
       rlf     bcd+3,f

       decfsz  ii,f            ;Do more?
       goto    b2bcdl          ;Yes.
; (Conversion is done)

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

1998\03\17@092317 by Ron Kreymborg

flavicon
face
I think the rlf statements at the end should reverse the way the buffers
are left shifted. ie:

       rlf     bin+2,f
       rlf     bin+1,f
       rlf     bin+0,f
       rlf     bcd+3,f
       rlf     bcd+2,f
       rlf     bcd+1,f
       rlf     bcd+0,f

I have been looking at multi-byte to ascii converters, in particular the
one in App note AN526, so it was easy to implement Mike's clever mod. A
complete program to test both 32-bit unsigned binary to BCD and then to
ASCII using these suggestions is on my web page.

http://www.shm.monash.edu.au/~r.kreymborg/pic/micro.html

Ron

On Mon, 16 Mar 1998, Mike Keitz wrote:

{Quote hidden}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ron Kreymborg                   Computer Systems Manager
Monash University               CRC for Southern Hemisphere Meteorology
Wellington Road
Clayton, VIC 3168               Phone     : 061-3-9905-9671
Australia                       Fax       : 061-3-9905-9689
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1998\03\17@142836 by Mike Keitz

picon face
On Wed, 18 Mar 1998 01:08:31 +1000 Ron Kreymborg
<KILLspamronKILLspamspamVORTEX.SHM.MONASH.EDU.AU> writes:
>I think the rlf statements at the end should reverse the way the
>buffers
>are left shifted. ie:
>
>        rlf     bin+2,f
>        rlf     bin+1,f
>        rlf     bin+0,f
>        rlf     bcd+3,f
>        rlf     bcd+2,f
>        rlf     bcd+1,f
>        rlf     bcd+0,f
>

The only consequence of this change is to put the bytes in opposite
order, i.e. MSB lowest.  In any case, the bit that comes out of the MSB
of bin needs to go into the LSB of bcd.  I try to consistently work
everything in MSB highest (little-endian) format, so my original routine
would output bcd+3 first to put it on the left end of the displayed
number.  It is strictly a matter of programmer preference, but curiously
supplies material for many flame-wars.

With this modified routine, output bcd+0 first.  In either case, the high
nibble (bits 4-7) of the bcd numbers is the leftmost (MS) digit of the
byte.

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

1998\03\17@202952 by rank A. Vorstenbosch

flavicon
picon face
Bob Fehrenbach wrote:
>
> Malone <RemoveMEmaloneTakeThisOuTspamMX2.REDESTB.ES> wrote:
> >Does anyone know how to convert 32 bits binary to BCD format?
>
> There probably faster routines but this should do the job.
>
>     ;Binary - BCD   32 bits
>     ;Input in buff_4|buff_3|buff_2|buff_1,
>     ;
>     ;Converts to *packed* bcd in temp_a, temp_b, temp_c, temp_d and temp_e
>     ;with the MSD temp_a.
>
>     ;Handles full range:  ff ff ff ff -> 4,294,967,296

Errm, I think that should have been 4294967295...

>     ;Also uses temp_f and count.
>
>     ;2940 cycles including call and return.

Here's mine:

; Binary to BCD conversion (32-bit).
;
; In:   value3,value2,value1,value0 : 32-bit integer
; Act:  Converts a 32 bit binary number to packed BCD
; Out:  bcd4,bcd3,bcd2,bcd1,bcd0 : BCD result (must be contiguous)
; Regs: tmp, Count
; Note: Uses one stack level internally.
;       Execution time is 2785 cycles (1948 cycles using in-line code
;       for B2B_Adjust).

bin2bcd_32      movlw   32
               str     Count
               clr     bcd4
               clr     bcd3
               clr     bcd2
               clr     bcd1
               clr     bcd0
               clc
               jp      B2B_Enter

B2B_Loop        movlw   bcd0
               str     fsr
               call    B2B_Adjust
               call    B2B_Adjust
               call    B2B_Adjust
               call    B2B_Adjust
               call    B2B_Adjust
B2B_Enter       rl      value0
               rl      value1
               rl      value2
               rl      value3
               rl      bcd0
               rl      bcd1
               rl      bcd2
               rl      bcd3
               rl      bcd4
               loop    Count,B2B_Loop
               retlw   0

B2B_Adjust      movlw   0x33
               addwf   ind,w
               btfsc   ind,3
               andlw   0xf0
               btfsc   ind,7
               andlw   0x0f
               subwf   ind,f

               if      bcd1>bcd0
               inc     fsr                         ; bcd0 at lowest address
               else
               dec     fsr                         ; bcd0 at highest address
               endif
               retlw   0



------------------------------------------------------------------------
Frank A. Vorstenbosch    <UCE_ACCEPT="NONE">    Mobile:  +44-976-430 569
Wimbledon, London SW19                          Home:   +44-181-544 1865
spamBeGonefrankspamBeGonespamfalstaff.demon.co.uk                      Office: +44-181-636 3391

1998\03\17@231634 by Ron Kreymborg

flavicon
face
Hmmm, what was the <tmp> variable for?

Ron

On Tue, 17 Mar 1998, Frank A. Vorstenbosch wrote:

{Quote hidden}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ron Kreymborg                   Computer Systems Manager
Monash University               CRC for Southern Hemisphere Meteorology
Wellington Road
Clayton, VIC 3168               Phone     : 061-3-9905-9671
Australia                       Fax       : 061-3-9905-9689
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1998\03\17@235847 by Bob Fehrenbach

picon face
"Frank A. Vorstenbosch" <frankEraseMEspam.....FALSTAFF.DEMON.CO.UK> wrote:
>Bob Fehrenbach wrote:
>>     ;Handles full range:  ff ff ff ff -> 4,294,967,296
>
>Errm, I think that should have been 4294967295...

   You have too much free time Frank.  Of course you are right.


--
Bob Fehrenbach    Wauwatosa, WI     EraseMEbfehrenbspamexecpc.com

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...