Converting Bytes from hex to Dec


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    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

  2. #2
    Join Date
    Mar 2004
    Posts
    44


    Did you find this post helpful? Yes | No

    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

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    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.

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 : 1

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