In an earlier discussion, we came up with the quick way of converting a nybble in "w" to a hex byte: NybbletoASCII ; Convert a Nybble in "w" to ASCII addlw 0x036 ; Add '0' + 6 to Value btfsc STATUS, DC ; If Digit Carry Set, then 'A' - 'F' addlw 7 ; Add Difference Between '9' and 'A' addlw 0-6 return ; Return the ASCII of Digit in "w" To convert a byte, this subroutine can be called from: BytetoASCII ; Convert a Byte to two ASCII Bytes using ; "NybbletoASCII" movwf Temp ; Save the Byte to Convert for Lower Nybble swapf Temp,w ; Move the High Nybble into "w" andlw 0x00F call NybbletoASCII movwf FirstByte ; Save the Byte Returned movf Temp,w andlw 0x007 ; Convert the Low Nybble call NybbletoASCII movwf SecondByte return Converting ASCII to a byte is an exercise left to the reader. myke >Clewer,Brian wrote: >> > move the byte to W >> > swap nibbles (swapf) >> > and with 0x0f >> > or with 0x30 >> > send that byte >> > >> > move the byte to W again >> > and with 0x0f >> > or with 0x30 >> > send that byte >> This is ok if you use the numbers 0 to 9 but it doesn't work if you >> need HEX values from A to F. >> Any more ideas? > >Yeah, you're right. Good observation. > >Okay, time to factor out the nibble to hex ASCII digit stuff into >it's own subroutine ... > >hex ; convert nibble to ascii hex > andlw 0x0f ; keep only the right bits > movwf tmp1 ; save it for later > sublw 0x09 ; subtract from nine > movf tmp1,w ; bring it back (leave carry alone) > btfsc status,c ; test result of subtraction > ; (maybe it's btfss, not sure!) > addlw 0x37 ; adjust to A-F > return >hex_0 > addlw 0x30 ; adjust to 0-9 > return > >byte > movwf tmp2 ; save the byte for later > swapf tmp2,w ; grab the high nibble back > call hex ; encode high nibble as hex ascii > call send ; send the byte in w to serial port > movf tmp2,w ; fetch the byte again > call hex ; encode low nibble > call send ; send the byte in w to serial port > return > >I've not tested that. >It's probably wrong somewhere. >I'm a beginner. >Did it work? ;-) > >-- >James Cameron (james.cameron@digital.com) >Digital Equipment Corporation (Australia) Pty. Ltd. A.C.N. 000 446 800 > > "In this life we all get an equal share of ice. The rich get it in the summer, the poor get it in the winter." - Bat Masterson's last words