# Thread: Converting Bytes from hex to Dec

1. ## Converting Bytes from hex to Dec

Hi all

I'm using a DS1307 as a RTC and seem to have reading and writing it sussed.
BUT...

The display device I'm using requires that each charater is written on it's own so to display 59 (seconds) I have to write 5 then move the pointer one space then write 9. (I actually have to write \$5 and \$9 to the display)

The DS1307 returns the seconds in hex so 59secs = 3B.
is there a simple way to convert hex to dec so i can read one char at a time?

I'm sure I'm missing something very obvious here but I have been away from pics for almost a year now and can not seem to find the answer on the archieve.

Rob

2. >> "The DS1307 returns the seconds in hex so 59secs = 3B"

No it doesn't! The DS1307 gives you Binary Coded Decimal (BCD), so 59 seconds is %01011001 = \$59

If RTCSeconds is \$59 then...

UnitSeconds=RTCSeconds & \$0F ' will get you your lower 4 bits = \$09
TensSeconds=RTCSeconds >> 4 ' will get you your upper four bits = \$05

3. Mel

That works perfectly!
I understand the basis of BCD but need to do somemore reading.
I assume I just reverse the formula to convert the other way? Or is that to simplistic

Thanks as always for your invaluable help.

ROb

4. >> "I assume I just reverse the formula to convert the other way? "

Basically Yes...

If you want to Save 27 minutes into the Minutes Register, then you save \$27. The 2 is the twenty out of the twenty minutes and the "7" is the seven.

Remember, that when reading or writing you should never have an 'alphabetic' character... there are exceptions, some of the registers have the 7th bit as a special bit... just strip it out if it's not needed. The seconds register especially.... if the 7th bit is set to '1' then the clock isn't running - which is a way you can tell if the DS1307 is initialised or not.

5. Melaine

I have now got the RTC working fine, thanks for the pointers.

One last question to anyone who reads this I'm setting the clock from a GPS which provides GMT.
I want to configure the GPS to configure the RTC for different time zones which I should be able to do from the Lat & Lon from the GPS. But before I go and write a routine has anybody done this before?
The biggest problem will be adding say 1hour when the clock is at 23hr as I then have to increment the days and possibly the months and possibly the years.

Rob

6. There was a thread which adquately handles all this for you. Do a search on the word 'Julian' to find it. This was a significant Thread which is useful in so many circumstances which everyone has missed.

Convert Day, Month and Year to Linear Days. Add one if required, and convert back. Update your RTC with the resultant Day, Month and Year.

7. Rob,

all you need is here

8. Summer or daylight-saving time.The amount of the time shift varies, but one hour is the most common.

The standard time zones are centered on meridians spaced 15 degrees apart, resulting in 24 separate time zones. Standard time zone boundaries, which in theory, are on longitudes 7.5 degrees east and west of a standard time zone meridian, in practice meander to accommodate local centers of population and political borders. The stretching of these boundaries can allow a region to use a zone time far different from the zone time that corresponds to its longitude. Much of Spain, for example, which is west of Greenwich, uses the zone time whose meridian is centered 15 degrees east of Greenwich. Lastly, not all areas make use of the standard time zones, instead their time is offset some fractional part of an hour from a standard zone time. Parts of Australia, for example, are offset one half hour from standard zone time.

Luciano