• AVR Freaks

BCD to Binary PIC18 assembly

Author
HOODEY
Super Member
  • Total Posts : 580
  • Reward points : 0
  • Joined: 2005/02/08 06:06:31
  • Status: offline
2010/01/09 15:56:49 (permalink)
0

BCD to Binary PIC18 assembly

Guys Having some issues with my bcd to Bin routines since I migrated to pic18.
I found that the binary value was 1 more than it should be.
The routine also started to freak out when large numers were use.

Anyone has a working pic18 routine around?
#1

10 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/09 16:54:51 (permalink)
    0
    wanna post your code, anyway?

    GENOVA :D :D ! GODO
    #2
    HOODEY
    Super Member
    • Total Posts : 580
    • Reward points : 0
    • Joined: 2005/02/08 06:06:31
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 07:48:13 (permalink)
    0
    Problem solved. The statement to clear the status but before the rotations started
    was missing.





    ;Enter with a BCD number in HUND, TENS, and ONES (one digit
    ; per register). Each register (well... TENS, anyway) must
    ; hold a number in the range [0-9].
    ;
    ; Exits with ONES and HUND unchanged, TENS scrambled, and
    ; HI_BYTE:LO_BYTE = (HUND * 100) + (TENS * 10) + ONES.
    ;
    ; This routine works for all BCD inputs in the range
    ; [000-999].
    ;
    ; 29 cycles.

    BCD2BINX:


    bcf STATUS,C

    rlcf KEYTENS,W
    SWAPF KEYTENS
    rrcf KEYTENS
    ADDWF KEYTENS,W

    ADDWF KEYONES,W

    BTFSC KEYHUNDS,0
    ADDLW d'100'

    CLRF REFHIGH

    BTFSC KEYHUNDS,1
    ADDLW d'200'

    rlcf REFHIGH

    BTFSC KEYHUNDS,2
    ADDLW LOW (400)


    rlcf REFHIGH

    BTFSC KEYHUNDS,3
    ADDLW LOW (800)


    MOVWF REFLOW

    MOVLW d'1'

    ANDWF REFHIGH,W
    BTFSC REFHIGH,1
    ADDLW d'1'
    btfsc STATUS,C
    ADDLW d'1'


    BTFSC KEYHUNDS,2
    ADDLW HIGH (400)

    BTFSC KEYHUNDS,3
    ADDLW HIGH (800)


    MOVWF REFHIGH

    ; movlw d'1'
    ; subwf REFLOW

    ; btfss STATUS,C
    ; decf REFHIGH,F


    return




    post edited by HOODEY - 2010/01/10 07:49:34
    #3
    MBedder
    Circuit breaker
    • Total Posts : 6974
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 09:32:58 (permalink)
    0
    Hmm... is this really a PIC18 code? I doubt itgringringrin

    Look at this one (should be 13 words/16 cycles ):


            list    p=18f2220, r=dec

            cblock  0x20    ; File variables (absolute mode)

            units           ; BCD units byte
            tens            ; BCD tens byte
            hundreds        ; BCD hundreds byte

            bin_l           ; Binary result low byte
            bin_h           ; Binary result high byte

            endc

    ;-----------------------------------------------------------------------
    bcd2bin:
            movff   units,bin_l     ; BIN_L = units

            movf    tens,w
            mullw   10              ; PRODL = tens * 10

            movf    PRODL,w
            addwf   bin_l,f         ; BIN_L = (tens * 10) + units

            movf    hundreds,w
            mullw   100             ; PROD = hundreds * 100
            movff   PRODH,bin_h     ; BIN_H = MSB (hundreds * 100)

            movf    PRODL,w
            addwf   bin_l,f         ; BIN_L +=  LSB (hundreds * 100)

            movlw   0
            addwfc  bin_h,f         ; BIN_H += Carry from previous addition

            return
    ;-----------------------------------------------------------------------


    #4
    1and0
    Access is Denied
    • Total Posts : 12108
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 11:43:41 (permalink)
    0
    ORIGINAL: MBedder

    Look at this one (should be 13 words/16 cycles ):

    Actually that takes 15 words/16 cycles, because MOVFF is a double-word instruction.

    Here's a modification that uses two less words/cycles.

    radix dec
    bcd2bin:
    movf tens,w
    mullw 10 ; PRODL = tens * 10

    movf PRODL,w
    addwf units,w
    movwf bin_l ; BIN_L = (tens * 10) + units

    movf hundreds,w
    mullw 100 ; PROD = hundreds * 100

    movf PRODL,w
    addwf bin_l,f ; BIN = (hundreds * 100) + BIN_L
    movlw 0
    addwfc PRODH,w
    movwf bin_h ;
    return
    post edited by 1and0 - 2010/01/10 11:50:23
    #5
    MBedder
    Circuit breaker
    • Total Posts : 6974
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 12:34:16 (permalink)
    0
    And even shorter:grin


    ;---------------------------------------------------------------------------
    ; Converts unpacked BCD (hundreds, tens, units - one byte per digit)
    ; to a binary integer 0..999 in PRODH:PRODL. Trashes UNITS.
            radix    10
    bcd2bin:
            movf    tens,w
            mullw   10              ; PRODL = TENS * 10

            movf    PRODL,w
            addwf   units,f         ; UNITS += TENS * 10

            movf    hundreds,w
            mullw   100             ; PROD = HUNDREDS * 100

            movf    units,w
            addwf   PRODL,f         ; PRODL += LSB (HUNDREDS * 100) = binary LSB
            movlw   0
            addwfc  PRODH,f         ; PRODH = binary MSB

            return
    ;---------------------------------------------------------------------------


    #6
    1and0
    Access is Denied
    • Total Posts : 12108
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 12:57:37 (permalink)
    0
    Even shorter:grin

    ;
    ; input : hundreds=0..9, tens=0..9, units=0..9
    ; output: PRODH:PRODL = 0..999. No trashing.
    ;
    ; 10 words, 13 cycles (including call)
    ;
    radix dec
    bcd2bin:
    movf hundreds,w
    mullw 10 ; PROD = hundreds*10, 0..90

    movf tens,w
    addwf PRODL,w ; 0..99
    mullw 10 ; PROD = [(hundreds*10) + tens]*10, 0..990

    movf units,w ; PROD = [(hundreds*10) + tens]*10 + units
    addwf PRODL
    movlw 0
    addwfc PRODH
    return
    #7
    MBedder
    Circuit breaker
    • Total Posts : 6974
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 13:04:51 (permalink)
    0
    Great! It was my almost 1st (and hopefully lastgrin) program for PIC18 - not that bad thenSmileSmile
    #8
    1and0
    Access is Denied
    • Total Posts : 12108
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 13:35:35 (permalink)
    0
    Good collaboration! Smile
    #9
    Stefan Uhlemayr
    Super Member
    • Total Posts : 4292
    • Reward points : 0
    • Joined: 2005/05/12 12:25:46
    • Location: Germany
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 14:47:30 (permalink)
    0
    ORIGINAL: MBedder

    Great! It was my almost 1st (and hopefully lastgrin) program for PIC18 - not that bad thenSmileSmile
    Don't hope so - great work!Smile

    ORIGINAL: 1and0

    Good collaboration!
    Completely agree. Nice work, guy's. Added into the sw-gallery, of course. wink

    Greetings,
    Stefan
    #10
    HOODEY
    Super Member
    • Total Posts : 580
    • Reward points : 0
    • Joined: 2005/02/08 06:06:31
    • Status: offline
    RE: BCD to Binary PIC18 assembly 2010/01/10 16:36:34 (permalink)
    0
    What would I do without you guys?Smile


    Great work. Good use of the harware multiplier.


    #11
    Jump to:
    © 2021 APG vNext Commercial Version 4.5