DS1307 Problems help please !!! :-)


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default 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
    Last edited by sayzer; - 19th September 2018 at 15:11.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default 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

  4. #4
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Thumbs up 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

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default 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

Similar Threads

  1. 7 segment clock PIC16f72 with DS1307 problems!!!!!!!
    By Antorbd04 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th December 2013, 16:07
  2. using DS1307
    By Scampy in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 2nd December 2013, 11:01
  3. ds1307
    By jonas2 in forum Code Examples
    Replies: 0
    Last Post: - 2nd November 2009, 10:50
  4. DS1307 help
    By Orlando in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th March 2005, 16:17
  5. Ds1307
    By miniman in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd February 2005, 09:29

Members who have read this thread : 1

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