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. www.shm.monash.edu.au/~r.kreymborg/pic/micro.html Ron On Mon, 16 Mar 1998, Mike Keitz wrote: > On Mon, 16 Mar 1998 14:18:12 -0600 Bob Fehrenbach > writes: > >Malone 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. > > >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 > > 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] > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~