>-----Original Message----- >From: pic microcontroller discussion list >[mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Joel Middleton >Sent: Sunday, June 22, 2003 5:10 PM >To: PICLIST@MITVMA.MIT.EDU >Subject: [PIC]: Byte to Digits > > >Can anyone point me to some sample code (assembly) >where I can convert a byte into individual digits? >i.e. 128 to 1, 2 and 8. I am looking at piclist.com >but I can't seem to find what I am looking for. >Thanks Claiming the prize for least efficient method.. This works for me on an 18F. Rather crude, but easy to understand. Lyle ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ; word2asc ; converts 16 bit value in lcdHI:lcdLO to five ascii digits in ; asctthou,ascthou,aschun,ascten,ascone ; alternate call to byte2asc will convert a byte value into the ; lowest three digits: aschun, ascten, ascone ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; word2asc BANKSEL asctthou MOVLW 0x27 ;establish ten thousand in lcdx MOVWF lcdxhi MOVLW 0x10 MOVWF lcdxlo CALL RepSub16 ;subtract as many 10,000s as it has MOVWF asctthou ;store top digit MOVLW 0x03 ;establish one thousand in lcdx MOVWF lcdxhi MOVLW 0xE8 MOVWF lcdxlo CALL RepSub16 ;subtract as many one thousands as we can MOVWF ascthou ;store second digit byte2asc BANKSEL asctthou CLRF lcdxhi ;write one hundred into lcdx MOVLW 0x64 MOVWF lcdxlo CALL RepSub16 ;subtract as many hundreds as we can MOVWF aschun MOVLW 0x0A ;write ten into lcdx MOVWF lcdxlo CALL RepSub16 ;subtract as many tens as we can MOVWF ascten MOVF lcdLO,W ;add remainder to last digit ADDLW '0' MOVWF ascone RETURN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;RepSub16 ;repeat subtraction until borrow ;Subtract 16 bit value in lcdxh,lcdxl from lcdHI,lcdLO as many times ;as possible, returning ASCII digit '0' to '9' in W as result ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; RepSub16 BANKSEL asctthou CLRF subcount nxsub INCF subcount MOVF lcdxlo,W ;subtract Source from Dest SUBWF lcdLO,F MOVF lcdxhi,W SUBWFB lcdHI,F BTFSC STATUS,C ;Did we carry? BRA nxsub ;not yet, do it again MOVF lcdxlo,W ;put the last one back in! ADDWF lcdLO,F MOVF lcdxhi,W ADDWFC lcdHI,F MOVF subcount,W ADDLW '0'-1 RETURN -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics