LIST R = DEC MOVLW '0' BTFSC CHAR1,6 MOVLW 'A'-10 SUBWF CHAR1 MOVLW '0' BTFSC CHAR2,6 MOVLW 'A'-10 SUBWF CHAR2 SWAPF CHAR1,W ADDWF CHAR1,W ADDWF CHAR1,W ADDWF CHAR2,W -Andy === Andrew Warren - fastfwd@ix.netcom.com === Fast Forward Engineering - San Diego, California === http://www.geocities.com/SiliconValley/2499
Questions:
Is the last part of ascii hex to binary right? Seems like all those adds of char1 should not be there. Here's how I modified it (and commented) it:HexToBin ; Convert hex in asciibuf[0] and asciibuf[1] to binary in w ; Based on http://www.piclist.com/techref/microchip/math/radix/a2b-2h8b-aw.htm ; As an example, assume hex number is 0xAB with 'A' in asciibuf and 'B' in asciibuf+1. LIST R = DEC MOVLW '0' ; ASCII 0 or 0x30 BTFSC asciibuf,6 ; if this bit clear, asciibuf<0x40, so it's numeric (not alpha) MOVLW 'A'-10 ; It's alpha, use a bias of 0x37 SUBWF asciibuf ; w=f-w. A (0x41) becomes 0x0A, 0 (0x30) becomes 0. MOVLW '0' ; ASCII 0 or 0x30 BTFSC asciibuf+1,6 ; if this bit clear, asciibuf<0x40, so it's numeric (not alpha) MOVLW 'A'-10 ; It's alpha, use a bias of 0x37 SUBWF asciibuf+1 ; w=f-w. B (0x42) becomes 0x0B, 0 (0x30) becomes 0. SWAPF asciibuf,W ; Put low byte of asciibuf (nibble converted to binary) in high byte of w (0xA0) ADDWF asciibuf+1,W ; add in second nibble converted to binary. In example, w=0xAB returnHarold
harold@dovesystems.com
Code:
The above code works great IF you are assured the incoming ascii for A through F is upper case. If not, you can force it to upper case by adding a few lines. The modified code is below.
Harold
harold@dovesystems.com
HexToBin ; Convert hex in asciibuf[0] and asciibuf[1] to binary in w ; Based on http://www.piclist.com/techref/microchip/math/radix/a2b-2h8b-aw.htm ; As an example, assume hex number is 0xAB with 'A' in asciibuf and 'B' in asciibuf+1. LIST R = DEC btfsc asciibuf,6 ; Skip next if not alpha bcf asciibuf,5 ; Force character to upper case btfsc asciibuf+1,6 ; Skip next if not alpha bcf asciibuf+1,5 ; Force character to lower case MOVLW '0' ; ASCII 0 or 0x30 BTFSC asciibuf,6 ; if this bit clear, asciibuf<0x40, so it's numeric (not alpha) MOVLW 'A'-10 ; It's alpha, use a bias of 0x37 SUBWF asciibuf ; w=f-w. A (0x41) becomes 0x0A, 0 (0x30) becomes 0. MOVLW '0' ; ASCII 0 or 0x30 BTFSC asciibuf+1,6 ; if this bit clear, asciibuf<0x40, so it's numeric (not alpha) MOVLW 'A'-10 ; It's alpha, use a bias of 0x37 SUBWF asciibuf+1 ; w=f-w. B (0x42) becomes 0x0B, 0 (0x30) becomes 0. SWAPF asciibuf,W ; Put low byte of asciibuf (nibble converted to binary) in high byte of w (0xA0) ADDWF asciibuf+1,W ; add in second nibble converted to binary. In example, w=0xAB return