Shop OBEX P1 Docs P2 Docs Learn Events
binary to bcd — Parallax Forums

binary to bcd

nick bernardnick bernard Posts: 329
edited 2005-08-11 21:52 in General Discussion
hia

i'm trying to write a little snippet to turn a 9 bit variable into 12 bit bcd variable split accross 3 bytes ( i'm displaying on a 3 digit 7 seg via a max 7219)

any help?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy

Comments

  • pjvpjv Posts: 1,903
    edited 2005-08-11 18:38
    Hi Nick;

    I pulled a callable section out of one of my projects but it is for 5 packed BCD digits in 3 bytes from a 16 bit source.

    Although the software does run in it's entirity, I have not test this snippet by itself. For only 9 bits, I believe all you need to do is change the first line from #16 to #9, and then also deal with unpacking the bcd bytes. The packed extra result byte can then also be discarded.

    Hope this works for you.

    Cheers,

    Peter (pjv)

    OOPS!!!!·Lost all the formatting, so here goes again!

    ;========================================================
    bcdconvert·········mov ·convertcount,#16··········;do 16 bits
    ························clr· · convertbcdlo·······························;lo result packed digits
    ························clr· ··convertbcdmid···························· ;mid result packed digits
    ······················· clr· ··convertbcdhi····························· ·;hi result packed digits

    bcdshiftboth······ mov ·fsr,#convertbinlo······················ ·;point to source lo byte
    ······················· clc
    ······················· rl····· ind··············································;shift lo source byte
    ······················· inc ·· fsr
    ······················· rl··· · ind············································· ;shift out of hi source byte
    ······················· inc·· ·fsr
    ······················· rl·· ···ind····························· ;shift into lo dest byte
    ······················· inc· · fsr
    ······················· rl···· ·ind····························· ;shift into next dest byte
    (discard)··········· inc ·· fsr
    (discard)··········· rl·······ind·····························;shift into hi dest byte
    bcdendchek······· decsz convertcount
    ······················· skip
    ······················· ret
    bcdadj·············· mov·· fsr,#convertbcdlo··········;point to dest lo byte
    ······················· call····bcddigitadj
    ······················· inc···· fsr
    ······················· call··· bcddigitadj
    ······················· inc··· ·fsr
    ······················· call·· ·bcddigitadj
    ······················· jmp····bcdshiftboth

    bcddigitadj·········mov····w,#3
    ······················ add···· w,ind
    ······················ mov··· convertbcdgtemp,w
    ······················ snb···· convertbcdgtemp.3
    ······················ mov··· ind,w
    ······················ mov··· w,#$30
    ······················ add·· ·w,ind
    ······················ mov··· convertbcdgtemp,w
    ······················ snb···· convertbcdgtemp.7
    ······················ mov···· ind,w
    ······················ ret


    Post Edited (pjv) : 8/11/2005 6:54:31 PM GMT
  • James NewtonJames Newton Posts: 329
    edited 2005-08-11 18:48
    http://www.sxlist.com/techref/ubicom/lib/math/radix/index_sx.htm
    has several 8 bit to 12 bit bcd (aka half packed or packed bcd) routines...
    why 9 bits? 8 bits is easy, but:

    2^8th is 256 which only uses about 1/3 of the capasity of a 3 digit display
    2^9th is 512 so that does fit in 3 digits...·gives about half the possible output values.
    2^10th is 1024 which is a better fit for 3...

    humm.. ok, there is a 10 bit to 4 digit(?) routine (why bother with the 25 values past 999?) at
    http://www.sxlist.com/techref/microchip/math/radix/b2bu-10b4d-eag.htm
    but it hasn't been converted to parallax / SASM notation yet... that requires a few seconds at:
    http://www.sxlist.com/cgi-bin/mpasm2sasm2.exe
    which then gives us:
    ;======= Bin2Dec10G.asm ============= Sep 04 =========
      ;       Test of 10b binary to 4 digit conversion as
      ;       based on division by powers of 10.
      ;
      ;       E. A., Grens
      ;
      ;       For most PICs - here for 16F628 with 4 MHz
      ;       internal oscillator.
      ;
      ;       Average processor cycles for execution of
      ;       subroutine over the input range 0x000 to 0x3FF
      ;       is 88.  No variables required except the
      ;       input blh, blo and the out BCD digits.
      ;       Subroutine length 38.
      ;======================================================
    
      ;-------Set Up RAM Variables---------------------------
    blo equ $20 ;lsbyte of 10b binary
    bhi equ $21 ;msb
    d4 equ $23 ;msdigit
    d3 equ $24
    d2 equ $25
    d1 equ $26 ;lsd
    
     ;-------Program Code--------------------------------
            org $00 ;Effective Reset Vector
     ;
            nop
            jmp Start ;Jump by interrupt
     ;
    Start
            mov W, #$07
            mov CMCON, W ;Digital I/O
            mov W, #$03 ;Test input
            mov bhi, W
            mov W, #$FF
            mov blo, W
            mov W, #$04
            call Bin2decg
    Hold
            jmp Hold ;Finished
     
    Bin2decg                      ;10b binaey to BCD
            clr d1
            clr d2
            clr d3
            clr d4
            clc
            rr bhi ;N = 4*Q + R
            rr blo ;blo = Q
            rr d1 ;d1 = R as temp, R25
            jmp B2d2 ;repeat
    B2d3
            add blo, W ;get remainder
                                      ;    after /100, C = 0
            rl d1 ;*4 and add R back in
            rl blo
            rl d1
            rl blo ;4*blo + R = N -
                                      ;    1000*d4 - 100*d3
            mov W, #10
    B2d4
            sub blo, W ;blo = N - 1000*d4 -
                                      ;     100*d3 - 10*d2
            sc
            jmp B2d5 ;blo10
            jmp B2d4
    B2d5
            add blo, W ;put last 10 back, C=0
            mov W, blo
            mov d1, W
            ret
            end
    
    

    Now, that obviously needs some polish, but it should save you some time. Note that this routine is NOT complete, it only figures out D1 by dividing blo/bhi by 10 and·saving the remander, but D2 is just the same thing over again with the value left in blo/bhi after D1 is extracted. So you can stop at D3 and have your 3 digit version.

    Please consider publishing what you come up with both here and at sxlist.com?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • nick bernardnick bernard Posts: 329
    edited 2005-08-11 19:08
    thanks guys, i made a sxb snippit, but i like your solutions much more.

    james, i'm having trouble with your program
    mov CMCON, W ;Digital I/O - what is cmcon, it doesnt compile
    jmp B2d2 ;repeat - there is no b2d2 label

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
  • James NewtonJames Newton Posts: 329
    edited 2005-08-11 21:52
    Ok, just to be clear, that is NOT my code. The author is specified in the comments...

    ...and I've been staring at it for about an hour and I'll be damned if I can see how that could possibly work. I've read the page it came from and it was supposed to be a possible improvement on another routine, which was for 12 bits to 4 digits, but I've been staring at that for another hour and I don't see how that could work either.

    It's either bloody brilliant or its been garbled somehow.

    The CMCON line is a hold over from the PIC, you can whack that. But the missing label has me confused, as does the original 12 bit code. It appears to be a variation on Paysons code at:
    http://www.sxlist.com/techref/ubicom/lib/math/radix/b2bu-16b5d_sx.htm for a 16bit to 5 digit conversion which DOES work and IS bloody brilliant (read: impossible to understand).

    If I had the time, I would convert that 12 bit program to SASM and just run it.... to see if it worked at all... Then I would try to understand HOW it worked and THEN... I would try to understand how the idea (which I mistakenly posted above) is supposed to improve on it.

    But I have to get back to work....

    ...the proof is left as an excercise for the student.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



Sign In or Register to comment.