Labels

[PIC]binary to BCD (3 digits)

; 8bit to BCD half-packed (3 digits)
; reference on PICList which is truely a good place for studing PIC assmbling
; course 3 of PIC MCU in YNU
;
; no CopyRight
; Mar,12 2009 edited by Seraph

include "p16f877a.inc"

hundreds equ 0x20
tens equ 0x21
ones equ 0x22


org 0
goto main
org 4
goto int_srv

int_srv:
main:
movlw 0xff
call bin2bcd
bin2bcd:

clrf tens
clrf hundreds
decf hundreds, f
decf tens, f
movwf ones

div10_tens
incf tens
movlw 0xa
subwf ones, f
btfsc STATUS, C
goto div10_tens
movlw 0xa
addwf ones, f

div10_hundreds
incf hundreds
movlw 0xa
subwf tens, f
btfsc STATUS, C
goto div10_hundreds
movlw 0xa
addwf tens, f
;transmit them through 'call put232' or 'call putLCD'
;
ret
end

1 comment:

  1. ; binary to bcd ROUTINES
    ; this is NOT the smallest converter but its the fastest...
    ;***************************************************************
    ; this table will convert 0 to 99 in binary to packed bcd
    ;***************************************************************
    ; binary to packed bcd 00-99 7 bits only jump table

    include "p16f877a.inc"

    dis_temp1 equ h'7e'
    dis_temp2 equ h'7f'

    org 0
    goto main

    main:
    movlw h'14'
    call bin2bcd
    goto $

    bin2pak:
    addwf PCL,f
    retlw 0
    retlw 1
    retlw 2
    retlw 3
    retlw 4
    retlw 5
    retlw 6
    retlw 7
    retlw 8
    retlw 9
    retlw h'10'
    retlw h'11'
    retlw h'12'
    retlw h'13'
    retlw h'14'
    retlw h'15'
    retlw h'16'
    retlw h'17'
    retlw h'18'
    retlw h'19'
    retlw h'20'
    retlw h'21'
    retlw h'22'
    retlw h'23'
    retlw h'24'
    retlw h'25'
    retlw h'26'
    retlw h'27'
    retlw h'28'
    retlw h'29'
    retlw h'30'
    retlw h'31'
    retlw h'32'
    retlw h'33'
    retlw h'34'
    retlw h'35'
    retlw h'36'
    retlw h'37'
    retlw h'38'
    retlw h'39'
    retlw h'40'
    retlw h'41'
    retlw h'42'
    retlw h'43'
    retlw h'44'
    retlw h'45'
    retlw h'46'
    retlw h'47'
    retlw h'48'
    retlw h'49'
    retlw h'50'
    retlw h'51'
    retlw h'52'
    retlw h'53'
    retlw h'54'
    retlw h'55'
    retlw h'56'
    retlw h'57'
    retlw h'58'
    retlw h'59'
    retlw h'60'
    retlw h'61'
    retlw h'62'
    retlw h'63'
    retlw h'64'
    retlw h'65'
    retlw h'66'
    retlw h'67'
    retlw h'68'
    retlw h'69'
    retlw h'70'
    retlw h'71'
    retlw h'72'
    retlw h'73'
    retlw h'74'
    retlw h'75'
    retlw h'76'
    retlw h'77'
    retlw h'78'
    retlw h'79'
    retlw h'80'
    retlw h'81'
    retlw h'82'
    retlw h'83'
    retlw h'84'
    retlw h'85'
    retlw h'86'
    retlw h'87'
    retlw h'88'
    retlw h'89'
    retlw h'90'
    retlw h'91'
    retlw h'92'
    retlw h'93'
    retlw h'94'
    retlw h'95'
    retlw h'96'
    retlw h'97'
    retlw h'98'
    retlw h'99'

    bin2bcd:
    andlw h'7f' ;7 bits only
    call bin2pak ;bin to 2-packed bin 99 max
    movwf dis_temp2
    movwf dis_temp1
    swapf dis_temp1
    movlw h'0f' ;fetch low 4 bit
    andwf dis_temp2
    andwf dis_temp1

    end

    ReplyDelete