;******************************************************************
; alternate bin2bcd 33 words, 737 cycles (147.4-us @20 MHz) *
;******************************************************************
bin2bcd
movlw d'23' ;
movwf countin ; bit counter = 23
bcdadj
movlw 0x33 ; Mike Keitz's "bcd adj" method
addwf lowbyte,F ;
btfsc lowbyte,3 ; test if lo result > 7
andlw 0xF0 ; lo result > 7 so take the 3 out
btfsc lowbyte,7 ; test if hi result > 7
andlw 0x0F ; hi result > 7 so ok
subwf lowbyte,F ; any results <= 7, subtract back
movlw 0x33 ;
addwf midbyte,F ;
btfsc midbyte,3 ; test if lo result > 7
andlw 0xF0 ; lo result > 7 so take the 3 out
btfsc midbyte,7 ; test if hi result > 7
andlw 0x0F ; hi result > 7 so ok
subwf midbyte,F ; any results <= 7, subtract back
movlw 0x33 ;
addwf highbyte,F ;
btfsc highbyte,3 ; test if lo result > 7
andlw 0xF0 ; lo result > 7 so take the 3 out
btfsc highbyte,7 ; test if hi result > 7
andlw 0x0F ; hi result > 7 so ok
subwf highbyte,F ; any results <= 7, subtract back
rlf lowbyte,F ; packed bcd digits 0 & 1
rlf midbyte,F ; packed bcd digits 2 & 3
rlf highbyte,F ; packed bcd digits 4 & 5
bsf PORTB,4 ; <clk> = '1'
bcf PORTB,4 ; <clk> = '0'
bcf lowbyte,0 ;
btfsc PORTB,5 ; <dat> = '0'? yes, skip, else
bsf lowbyte,0 ;
decfsz countin,F ; last bit? yes, skip, else
goto bcdadj ; process another bit
;