Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 and setting/viewing data - HEX2/DEC2 — Parallax Forums

DS1302 and setting/viewing data - HEX2/DEC2

xanatosxanatos Posts: 1,120
edited 2009-06-19 00:11 in General Discussion
If I set the DS1302 with variables in DEC format, and then view the output in DEC, I get ODD counts - the seconds count to 9, then jump 7 (oddly enough...) to 16, where it continues counting...· So I viewed the seconds as HEX2 and they count fine, recycling at 59 as normal.

The issue comes in with the minutes and hours.· If I set in hex, they display ODD numbers.· If I set in DEC, and display in DEC2, I get the same hex count, but if I view in HEX2, I get hex digits in the time (like 0B, 0C, etc).· So...· I am confused, a state I am not unfamiliar with.

I have attached the code I am using below.· I'm sure it's something VERY obvious to most, but it is eluding me at the moment.· The regular demo code for the DS1302 works fine, and what I've done is alter the code to do away with the debug menu input and replace it with simply (for now) a set of hard coded variables (I will be setting the clock remotely to ensure the remote system and the local system's clocks match).· But there seems to be something in the function of receiving the set data in debug mode as Hex.· I've tried setting the data as hev (mins = $0A, for example), but that isn't working.

Please see the code section labeled '
[noparse][[/noparse] VARIABLES THAT WILL BE SET FROM READING CLOCK ON WEB TERMINAL ]---·

In short, I am looking to get proper time display that doesn't have oddities like 12:85:34, or 0C:B3:28.....

Ideas?· Direction?

THANK YOU!!!

Dave

