This is a multi-part message in MIME format. ------=_NextPart_000_00A0_01C5B882.4AF29BC0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by pch.mit.edu id j8DEkZq1010138 >Please consider sharing any updated version of those routines. It would >be nice to have versions optimized for the 18. > >--- >James. James and Luis, I use these, they are as fast as I could do it. 24 bit binary to 8 digits BCD 16 bit binary to 5 digits BCD (only for PIC18) Feel free to post them to piclist site if you want. Djula >> -----Original Message----- >> From: piclist-bounces@mit.edu >> [mailto:piclist-bounces@mit.edu] On Behalf Of Harold Hallikainen >> Sent: 2005 Sep 12, Mon 13:07 >> To: Microcontroller discussion list - Public. >> Subject: Re: [PIC]: Bin_To_Bcd to PIC18 >> >> See >> http://www.piclist.org/techref/microchip/math/radix/index.htm >> . Though I think most of the stuff there is for PIC16, it >> should work with minor modification on PIC18. Watch >> especially for status bit changes after an incf or decf. As I >> recall, the 16 and 18 handle this differently. >> >> Also, I had a look at your company website. My father >> manufactured a heart-lung machine about 50 years ago. A >> brochure is available at >> http://kauko.hallikainen.org/hi/brochures/1432.pdf . It looks >> like the pumps on your machine are very similar to those used >> on the old machine! >> >> Harold >> >> >> > Hi all, >> > >> > I=B4m looking for a rotine in asm to convert a 16bit number >> to BCD using >> > a PIC18Fxxxx. >> > Any help, >> > >> > Thanks in advanced >> > >> > Luis F. ------=_NextPart_000_00A0_01C5B882.4AF29BC0 Content-Type: application/octet-stream; name="BIN2BCD.asm" Content-Disposition: attachment; filename="BIN2BCD.asm" Content-Transfer-Encoding: quoted-printable ;************************************************************************= ********************** ;CONVERT 24-BIT BINARY TO EIGHT BCD DIGITS (150-230 cycles, 116 program = memory) ;Input bytes: AARGB5:AARGB4:AARGB3 ;Output digits: AARGB7:AARGB6:AARGB5:AARGB4:AARGB3:AARGB2:AARGB1:AARGB0 ;Trashes: PRODH, PRODL ;************************************************************************= ********************** Bin24_BCD clrf AARGB1 ;Divide 24 bit binary by 1000. = First, divide by 1024 clrf AARGB0 ;Clear final result = (AARGB5:AARGB4:AARGB3/1000) B24_Div1000 bcf STATUS,C ; rrcf AARGB5,w ;Get upper 14 bits (from 24) of = AARGB5:AARGB4:AARGB3 movwf AARGB7 ;and save to AARGB7:AARGB6 rrcf AARGB4,w ;Shift once right movwf AARGB6 ; clrf AARGB5 ;Lower 10 bits left in = AARGB4:AARGB3 movlw B'00000011' ; andwf AARGB4,f ; bcf STATUS,C ; rrcf AARGB7,f ;Shift once more to right, now = we are byte aligned rrcf AARGB6,w ;Upper 14 bits are in = AARGB7:WREG mullw d'24' ;Multiply by correction factor = 24 (1024-1000) addwf AARGB0,f ;Add division result to final = result movf AARGB7,w ; addwfc AARGB1,f ; movf PRODL,w ;Add correction factor = (result*24) to remainder addwf AARGB3,f ;Correction low already = calculated, just add movf PRODH,w ; addwfc AARGB4,f ; movf AARGB7,w ;Multiply correction high mullw d'24' ; movf PRODL,w ;Add correction to remainder addwf AARGB4,f ; movf PRODH,w ; addwfc AARGB5,f ; movlw B'11111000' ;Result >=3D 2048? andwf AARGB4,w ; iorwf AARGB5,w ; bnz B24_Div1000 ;Yes, divide by 1000 again clrf AARGB6 ;Clear memory for other = procedure rcall Do11Bit ;No, use other procedure to = parse 11 bits movff AARGB5,AARGB2 ;Move BCD x100 to AARGB2 movf AARGB1,w ;Prepare /1000 result high byte = for parsing movff AARGB4,AARGB1 ;Move BCD x10 to AARGB1 movwf AARGB4 ; movf AARGB0,w ;Prepare /1000 result low byte = for parsing movff AARGB3,AARGB0 ;Move BCD x1 to AARGB0 movwf AARGB3 ; movf AARGB6,w ;Add BCD thousands (it can be 0, = 1 or 2) addwf AARGB3,f ;to /1000 result movlw 0 ; addwfc AARGB4,f ;Done, parse the /1000 result = (16 bits) ;************************************************************************= ********************** ;CONVERT 16-BIT BINARY TO FIVE BCD DIGITS (65 cycles, 69 program memory) ;Input bytes: AARGB4:AARGB3 ;Output digits: AARGB7:AARGB6:AARGB5:AARGB4:AARGB3 ;Trashes: PRODH, PRODL ;************************************************************************= ********************** Bin16_BCD rrcf AARGB4,w ;Divide by 1000 rrcf WREG,f ;Start with divide by 1024 andlw B'00111111' ; movwf AARGB6 ;Result to AARGB6 mullw d'24' ;Correction factor =3D 24 = (1024-1000) movlw B'00000011' ;Calculate /1024 remainder andwf AARGB4,f ; movf PRODL,w ;Add correction factor addwf AARGB3,f ;to remainder movf PRODH,w ;(maximum possible value is = 2535) addwfc AARGB4,f ; Do11Bit movlw low d'1000' ; subwf AARGB3,f ;Subtract 1000 movlw high d'1000' ; subwfb AARGB4,f ; bnc Fix1000 ;Abort if negative incf AARGB6,f ;Still positive, increment = thousands movlw low d'1000' ; subwf AARGB3,f ;Subtract 1000 movlw high d'1000' ; subwfb AARGB4,f ; bnc Fix1000 ;Abort if negative incf AARGB6,f ;Still positive, increment = thousands bra End1000 ;Done Fix1000 movlw low d'1000' ;Negative, add 1000 addwf AARGB3,f ; movlw high d'1000' ; addwfc AARGB4,f ; End1000 rlcf AARGB3,w ;Divide remainder by 100 rlcf AARGB4,w ;Start with divide by 128 andlw B'00000111' ; movwf AARGB5 ;Result to AARGB5 mullw d'28' ;Correction factor =3D 28 = (128-100) bcf AARGB3,7 ;Calculate /128 remainder movf PRODL,w ;Add correction factor to = remainder addwf AARGB3,f ;(maximum possible value is 299) bnc Less256 ;No carry, ramainder < 256 movlw -d'200' ;Remainder >=3D256 addwf AARGB3,f ;Subtract 200 incf AARGB5,f ;Increment hundreds twice incf AARGB5,f ; bra End100 ;Done Less256 movlw d'100' ; subwf AARGB3,f ;Subtract 100 bnc Fix100 ;Abort if negative incf AARGB5,f ;Still positive, increment = hundreds subwf AARGB3,f ;Subtract 100 bnc Fix100 ;Abort if negative infsnz AARGB5,f ;Positive, incr. hundreds, skip = next Fix100 addwf AARGB3,f ;Negative, add 100 End100 movf AARGB6,w ;Now we have: TT:H:OO mullw d'205' ;AARGB6*1000 + AARGB5*100 + = AARGB3*1 movlw d'32' ;Split AARGB6 to two digits mulwf PRODH ;Multiply by 0.1 movf PRODH,w ;(205/256 * 32/256) movwf AARGB7 ;Result to AARGB7 mullw d'10' ;Multiply by 10 movf PRODL,w ;Subtract from original AARGB6 subwf AARGB6,f ;Remainder is AARGB6 movf AARGB3,w ;Split AARGB3 to two digits mullw d'205' ; movlw d'32' ;Multiply by 0.1 mulwf PRODH ;(205/256 * 32/256) movf PRODH,w ; movwf AARGB4 ;Result to AARGB4 mullw d'10' ;Multiply by 10 movf PRODL,w ;Subtract from original AARGB3 subwf AARGB3,f ;Remainder is AARGB3 return ;Done ------=_NextPart_000_00A0_01C5B882.4AF29BC0 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist ------=_NextPart_000_00A0_01C5B882.4AF29BC0--