Code:
' 16F877A RTC
' 10/30/2011
DEFINE OSC 4
#CONFIG
__config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG
LED VAR PORTB.5
TX VAR PORTD.1 ' DATA SEND PIN
BAUD CON 18030 ' 18030 = 600 BAUD
'RTC pins on 877a
SDA Var PORTC.4
SCL Var PORTC.3
' 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
TRISC= %11111111
CMCON = %00000111
' Set initial time
RTCYear = $10
RTCMonth = $03
RTCDate = $23
RTCDay = $02
RTCHour = $19
RTCMin = $35
RTCSec = 0
RTCCtrl = 0
'Gosub set ' Set the time
Goto mainloop ' Skip over subroutines
' Subroutine to write time to RTC
set:
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
mainloop:
Gosub gettime ' Read the time from the RTC
TOGGLE LED
SEROUT2 TX, BAUD,["Date:", hex2 RTCDate, "/",hex2 RTCMonth, "/" , hex2 RTCYear,13]
SEROUT2 TX, BAUD,["Time:", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec,13]
' Lcdout $fe, 128,"Date:", hex2 RTCDate, "/",hex2 RTCMonth, "/" , hex2 RTCYear
' Lcdout $fe, $c0, "Time:", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec
Pause 500
GOTO mainloop
Bookmarks