Comments

  • phil kennyphil kenny Posts: 233
    edited 2009-06-16 02:32
    If you look at the data sheet for the DS1302, you'll see that the registers
    containing seconds, minutes, etc are coded in BCD. That's why you are
    getting bad displays when you use DEC2 for the Debug statements.

    They only display properly if you use HEX2.

    For hours you have to mask off the AM-PM bit.

    
    hrs = hrs & %01111111 
    
     ' When reading the DS1302's clock data, BIT7 will be set when the RTC
      ' is in 12 Hour Mode.  We need to save this information, but we must
      ' then clear this bit so it doesn't interfere with the display.  Since
      ' the data is in BCD format, leaving BIT7 set would give us invalid
      ' value to display.  If the DS1302 is in 12 Hour Mode then we must
      ' also save the status of BIT5, which is the AM/PM bit, however, once
      ' again we must clear this Bit so our digits are valid for display.
      ' If the DS1302 is in 24 Hour Mode we must LEAVE BIT5 alone, since it
      ' is used when the hours are greater than 19.
    




    At the bottom of your Show_Time subroutine, change it to read:


    SEROUT LCD, Baud, [noparse][[/noparse]"     ", HEX2 month, "/", HEX2 date, "/", HEX2 year]
    
      hrs = hrs & %01111111    ' <--------- Need this to strip off the AM-PM bit
    
      SEROUT LCD, Baud, [noparse][[/noparse]"    ", HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs, "   "]
      DEBUG HEX2 month, "/", HEX2 date, "/", HEX2 year, CR
      DEBUG HEX2 hrs, ":",HEX2 mins, ":", HEX2 secs, "   ", "    ", HOME
      RETURN
    

    Post Edited (phil kenny) : 6/16/2009 6:00:56 AM GMT
    300 x 512 - 25K
  • xanatosxanatos Posts: 1,120
    edited 2009-06-17 02:44
    This is probably EXACTLY what I am missing - but I am having to address another issue related to a non-parallax product at the moment. I hope to soon implement your information, and I will respond with my results as soon as I do.

    Thanks very much for your input on this (no pun intended!)

    Dave
  • xanatosxanatos Posts: 1,120
    edited 2009-06-18 22:37
    Unfortunately, on further investigation, this is not the issue.· I am specifically using 24 hour format.· If I set the minutes to hex $3B (DEC 59), and display in hex, the minutes will increment (when the seconds hit 59) to 68 and it continues to count up from there.· I've been playing with all sorts of combinations... I KNOW it's something dumb I am doing, but it is absolutely baffling me as to WHAT I am doing wrong....

    If anyone wants to run the code I am messing with, I have attached it below ("SerialLCD+Clock.bs2") along with the original demo code for the DS1302 ("DS1302_Demo_LCD.bs2") for comparison.· The ONLY DIFFERENCE I can find is that the original demo is setting the time & date vars with a DEBUGIN HEX2 format, and I am setting the vars directly - hardcoding them in the "variables" section of the program, the reason being that I will actually be reading these variables from the variables written to the PINK Server.· The BS2 will then read these variables and set the clock, effectively synchronizing the "slave" clock with the "master" clock at the operator's console.· What the difference is between setting the variables with the DEBUGIN method, vs. simply hardcoding the variables, baffles me.· I have also tried hardcoding the variables in both HEX and DEC ($3B vs. 59, for example).· I am looking to find if there's a way to set as BCD... :-)

    I R so confused....

    Thanks for your help,

    Dave

    PS., although this is set up for an LCD, it works just fine without the LCD hooked up if you just use the debug terminal.· I get the same error in display there as with the LCD (so long as I remember to set the right set of output lines (DEBUG vs LCD)).
  • xanatosxanatos Posts: 1,120
    edited 2009-06-18 23:56
    SOLVED! The variables must be set as binary and - as you stated - in BCD Format. This code did the trick:

    '
    [noparse][[/noparse] VARIABLES THAT WILL BE SET FROM READING CLOCK ON WEB TERMINAL ]---

    '[noparse][[/noparse]secs, mins, hrs, date, month, day, year, 0]
    year = %00001001 ' Set Year
    month = %00000110 ' Set Month
    date = %00011000 ' Set Date
    day = 5 ' Set Day [noparse][[/noparse]Sun, Mon, Tue, Wed, Thu, Fri, Sat]
    hrs = %00010111 ' Set Hours (24 Hour Mode)
    mins = %01011001 ' Set Minutes
    secs = %01010000 ' Set seconds
    GOSUB Set_Time


    So all I should need to do is convert each digit of the incoming variable to its BCD equivalent and stick 'em together.· That sounds like a job for BIN8!!!!

    WHEW!

    Dave

    Post Edited (xanatos) : 6/19/2009 12:11:53 AM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-06-19 00:07
    I just used this routine to save time in the DS1302 RAM
    You could use this if you need to use DEC format

    I do not know if this will help you or not

    IF s = 59 THEN
    ·S = 00
    ·M = M + 1

    ·IF M = 59 THEN
    ·M = 00
    ·H = H + 1

    ·IF H = 23 AND M = 58 THEN
    ·H = 00
    ·D = D + 1

    ·ENDIF
    ·ENDIF
    ·ENDIF


    IF oldsecs <> secs THEN····· ' This Routine Compares the oldsecs to secs
    ·· oldsecs = secs··········· ' This Routine is so that Counter Update once a Seconds

    ···
    ·· S = S + 1················ ' This for the Timer Counter for the amount of time· has that has gone by


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 6/22/2009 9:12:41 PM GMT
  • xanatosxanatos Posts: 1,120
    edited 2009-06-19 00:11
    UPDATE:· I R DUMB!· The problem was my concept of inputting the numbers in hex.· I thought it meant the numbers had to BE in hex, so if it was 1400 hours (that's 2:00pm for you non 24 hour folks) - I was entering the hours as 0E - literally 14 in hex.

    The numbers have to be accepted as HEX2, but they are entered as if they were regular decimal.·

    So - for any and all of you who scratched their heads over my conundrum, I apologize for not figuring this out sooner and saving you the effort! :-)

    Humbled and embarrassed, yet happy I managed to get this figured out...

    Dave X



    Post Edited (xanatos) : 6/19/2009 2:51:00 AM GMT
Sign In or Register to comment.