Shop OBEX P1 Docs P2 Docs Learn Events
Decimal to BCD conversion — Parallax Forums

Decimal to BCD conversion

AGCBAGCB Posts: 327
edited 2015-03-22 11:49 in Propeller 1
I have a program to reset the clock on an RTC in runtime. The RTC needs BCD (2 places each for minutes, seconds etc) to do a write_page w/ I2C , but the set program puts out straight decimal (2 places). I've looked at various format and string objects but can't see how to make the conversion. Would you please point me in the right direction to solve this.

Thanks
Aaron

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-03-22 11:42
    If you have two ASCII digits:
    bcd_digits := ((tens_digit & $0f) << 4) + (ones_digit & $0f)
    

    If you have a two-digit decimal value (i.e. < 100):
    bcd_digits := ((decimal_value / 10) << 4) + decimal_value // 10
    

    -Phil
  • AGCBAGCB Posts: 327
    edited 2015-03-22 11:49
    Thanks Phil! I think I can get that now. I knew it would be simple, it just eluded my thick skull.
Sign In or Register to comment.