DS1302 RTC Loop issues


+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    225

    Default DS1302 RTC Loop issues

    Hi All

    Just a quick question look for help maybe ..... I have the following code with a DS1307

    Code:
    gettime:    I2CRead SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
                pause 5
                Return
    
    ' Main program loop 
            
    mainloop:   Gosub gettime        ' Read the time from the RTC
    
                'IF (RTCHour=$17) and (RTCMin=$15) Then  
                'gosub number ' flash  in Morse
                'endif
      
                'IF (RTCHour=$18) and (RTCMin=$15) Then  
                'gosub number ' flash  in Morse
                'endif 
             
                'IF (RTCHour=$19) and (RTCMin=$15) Then  
                'gosub number ' flash  in Morse
                'endif
                
                IF (RTCHour=$19) and (RTCMin=$30) Then 
                gosub Led_On ' Led on  for a bit 
                endif
                
                IF (RTCHour=$19) and (RTCMin=$59) Then 
                gosub Led_On ' Led on  for a bit 
                endif
                
                'IF (RTCHour=$17) and (RTCMin=$59) Then
                'gosub Led_Off ' Led off
                'endif
                
                If (ir=0) then
                gosub on_four ' then on for four mins
                gosub number ' Jumps to 3003 in morse
                endif 
                      
                'Pause 500           ' Do it about 2 times a min
                Goto mainloop        ' Do it forever and ever and ever
                
     'Light control routines             
            
    Led_On:     'high Led
                for time = 1 to 20  'On for 90 Mins
                high led
                pause 60000
                next time
                low led
                return 
    
    Number: High Led
                  Pause 6000
                 Low Led
                 return
    
    end
    Which seems to work mostly..... is there a better way of reading the clock ?? occasionally it misbehaves / doesn't work or turns on at wrong time or jumps to number label ...

    Just looking for pointers

    Thank you
    Andy
    Last edited by andybarrett1; - 21st November 2018 at 09:58. Reason: DS1307 not DS1302

  2. #2
    Join Date
    Oct 2009
    Posts
    582

    Default Re: DS1302 RTC Loop issues

    I've used the following, which may help

    Code:
    CH1_on_Time      var word                'stores the on time as a digit in minutes, eg 14:00hrs = 840
    
    TimeH           var byte                 'variable to store Hours
    TimeM           var byte                 'variable to store Minutes
    SS              VAR Byte                 'variable to store Seconds
    
    RTCsec          var byte                'RTC Seconds
    RTCMin          var byte		        'RTC Minutes
    RTCHour         var byte	            'RTC Hours
    RTCWDay         var byte	            ' Weekday
    RTCDay          var byte		        ' Day
    RTCMonth        var byte	            ' Months
    RTCYear         var byte	            ' Year
    RTCCtrl         var byte	            ' Control 
    
    Counter1        var word                'used to store the current time as a digit.  Counts minutes from 0000 at midnight
    
    
    lightsetHR          VAR word[16]                   
      lightsetHR1       VAR lightsetHR[0]     'CH1 on time hour
    LightsetHR is an array of 16, but you could simply set it to a word variable

    Then the main code in the loop


    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)    
    
    ss=(RTCSec>>4)                            'convert the BCD format of the sec register and store in variable ss
    ss=(ss &$07)*10
    ss=ss+(RTCSec&$0F)                     
    
    Counter1 = (TimeH *60)+ TimeM             'take hours and multiply it by 60 then add odd minutes to get the total minutes into counter1
    
    LCDOut $FE,$C0+11,#TimeH DIG 1,#TimeH DIG 0,":",#TimeM DIG 1,#TimeM DIG 0          'display the current time on the LCD
    gosub checktimes                            'routine for converting the HRs and Mins into a minutes
    Code:
    checktimes:
    ' converts the set hour and set min into minutes and places the result in the on time variable
    
    CH1_on_Time = (lightsetHR1*60)+lightsetMN1
    and then the matching

    Code:
    If Counter1 => CH1_on_Time and counter1 < CH1_off_time then                     ' check to see if the time in minutes since midnight matches the Channel on time
    Hope that helps
    Last edited by Scampy; - 21st November 2018 at 12:26.

Similar Threads

  1. 16f627a Loop issues
    By andybarrett1 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th October 2018, 13:46
  2. pic16F777 and ds1302 RTC
    By orjon1 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 28th June 2013, 11:57
  3. Issues with a feedback loop.
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th June 2012, 18:54
  4. RTC Problem using DS1302
    By gavo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2010, 18:48
  5. Interfacing RTC (DS1302) with pic16F676
    By Andrew_A in forum Off Topic
    Replies: 1
    Last Post: - 14th February 2006, 09:49

Members who have read this thread : 17

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