; 16-bit binary to BCD conversion ; pete griffiths 2007 ; http://picprojects.org.uk/projects/pictips.htm ; ; These file register variables will ; need to be defined elsewhere. ; binH ; binL ; bcdH ; bcdM ; bcdL ; counter ; temp ; ; binH, binL contain the binary value to ; convert. Conversion process destroys contents. ; Result is in bcdH, bcdM, bcdL on return. ; Call _bin2bcd to perform conversion. ; ; Executes in 454 instructions _bin2bcd movlw d'16' movwf counter clrf bcdL clrf bcdM clrf bcdH _repeat rlf binL,F rlf binH,F rlf bcdL,F rlf bcdM,F rlf bcdH,F decfsz counter,F goto _adjust return _adjust movlw d'14' subwf counter,W skpnc goto _repeat movfw bcdL addlw 0x33 movwf temp movfw bcdL btfsc temp,3 addlw 0x03 btfsc temp,7 addlw 0x30 movwf bcdL movfw bcdM addlw 0x33 movwf temp movfw bcdM btfsc temp,3 addlw 0x03 btfsc temp,7 addlw 0x30 movwf bcdM goto _repeat ; we only need to do the test and add +3 for ; the low and middle bcd variables since the ; largest binary value is 0xFFFF which is ; 65535 decimal so the high bcd byte variable ; will never be greater than 6. ; We also skip the tests for the first two ; shifts.