PDA

View Full Version : Hex minus Dec Problem



Sutton Murray
08-10-2008, 05:26 AM
Greetings

I am battling with minimizing two varibles from each other.
The first varible is loaded with a byte from the DS1302(hours) and the second varible byte·is loaded from the stamps EEProm.
When the two is minimized from each other the sum value is incorrect. ( IHours = Hours - IHours)
My thoughts are that·this is as a result that the information retrieved from the EEProm is·in Hex, where that from the DS1302 is in·Dec. Is my thinking·correct in this area?

How will one convert one to a other to do the sum calculation?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18

Mike Green
08-10-2008, 06:54 AM
The value in the DS1302 is not really "hex". It's what's called BCD (binary coded decimal). To convert a BCD byte to a binary result (or decimal ... it's all the same internally), do: result = bcdbyte.nib1 * 10 + bcdbyte.nib0
The ".nib0"/".nib1" notation is described in the Stamp Manual and it allows you to access the most significant 4 bits and the least significant 4 bits of a byte.

To go back, do: bcdbyte = (binary DIG 1) << 4 + (binary DIG 0)

Look in the Manual for the descriptions of the DIG and << operators.

Sutton Murray
08-10-2008, 06:25 PM
Mike

Battle to understand your script at first, but am making it up. I am now buzy going through Jon Williams "It's time to get real with the dallas 1302". Your line is more advanced(understand how this works), but i will get their.
Thanks for putting me on the right track.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For in much wisdom is much grief, and he who increases knowledge increases sorrow." ~Ecclesiastes 1:18