PIC Microcontoller Radix Math Method

Borowski Hex Format

a.borowski@student.qut.edu.au shares this code:

http://alborowski.tk These chunks of code convert a binary number to 'bastard hex format' (NOTE TO PICLIST PERSON: If the word 'bastard' offends you, feel free to substitute borowski :-)

EG d'255' will become ff (lower case), and bhf'73' will become 115. This is useful for EPROM programmers and the like, where you wish to send binary data to the PIC and back, but don't want your data accidently triggering any of the PIC's commands. Simply encode your data in BHF, send it to the PIC, which will decode it and process it.

I hope this is useful to someone...

BHF2BIN ; In: BHF1,BHF2 Out: BIN
	;Working on BHF1:
	clrf BIN
	clrf W
	movf BHF1, 0 ; put BHF 1 into W register 
	sublw d'96' ; ASCII char just before 'a'
	btfss STATUS,0 ; check to see if we have a letter or a number 
	GOTO letBHF1
	GOTO numBHF1
letBHF1
	;Code here is executed if we have a letter for BHF1
	movlw d'87'
	subwf BHF1,0 ; get rid of the ASCII offset
	movwf BIN ; put the high nibble in BIN
GOTO DoBHF2
numBHF1
	;Code here is executed if we have a  number for BHF1
	movlw d'48'
	subwf BHF1,0 ; get rid of the ASCII offset
	movwf BIN ; put the high nibble in BIN
DoBHF2 ; this code takes care of the second BHf to bin nibble
	clrf W
	movf BHF2, 0 ; put BHF 2 into W register 
	sublw d'96' ; ASCII char just before 'a'
	btfss STATUS,0 ; check to see if we have a letter or a number 
	GOTO letBHF2
	GOTO numBHF2
letBHF2
	;Code here is executed if we have a letter for BHF2
	movlw d'87'
	subwf BHF2,0 ; get rid of the ASCII offset
	GOTO ShiftIn
numBHF2
	;Code here is executed if we have a  number for BHF2
	movlw d'48'
	subwf BHF2,0 ; get rid of the ASCII offset
ShiftIn
	bcf STATUS,0 ; get rid of any carry that could screw up the bitshift
	movwf Temp ; save out 2nd nibble in temp to prevent errors
	swapf Temp, 1	;shift nibble to high nibble
	bcf STATUS,0 ; get rid of any carry that could screw up the bitshift
	; All this tricky bit shifting merges the 2 nibbles gained from decoding BHF1 and BHF2
	; into a single binary byte
	rlf Temp,1
	rlf BIN,1
	rlf Temp,1
	rlf BIN,1
	rlf Temp,1
	rlf BIN,1
	rlf Temp,1
	rlf BIN,1
	movf BIN,0 ; put the result in W for debugging	

         RETURN



	
BIN2BHF
	; this command wil encode a byte into bastard hex format. In: BIN Out: BHF1,BHF2
	clrw
	clrf BHF1
	clrf BHF2
	bcf STATUS,0 ; get rid of any carry that could screw up the bitshift
	rrf BIN,1
	rrf BHF2,1	
	rrf BIN,1
	rrf BHF2,1
	rrf BIN,1
	rrf BHF2,1
	rrf BIN,1
	rrf BHF2,1
	swapf BHF2,1 ; fix up annoying bug	
	movf BIN, 0
	movwf BHF1 ; put BHF nibble in place
;Now, to enocde in ASCII
	movf BHF1, 0; put BHF1 into W for debugging
	movf BHF2,0 ; put BHF2 into W for debugging	
	movlw d'10' ; put 10 in W
	subwf BHF1, 0
	clrw ; W is cleared
	btfsc STATUS,0 ; test if we have a number or a letter - next command is skipped if we have a number
	addlw d'39'
	addlw d'48' ; subtract 10 for hex
	addwf BHF1,1
	movf BHF1,0 ; put BHF1 in W for debugging	
	;OK, BHF 1 is encoded
	bcf STATUS,0 ; clear any carry (as if that could make a difference)
	clrw ; Wipe W
	movf BHF2,0 ; put BHF2 into W for debugging
	movlw d'10' ; put 10 in W	
	subwf BHF2, 0
	clrw ; W is cleared
	btfsc STATUS,0 ; test if we have a number or a letter - next command is skipped if we have a number
	addlw d'39'
	addlw d'48' ; subtract 10 for hex
	addwf BHF2,1
	movf BHF2,0 ; put BHF2 in W for debugging
        RETURN