Matching time conditions


Closed Thread
Results 1 to 40 of 49

Hybrid View

  1. #1
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    ... And what about $00 - what's it there for? The answer is screaming at you from the pages of the PBP Manual description of the I2C Command, and the DS1307's Datasheet. The two of them go together hand-in-hand.
    Mel,

    Bear with me on this as I'm sure quite a few of us still have difficulties in getting our heads round bits and bytes and registers etc. But to try and answer your question and see if I've got some grips with the data sheet, here's my take on the above.

    $00 is the start of the register address (00H). This byte stores the seconds, bits 0-4 for the digits and bits 5-7 the 10's. The next byte (01H) is the same but for mins. 02H stores the hours, with bit 6 setting the device up for 12/24 hrs depending if set high or low. Bits 0-2 of 03H stores the days, 04H date, 05H month, 06H year (1st 4 bits for digits, last 4 bits decades).

    The only part I don't grasp is the $D0 part - The manual stated
    I2CREAD DataPin,ClockPin,Control,{Address,}[Var{,Var...}]{,Label}
    so in the code I have
    Code:
    I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
    which transpires as datapin, clockpin, then control. but the only control reference I can find in the data sheet is for SQW (square wave out), and I'm still at a loss as to where you arrive at the $D0

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post

    The only part I don't grasp is the $D0 part -
    OK now got it...

    from the datasheet The address byte contains
    the 7 bit DS1307 address, which is 1101000, followed by the *direction bit (R/W ) which, for a write,
    is a 0.
    I used a binary to decimal / hex convertor.... 11010000 is D0, therefore the $D0 is the address which the PIC looks for to locate the DS1307 on the I2C buss and sets the DS1307 to either receive data or to send it ?

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


    Did you find this post helpful? Yes | No

    Default

    You've nailed it Malcolm.

    I know I was a bit hard on you, but now you see how simple it all is with that Datasheet and how near impossible it is to understand without.

  4. #4
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks...

    Although I now have an understanding of how the DS1307 is accessed and what the registers are... I'm still a long way from understanding a 300 + page data sheet on one of the more complex PICs !, and I must confess I'm still at a loss on how to use the 1307 to match a value in the code an in turn set a value of a variable.

    I assume that each byte for each register (01H, 02H etc) is converted from binary into BCD (which I gather is 4bit type code format of binary)... or does pbp simply read each byte at each register.

    I guess the simple answer is to wait until I receive the new chip with the alarm function and use that ?

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


    Did you find this post helpful? Yes | No

    Default

    The I2CRead command will simply grab the content of each Register.

    With the DS1307, you find that bit 7 in some of the registers is used for odd things. So, stripping that out, the first four bits (0-3) is the units digit (in binary), and bits 4-6 is the tens digit (in Binary). Which is why they fiendishly called it Binary Coded Decimal.

    You've noticed it's in BCD. The first thing is to convert it to something that is useable. So I end up with a single variable that is HOURS, another variable for MINUTES etc. It's easier to have a single byte whose content varies from 0 (zero) to 59 representing MINUTES, because then you can perform math on it directly.

    If your Alarm time is stored in the PIC (say your morning alarm at 07 Hours and 15 Minutes) it's a simple matter to compare if those two variables (say ALARMHour and ALARMMinute) match what you've read from the RTC (after converting from BCD). I tend to ignore seconds because unless you're polling better than once a second (or have a hardware interrupt on receipt of the SQUARE wave the RTC can output), or if your code is sloppy, you can easily miss one second.

    If you're really clever, you can convert the Date and Time to JULIAN Date & Time, which gives you a single variable which holds a linear combination of Year, Month, Day, Hour, Minute and Second. Now this is great, because then you can simply add 3600 to advance an hour etc etc. There's another thread somewhere on this forum where I did just that with another DS series chip (albeit some years ago). This is the method I choose to use for things like Alarms with a DATE attached (eg Central Heating Timers), whereas Alarms with just TIME (such as your bedside radio's) usually comparing Hours and Minutes suffices.

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Mel,

    Thanks for that explanation. I've been watching the US Masters on TV so it's just coming up to midnight here in the UK, so will look at your suggestion tomorrow after work.

    If your Alarm time is stored in the PIC (say your morning alarm at 07 Hours and 15 Minutes) it's a simple matter to compare if those two variables (say ALARMHour and ALARMMinute) match what you've read from the RTC (after converting from BCD).
    Which is basically what I'm after. I intend to have an ALARMHour1 and ALARMHour2 to set an on time and off time to monitor.

    Thanks once again for the push to try and understand these data sheets. It's somewhat clearer now

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default Strugling

    Help !

    I'm struggling with the conversion bit, and would appreciate some assistance.

    To keep things simple I'm going to restrict the alarm values to just hours, so I have one Alarm variable called AlarmH1 which I'm trying to match to the RTCHour.

    The I2C statement is thus
    Code:
    	I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
    I'll now post the part of the code I'm using for testing in it's current state (sorry it's somewhat messy)
    Code:
    	LCDOut $FE,$80
    	If RTCHour.6=1 then
    			' Work-Out 12 or 24 hour Display for Hours
    		CounterA=(RTCHour>>4)&$01
    		else
    		CounterA=(RTCHour>>4)&$03
    		endif
    	CounterA=CounterA*10+(RTCHour&$0F)
    	If RTCHour.6=1 then
    			' Display Hours appropriately for 12 or 24 hour Mode 
    		LCDOut #CounterA
    		else
    		LCDOut #CounterA Dig 1,#CounterA Dig 0
    		endif
    		
    	LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F,":"
    	LCDOut #(RTCSec>>4)&$0F,#RTCSec&$0F," "
    	LCDOut $FE, $D4,"value = ",  #RTCHour;dec CounterA ;Dig 1,#CounterA Dig 0
    
        alarmhour = 25
        if alarmhour = rtchour then
         alarmled=1
        endif
    The LCD is used to display the time and date, with line 4 used to "debug" what's happening with whatever I choose to display, eg as you can see dig1 and 0 og CounterA, ascii value of RTCHour, even tried DEC RTCHour etc. In the above example the LCD gives an ascii value of 25 for RTCHour hence the value for alarmhour. I've also tried using something like if DEC alarmhour = DEC RTChour but that still fails...

    Can someone (Mel ?) point me in the right direction on how I convert the value stored in the RTCHour register into some usable format (decimal say ) that I can use. I'm hoping to use a repeat of the set up routine to allow me to set the on hour and off hour, but that will come later. For now I just need to find the best way to match the value....

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