; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.picnpoke.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; by FredMaher Madrid 10 Nov 02, Rewritten for SX by James Newton Feb 03. ; Using an extension of the >4 add 3 algorithm ; device sx18 P = pic16c54 #include <16c54.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start: BCDdigits equ d'5' BINdigits equ d'3' org 16 bcd Res BCDdigits bin Res BINdigits ;IFNDEF count count Res d'1' ; ENDIF ;IFNDEF temp temp Res d'1' ; ENDIF ; org 0 Start: ;----------------------------------------------------------------- ;Hex(Binary) to BCD routine ;----------------------------------------------------------------- Main: MOVLW BCDdigits MOVWF temp MOVLW bcd MOVWF fsr BCDclear: CLRF indf INCF fsr DECFSZ temp GOTO BCDclear_ Testvals: MOVLW 0x00FF MOVWF bin MOVLW 0x00FF MOVWF bin+1 MOVLW 0x0F MOVWF bin+2 CALL bin2bcd_ Convend: ; remove when in main prg GOTO Convend_ ; remove when in main prg ;----------------------------------------------------------- ; End main Bin Bcd ;------------------------------------------------------------ bin2bcd: MOVLW BINdigits*8 ; count max value MOVWF count Shifting: MOVF count BTFSC status,z RETLW 0h ; bin number is now converted 2 bcd DECF count ;---------------------------- ; Now left shift all registers BCF status,c MOVLW BINdigits MOVWF temp MOVLW bin MOVWF fsr BINrlLoop: RLF indf INCF fsr DECFSZ temp GOTO BINrlLoop_ MOVLW BCDdigits MOVWF temp MOVLW bcd MOVWF fsr BCDrlLoop: RLF indf INCF fsr DECFSZ temp GOTO BCDrlLoop_ ; next step check those which are >4 and add 3 MOVF count BTFSC status,z ; finish without testing >4 RETLW 0h Nibtests: ;Test BCD, high and low nibbles. If greater than 4 ( or 40) add 3 (or 30) MOVLW BCDdigits MOVWF temp MOVLW bcd MOVWF fsr BCDadjLoop: MOVLW 0x0B ;(16-5) ADDWF indf,w ;DC set if carry from bit 3 to 4 MOVLW 0x03 ; >4 add3 BTFSC status,dc ADDWF indf MOVLW 0x00B0 ; is high nibble more than 5? ADDWF indf,w MOVLW 0x30 ; add 3 to high nibble BTFSC status,c ADDWF indf INCF fsr DECFSZ temp GOTO BCDadjLoop_ GOTO Shifting_ end