RTC Clock - Resetting


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2005
    Posts
    53

    Default RTC Clock - Resetting

    Hi,

    I am battling with a RTC Clock that keeps re-setting after I loose power. I have done a few things which has helped the RTC keep the time when the power is off.

    Please see code below:

    * When I program the PIC I first need to exclude this line of code:

    Shiftout IO, SCLK, LSBFIRST, [$8e, 128]

    * and then I Re-insert the line of code after I program the PIC for the first time then the RTC Clock runs if power is disconnected

    * I have also deleted the GoSub Gettime Routine after I also program the PIC for the First time that also seems to help as well

    Is there any other way of doing this without deleting the above?

    Any help would be greatly appreciated..;-)


    ' This will not affect normal program operation.
    Define LOADER_USED 1
    define OSC 20
    Include "MODEDEFS.BAS" ' Include Shiftin/out modes
    Define LCD_DREG PORTD ' Define LCD connections
    Define LCD_DBIT 0
    Define LCD_RSREG PORTA
    Define LCD_RSBIT 0
    Define LCD_EREG PORTA
    Define LCD_EBIT 1

    ' Alias pins

    RST Var PORTC.2
    IO Var PORTC.1
    SCLK Var PORTC.0

    ' Allocate variables

    rtcyear Var byte
    rtcday Var byte
    rtcmonth Var byte
    rtcdate Var byte
    rtchr Var byte
    rtcmin Var byte
    rtcsec Var byte
    rtccontrol Var byte

    Low RST ' Reset RTC

    Low SCLK

    ADCON1 = 7 ' PORTA and E digital

    'Shiftout IO, SCLK, LSBFIRST, [$8e, 0]

    rtcyear = $14
    rtcday = $04
    rtcmonth = $7
    rtcdate = $24
    rtchr = $07
    rtcmin = $57
    rtcsec = $00


    Gosub settime ' Set the time

    Goto mainloop ' Skip subroutines

    ' Subroutine to write time to RTC

    settime:

    RST = 1 ' Ready for transfer

    Shiftout IO, SCLK, LSBFIRST, [$8e, 0] ' Enable write

    RST = 0 ' Reset RTC

    RST = 1 ' Ready for transfer

    ' Write all 8 RTC registers in burst mode
    Shiftout IO, SCLK, LSBFIRST, [$8e, 128]
    Shiftout IO, SCLK, LSBFIRST, [$be, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0]

    RST = 0 ' Reset RTC
    'Shiftout IO, SCLK, LSBFIRST, [$8e, 128]

    Return



    gettime:

    RST = 1 ' Ready for transfer

    Shiftout IO, SCLK, LSBFIRST, [$bf] ' Read all 8 RTC registers in burst mode

    Shiftin IO, SCLK, LSBPRE, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol]

    RST = 0 ' Reset RTC

    Return

    ' Main program loop - in this case, it only updates the LCD with the time

    mainloop:

    Gosub gettime ' Read the time from the RTC

    ' Display time on LCD

    Lcdout $fe, 1, hex2 rtcmonth, "/", hex2 rtcdate, "/" , hex2 rtcyear," "
    Lcdout $fe,$C0, hex2 rtchr, ":", hex2 rtcmin, ":", hex2 rtcsec

    Pause 30 ' Do it about 3 times a second

    Goto mainloop


    End

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,378


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    some rtc's power on reset to a set value eg ds1307 power on default is 01/01/00 01 .
    you can look for this value when your pic powers up and if its something else then assume the time is still set and leave it as it is.


    extract from the ds1307 data sheet

    On first application of power to the device the time and date registers are typically reset to 01/01/00 01 00:00:00 (MM/DD/YY DOW HH:MM:SS). The CH bit in the seconds register will be set to a 1.

  3. #3
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Are you using a battery backup on the RTC that is isolated from the rest of the power supply?
    Louie

  4. #4
    Join Date
    Sep 2005
    Posts
    53


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Hi Louie,

    Yes I am connecting as per the datasheet, I am using a DS1302

    Regards,

    gavo

  5. #5
    Join Date
    Sep 2005
    Posts
    53


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Hi Richard,

    I seem to be doing everything correct, I even had MAXIM have a look at what I was doing, and they said it all looked good, that is why I am at such a loss having to program the device twice

    Regards,

    gavo

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,378


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    the 1302 seems to have no defined por register settings, so why not write a couple of bytes to the rtc's ram when you set the time . on subsequent pic boot ups check the rtc ram for those bytes , if they are not set then run your settime code

  7. #7
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    I noticed you have "rtccontrol Var byte" but no value assigned to it, then used in the "gettime" sub-routine.
    What is supposed to happen there?

    I would also suggest using a button input to jump to "settime" so you only have to program once and just push the button to set the time while its running.

    Code:
    ' Main program loop - in this case, it only updates the LCD with the time
    
    mainloop:
    
    Gosub gettime ' Read the time from the RTC
    
    ' Display time on LCD
    
    Lcdout $fe, 1, hex2 rtcmonth, "/", hex2 rtcdate, "/" , hex2 rtcyear," "
    Lcdout $fe,$C0, hex2 rtchr, ":", hex2 rtcmin, ":", hex2 rtcsec
    
    Pause 30 ' Do it about 3 times a second
    
    IF !switch THEN GOSUB settime
    
    Goto mainloop
    
    End
    And add a While/Wend for the switch so it waits before going back or else it'll be continuously setting the time until you release the button.

    Code:
    settime:
    
    RST = 1 ' Ready for transfer
    
    Shiftout IO, SCLK, LSBFIRST, [$8e, 0] ' Enable write
    
    RST = 0 ' Reset RTC
    
    RST = 1 ' Ready for transfer
    
    ' Write all 8 RTC registers in burst mode
    Shiftout IO, SCLK, LSBFIRST, [$8e, 128]
    Shiftout IO, SCLK, LSBFIRST, [$be, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0]
    
    RST = 0 ' Reset RTC
    'Shiftout IO, SCLK, LSBFIRST, [$8e, 128]
    
    WHILE !switch: WEND   ' Wait until button is released
    
    Return
    Louie

  8. #8
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    When a RTC is installed with battery backup the time only needs setting once. There are no routines within your code to utilise the settime: and by including the line
    Shiftout IO, SCLK, LSBFIRST, [$8e, 128] writes are disabled.

    Could I suggest you have two programs one to set the initial time which includes the settime:, without Shiftout IO, SCLK, LSBFIRST, [$8e, 128] line and a second program that does not include settime:.

    You are stuck with a two stage programing process but the second program will not include the unused settime: code.

    There are other alternatives as others have suggested one would be to read the date from the RTC and compare to the initial settings. What are the readings from the RTC before any time is set? Is the default reading on power up 01/01/00 for example. If so then simply test for the year being 00 and if it is set the time else skip over it.

    Let me know what the default power up readings are and then I can help with the code.

  9. #9
    Join Date
    Sep 2005
    Posts
    53


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Hi Steve,

    My apologies for the delay

    Default Clock is:

    01:01:00
    00:00:80

    Clock does not move

    Regards,

    Gavin

  10. #10
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Gavin,

    Gosub gettime before settime. Then if year =00 then settime also remove the disableing line "Shiftout IO, SCLK, LSBFIRST, [$8e, 128] "

  11. #11
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,793


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Gavin, didn't we solve that issue?

    Ioannis

  12. #12
    Join Date
    Sep 2005
    Posts
    53


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Yes we did, I was seeing if I could do a "one time" programming instead of 2, the rest is working 100%

    Regards,

    Gavin

  13. #13
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Quote Originally Posted by gavo View Post
    Yes we did, I was seeing if I could do a "one time" programming instead of 2, the rest is working 100%

    Regards,

    Gavin
    Is the one time programing working?
    Need more help?

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,793


    Did you find this post helpful? Yes | No

    Default Re: RTC Clock - Resetting

    Steve gave you the idea. Check the default of the DS1302 and then decide to setup or continue the normal program flow.

    Ioannis

Similar Threads

  1. Resetting a timer in VB6
    By Bruce in forum Off Topic
    Replies: 11
    Last Post: - 26th December 2013, 21:02
  2. PIC18F2220 I2C resetting
    By cpayne in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th October 2010, 00:09
  3. PIC keeps resetting when executing subroutine
    By jbirnsch in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 27th August 2009, 08:03
  4. PIC chip resetting. Very weird
    By The Master in forum Off Topic
    Replies: 0
    Last Post: - 28th October 2007, 18:07
  5. Pic resetting
    By malc-c in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st January 2007, 22:00

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