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

    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

  2. #2
    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....

  3. #3
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    I use these with a DS1302 to convert RTC minutes, hours, etc, to BCD minutes.

    Code:
    decminutes =(rtcmin>>4)*10+(rtcmin & $0F)  'convert BCD minutes into Decimal minutes       .
    Code:
    rtcmin=((decminutes DIG 1)<<4)+decminutes DIG 0 'convert Decimal back to BCD      .
    steve

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


    Did you find this post helpful? Yes | No

    Default

    DecimalHours=RTCHours>>4
    DecimalHours=(DecimalHours&$03)*10
    DecimalHours=DecimalHours+(RTCHours&$0F)

    RTCHours is read from the DS1307. It is in BCD format.

    The first line takes bits 4 thru 7 and loads them into Decimal Hours as bits 0 thru 3. These are the DECIMAL TENS DIGIT VALUE of the BCD HOURS (plus a couple of additional control bits).

    If you notice from the Datasheet, only bits 4 and 5 are the BCD TENS DIGIT (which we have moved to bits 0 and 1 of variable DecimalHours. Since we only need those two bits, we exclude all others with the AND '&' operator, ANDing it with $03. The result of that operation we simply multiply by ten. We now have our TENS DIGIT value extracted.

    The third line takes the lower four bits ONLY (by ANDing them with $0F) of the RTCHours variable (those four bits being the BCD UNITS VALUE) and we ADD them to our previously extracted TENS VALUE.

    We now have a BYTE variable called DecimalHours which contains a value between 0-23 (assuming you are running a 24 hour clock). You can Output it to your LCD with the command...

    LCDOUT #DecimalHours

    If you wanted your alarm at say 7am you can now simply say...

    AlarmHours=7
    If AlarmHours=DecimalHours then goto SoundKlaxxon

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


    Did you find this post helpful? Yes | No

    Default

    Steve,

    Thanks for that, which I guess is similar to some of the code posted above.

    This is the section of code based on your suggestion, and it too displays the value of AlarmH1 as 21 when the rtc is displaying 21 hrs, however the led on D7 fails to light (I've tested an the hardware works)

    Code:
    //    Alarmh1 =(rtchour>>4)*10+(rtchour & $0F)  
        alarmhour = 21
    	
    	LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F,":"
    	LCDOut #(RTCSec>>4)&$0F,#RTCSec&$0F," "
    	LCDOut $FE, $D4,"value = ",  dec alarmh1
    
        if alarmhour = alarmh1 then
         PORTD.7=1
        endif
    Welcome any comments... even if it's to point out the illogical of my logical coding.

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


    Did you find this post helpful? Yes | No

    Default

    Mel, thanks for the post, which happened whilst I was typing mine.

    It's not the displaying of the time I'm having a brain freeze on, it's working out how to do the comparison with another variable so that if the condition is true (ie the hour set ( between 0 and 23) matches the RTC hour) the LED can be made to light (or a value in another variable can be set)

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


    Did you find this post helpful? Yes | No

    Default

    Last three lines of my post Malcolm...

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    Last three lines of my post Malcolm...
    Mel,

    I've used your example but still can't make the LED go high

    Code:
    AlarmH1=(RTCHour>>4)
    AlarmH1=(AlarmH1&$03)*10
    AlarmH1=AlarmH1+(RTCHour&$0F)
        
    alarmhour = 21
    	
    	LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F,":"
    	LCDOut #(RTCSec>>4)&$0F,#RTCSec&$0F," "
    	LCDOut $FE, $D4,"value = ",  # AlarmH1
    
        if AlarmH1 = alarmhour then
         PORTD.7=1
        endif
    The LCD displays 21 as the value for AlarmH1

    what am I doing wrong ???

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