PDA

View Full Version : Decimal to hex clock conversion



Steve2381
05-07-2006, 11:46 PM
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

·

Jon Williams
05-08-2006, 12:06 AM
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

Steve2381
05-08-2006, 12:21 AM
Many thanks!

Steve

Steve2381
05-08-2006, 05:38 AM
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 Savage
05-08-2006, 05:40 AM
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 (mailto:csavage@parallax.com)

Jon Williams
05-08-2006, 05:54 AM
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

Newzed
05-08-2006, 05:57 AM
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

·

Steve2381
05-08-2006, 05:57 AM
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

Steve2381
05-08-2006, 05:59 AM
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.

Newzed
05-08-2006, 06:09 AM
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

·