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

best way to convert binary to bcd

ohVaNiLLaGoRiLLaohVaNiLLaGoRiLLa Posts: 33
edited 2013-04-25 19:48 in BASIC Stamp
What is the best way to convert binary to bcd with the bs2? I am receiving an 8 bit number and want to put out the bcd equivalent

There is some way to do it with shifting to the left but im not quite sure how that works..

the only other way i can think of is with a ton of if then statements like so
IF NumIn = 00000000 THEN a = 0000 AND b = 0000 AND c = 0000  '0
IF NumIn = 00000001 THEN a = 0000 AND b = 0000 AND c = 0001  '1
IF NumIn = 00000010 THEN a = 0000 AND b = 0000 AND c = 0010  '2

I do not feel like typing that out for 0 - 255 so if there's an easier way please let me know!
i just need to do 0-255 positive numbers

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-25 17:36
    With a Stamp it's easy. Use the DIG operator. Look this up in the BASIC Stamp Syntax and Reference Manual
  • ohVaNiLLaGoRiLLaohVaNiLLaGoRiLLa Posts: 33
    edited 2013-04-25 17:45
    i looked up dig but is this just for the debug window? the manual does not have to much on it.

    how would i get it to dig a variable and then use that variable with outh to output it on the desired pins?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-25 19:48
    bcd   var   nibble(3)
    
    for i = 0 to 2
    bcd(i) = variable dig i
    next i
    
    You should be able to figure out the rest which depends anyway on how you're driving the display and doing the bcd to 7-segment decoding.
Sign In or Register to comment.