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