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

Decimal to hex clock conversion

Steve2381Steve2381 Posts: 94
edited 2006-05-07 23:09 in BASIC Stamp
Hi

I have a remote·keypad with a scanning·routine that allows you to enter 3 sets of values - temp_hours, temp_mins and temp_secs.
This is to 'set' the correct time on a clock that is being tracked using a DS1302 ic.

To send the digits to the DS1302, these values need to be in Hex format... its driving me mad this whole hex/decimal converting.... any ideas how to write the above values to EEPROM and then recover them·in hex format?

Write 1,temp_hours - stores it in decimal

Then...

Read 1,hours·· - how do I convert this to Hex?

Thanks

Steve

·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-05-07 17:06
    Luckily, you're using a BASIC Stamp and the conversion is easy.

    Decimal to BCD:

    · bcdVal = ((decVal DIG 1) << 4) + (decVal DIG 0)

    BCD to Decimal

    · decVal = (bcdVal.NIB1 * 10) + bcdVal.NIB0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Steve2381Steve2381 Posts: 94
    edited 2006-05-07 17:21
    Many thanks!

    Steve
  • Steve2381Steve2381 Posts: 94
    edited 2006-05-07 22:38
    Jon.

    Sorry to be a pain, my understanding of hex - decimal conversion isn't up to scratch yet!

    The formula you gave me works fine for the 2 digit variables, but I now need·to convert the 4 digit year from decimal to hex.

    I have had a go, but can't seem to suss that one out either.... perhaps I have been staring at this screen too long!

    Many thanks

    Steve
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-07 22:40
    Steve,

    ·· The year is only 2 digits in the clock, so that is all you need to convert.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-05-07 22:54
    Chris is right, that part doesn't use all four digits of the year, but here's what you'd do if it did (please use the manual to learn details of the operators used):

    · bcdYr = ((year DIG 3) << 12) | ((year DIG 2) << 8) | ((year DIG 1) << 4)·| (year DIG 0)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Steve2381Steve2381 Posts: 94
    edited 2006-05-07 22:57
    Thanks guys

    I was trying to convert the 4 digit dec variable to hex - just to see if I could.
    Even with the manual, too long on the laptop and being rubbish at maths doesn't help!

    thanks again

    Steve
  • NewzedNewzed Posts: 2,503
    edited 2006-05-07 22:57
    Steve, if you want it in HEX, why not just write HEX data to the EEPROM to begin with?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Steve2381Steve2381 Posts: 94
    edited 2006-05-07 22:59
    I still have to convert it from the decimal variable I start with (result of my keypad scan routine) - either before I store it as an EEPROM or after I have READ it.
  • NewzedNewzed Posts: 2,503
    edited 2006-05-07 23:09
    OK - I thought you were entering it from the keypad on the PC keboard.· Didn't realize you were using an external keypad.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
Sign In or Register to comment.