Converting Bytes from hex to Dec
+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Posts
    44

    Default 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. #2
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    >> "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. #3
    Join Date
    Mar 2004
    Posts
    44

    Default

    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. #4
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    >> "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. #5
    Join Date
    Mar 2004
    Posts
    44

    Default

    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. #6
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    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. #7
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762

    Default

    Rob,

    all you need is here
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  8. #8
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695

    Default

    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

Similar Threads

  1. negative HEX to bytes
    By cpayne in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th October 2008, 18:50
  2. PBPL 2.50 observations
    By BrianT in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 17th September 2007, 23:58
  3. Converting Dec word to Hex string
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th August 2007, 05:44
  4. Need help converting to ASCII Hex format
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th August 2007, 19:14
  5. Converting ASCII to HEX
    By BobP in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th September 2006, 10:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts