ON 20030101@11:01:36 AM at page: http://www.piclist.com/techref/microchip/math/radix/index.htm JMN-EFP-786 James Newton added 'archive reference http://www.piclist.org/techref/postbot.asp?by=time&id=piclist/2003/01/01/101131a PICList post "fixed point to ASCII on 16F87x?"' ON 20030116@7:56:53 AM at page: http://piclist.com/techref/microchip/math/radix/index.htm JMN-EFP-786 James Newton published post 37578.2670717593 paulswork@hotmail.com
here are a couple of BIN to ASCII converters I wrote, might be useful.|Delete 'P-' before: '' but after: 'a.borowski@student.qut.edu.au shares this code:;********************************************************************** ; Routine:ASCIItoBIN ; Input Registers:ASCII_MSW, ASCII_LSW ; Output Registers: BUFFER ; Internal Registers:None ; CALLS:None ; Function: Receives two ASCII bytes in ASCII_MSW and ASCII_LSW registers. ; These two bytes are converted into 1 byte binary, returned from this ; routine in register BUFFER ;********************************************************************** ASCIItoBIN MOVLW 65 ;convert ascii into MSW address SUBWF ASCII_MSW,W MOVLW 48 BTFSC StatusREG,C MOVLW 55 SUBWF ASCII_MSW,W ANDLW b'00001111' MOVWF BUFFER ;Save result of the upper nibble SWAPF BUFFER,1 MOVLW 65 ;convert ascii into LSW address SUBWF ASCII_LSW,W MOVLW 48 BTFSC StatusREG,C MOVLW 55 SUBWF ASCII_LSW,W ANDLW b'00001111' ;Get result of lower nibble IORWF BUFFER,1 ;and put final result in BUFFER for output retlw 0 ;********************************************************************** ; Routine:BINtoASCII ; Input Registers:BUFFER ; Output Registers: ASCII_MSW, ASCII_LSW ; Internal Registers:None ; CALLS:None ; Function: Receives a binary number in the input register BUFFER. This ; is then converted into two ASCII output bytes. ;********************************************************************** BINtoASCII CLRF ASCII_MSW ;Clear output registers CLRF ASCII_LSW SWAPF BUFFER,W ;Get upper nibble ANDLW h'0F' ;Mask off unwanted bits MOVWF ASCII_MSW ;Save MOVLW 10 ;For BCD lower nibble only (lower 4 bits) SUBWF ASCII_MSW,W MOVLW h'30' ;If numerical add ascii value (-10) BTFSC StatusREG,C MOVLW h'37' ;If alphabetical, then add a different ascii value (-10) ADDWF ASCII_MSW,1 ;save in output reg MOVF BUFFER,W ;get lower nibble of input byte ANDLW h'0F' ;mask off unwanted data MOVWF ASCII_LSW MOVLW 10 SUBWF ASCII_LSW,W MOVLW h'30' BTFSC StatusREG,C MOVLW h'37' ADDWF ASCII_LSW,1 ;and save in output reg retlw 0
/techref/microchip/math/radix/Alborowski.tk|Delete 'P-' before: '' but after: 'a.borowski@student.qut.edu.au shares this code:Here's a nice bit of code that turns a 8 bit binary number into it's hundreds, tens and ones in ASCII, suitable for displaying. Simply put your number in 'bin', call BIN2BCD, then use huns, tens and ones for display. BIN2BCD ;--------------------- ;in: BIN ;out huns. tens, ones ;uses ADD-3 algoerthm movlw 8 movwf count clrf huns clrf tens clrf ones BCDADD3 movlw 5 subwf huns, 0 btfsc STATUS, C CALL ADD3HUNS movlw 5 subwf tens, 0 btfsc STATUS, C CALL ADD3TENS movlw 5 subwf ones, 0 btfsc STATUS, C CALL ADD3ONES decf count, 1 bcf STATUS, C rlf BIN, 1 rlf ones, 1 btfsc ones,4 ; CALL CARRYONES rlf tens, 1 btfsc tens,4 ; CALL CARRYTENS rlf huns,1 bcf STATUS, C movf count, 0 btfss STATUS, Z GOTO BCDADD3 movf huns, 0 ; add ASCII Offset addlw h'30' movwf huns movf tens, 0 ; add ASCII Offset addlw h'30' movwf tens movf ones, 0 ; add ASCII Offset addlw h'30' movwf ones RETURN ADD3HUNS movlw 3 addwf huns,1 RETURN ADD3TENS movlw 3 addwf tens,1 RETURN ADD3ONES movlw 3 addwf ones,1 RETURN CARRYONES bcf ones, 4 bsf STATUS, C RETURN CARRYTENS bcf tens, 4 bsf STATUS, C RETURN
http://alborowski.tk|Delete 'P-' before: '' but after: '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