DS1307 Problems help please !!! :-)
Hi Thank you for reading...
Using a 16f627
All I want to do is turn a light on at a time and turn it off a time later.... What am I doing wrong here.
Turns on OK but sees to add 4mins to the turn off time... Also randomly turns on !
Help please... Thank you for reading !
Code:
' Alias pins
SDA Var PORTB.1
SCL Var PORTB.2
Light var PORTB.6
' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte
'Set initial time
RTCYear = $18
RTCMonth = $09
RTCDate = $18
RTCDay = $03
RTCHour = $00
RTCMin = $00
RTCSec = 0
RTCCtrl = 0
Gosub settime ' Set the time
Goto mainloop ' Skip over subroutines
' Subroutine to write time to RTC
settime:I2CWrite SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
' Subroutine to read time from RTC
gettime: I2CRead SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
' Main program loop
mainloop: Gosub gettime ' Read the time from the RTC
IF (RTCHour=$00) and (RTCMin=$01) Then Light_On ' Light on
IF (RTCHour=$00) and (RTCMin=$02) Then Light_Off ' Light off
Pause 500 ' Do it about 2 times a min
Goto mainloop ' Do it forever and ever and ever
Light_On: high Light
return
Light_Off: Low light
return
End
Re: DS1307 Problems help please !!! :-)
Code:
mainloop:
Gosub gettime ' Read the time from the RTC
IF (RTCHour=$00) and (RTCMin=$01) Then
Gosub Light_On ' Light on
ENDIF
IF (RTCHour=$00) and (RTCMin=$02) Then
Gosub Light_Off ' Light off
ENDIF
Pause 500 ' Do it about 2 times a min
Goto mainloop ' Do it forever and ever and ever
Light_On: high Light
return
Light_Off: Low light
return
End
Re: DS1307 Problems help please !!! :-)
I tend to convert the time from the RTC chip into two variables such as timeH and timeM for hours and minutes
Code:
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
And then its a simple matter to match values with other variables to turn lights on or off
Code:
lightsetHR = 14 ' Set lights to come on at 2pm
lightsetMN = 00
lightoffHR = 22 ' Set lights to go off at 10pm
lightoffMN = 00
You could then used a simple logic routine like
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
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
I've used both options in various projects and both work within the limitations described above.
Hope that helps
Re: DS1307 Problems help please !!! :-)
Thank you Both
Both your replies have been implemented now I am running again..... Always have problems with the If/Then/Next Endif stuff :-)
Thank you again
BR
Andy
Re: DS1307 Problems help please !!! :-)
Glad it helped. My solutions may not be the most elegant, but it worked for me... and as all things in life there is always more than one solution :)