Roy J. Gromlich wrote... >I need to Read the 18Fxxx A/D and send the resulting >16-bit number to the UART for output, thus I need to >convert 16-bit Binary to ASCII. I have tried several >procedures fromn PIClist but none of them appear to >be complete and so far none of them will make it >through the PIC assembler (MASM). I am getting various >cryptic messages regarding illegal and/or missing op >codes. I can probably figure this out eventually, but >I have a deadline issue and would like to know if ANYONE >has a working & tested procedure for the 18Fxxx series >chips with sufficient documentation that it can be used >without a lot of hacking. The following PIC18F code will convert an unsigned 16-bit integer in a two-byte variable called Binreg, to unpacked 5-digit BCD in a 5-byte variable BCDreg (both these variables are stored with most-significant-byte/digit at the lowest address). The unpacked BCD then can be converted to ASCII simply by adding hexadecimal 0x30 to each digit. Sorry for the lack of comments. This code has been copied many times for use on many projects over the years, and the comments are long gone. It's based on what I believe is commonly called the "add-3 algorithm", I remember that much. And it works. UDATA Count RES 1 ; loop counter BCDreg RES 5 ; 5-digit unpacked BCD output register Binreg RES 2 ; 16-bit unsigned binary input register CODE GLOBAL UInt16toBCD5, BCDreg, Binreg UInt16toBCD5 movlw 16 movwf Count clrf BCDreg+0 clrf BCDreg+1 clrf BCDreg+2 clrf BCDreg+3 clrf BCDreg+4 loop movlw 5 subwf BCDreg+0, W movlw 3 btfsc STATUS, C addwf BCDreg+0, F movlw 5 subwf BCDreg+1, W movlw 3 btfsc STATUS, C addwf BCDreg+1, F movlw 5 subwf BCDreg+2, W movlw 3 btfsc STATUS, C addwf BCDreg+2, F movlw 5 subwf BCDreg+3, W movlw 3 btfsc STATUS, C addwf BCDreg+3, F movlw 5 subwf BCDreg+4, W movlw 3 btfsc STATUS, C addwf BCDreg+4, F bcf STATUS, C rlcf Binreg+1, F rlcf Binreg+0, F rlcf BCDreg+4, F bcf STATUS, C btfsc BCDreg+4, 4 bsf STATUS, C rlcf BCDreg+3, F bcf STATUS, C btfsc BCDreg+3, 4 bsf STATUS, C rlcf BCDreg+2, F bcf STATUS, C btfsc BCDreg+2, 4 bsf STATUS, C rlcf BCDreg+1, F bcf STATUS, C btfsc BCDreg+1, 4 bsf STATUS, C rlcf BCDreg+0, F movlw 0x0F andwf BCDreg+0, F andwf BCDreg+1, F andwf BCDreg+2, F andwf BCDreg+3, F andwf BCDreg+4, F decfsz Count, F goto loop return END Hope this helps a bit... Dave D. -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.