I tend to convert the time from the RTC chip into two variables such as timeH and timeM for hours and minutes
And then its a simple matter to match values with other variables to turn lights on or offCode:I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl] ; read DS1307 chip timeH=(RTCHour>>4) 'convert the BCD format of the hours register and store in variable timeH timeH=(timeH &$03)*10 timeH=timeH+(RTCHour&$0F) timeM=(RTCMin>>4) timeM=(timeM &$07)*10 timeM=timeM+(RTCMin&$0F) 'convert the BCD format of the mins register and store in variable timeM
You could then used a simple logic routine likeCode:lightsetHR = 14 ' Set lights to come on at 2pm lightsetMN = 00 lightoffHR = 22 ' Set lights to go off at 10pm lightoffMN = 00
Code:If timeH >= lightsetHR and timeM>=lightsetMN then turn the lights on If timeH < lightsetHR and timeM < lightsetMN or timeH>=lightoffHR and timeM>=lightoffMN then turn lights off
Or an alternative is to use a counter that resets at midnight. This only works where you need to turn the light on and off between 00:01 and 23:59 in the same day. This basically counts the minutes in a 24 hour period since midnight
Code:I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour] ' read the RTC timeH=(RTCHour>>4) 'convert the BCD format of the hours register and store in variable timeH timeH=(timeH &$03)*10 timeH=timeH+(RTCHour&$0F) timeM=(RTCMin>>4) 'convert the BCD format of the mins register and store in variable timeM timeM=(timeM &$07)*10 timeM=timeM+(RTCMin&$0F) Counter1 = (TimeH *60)+ TimeM 'take hours and multiply it by 60 then add odd minutes to get the total minutes into counter1
Then use if/then statements to match the on / off times to the counter value to turn on / off the light
I've used both options in various projects and both work within the limitations described above.Code:If Counter1 => OnTime and counter1 < OffTime then ' check to see if the time in minutes since midnight matches the on time pin made high to turn light on ' insert the routine to make the pin high and turn the light on endif If Counter1 => OffTime or Counter1 < Ontime then ' check to see if the time in minutes since midnight matches the channel off time pin made low to turn lamp off ' insert the line to make the pin low and turn the light off
Hope that helps




Bookmarks