Code:
' read the RTC date in BCD format
RTC_GetDate:
I2Cread SDA, SCL, $A2, DayYear, [gr[0]]
Date[0] = gr[0] & $3f
Date[2] = gr[0] >> 6
I2Cread SDA, SCL, $A2, Month, [Date[1]]
' if the RTC year has advanced, advance the baseyear too
I2Cread SDA, SCL, $A2, LastYear, [gr[0]]
if Date[2] <> gr[0] then
I2Cwrite SDA,SCL, $A2, LastYear, [Date[2]] ' record the current rTC year
I2Cread SDA, SCL, $A2, BaseYear, [gr[0]]
gr[0] = gr[0] + 1 ' advance the baseyear
I2Cwrite SDA,SCL, $A2, BaseYear, [gr[0]]
endif
' add the baseyear to the Current RTC year
I2Cread SDA, SCL, $A2, BaseYear, [gr[0]]
Date[2] = Date[2] + gr[0]
return
' Set date in BCD format
RTC_SetDate:
' Store the lastyear
gr[0] = Date[2] & 3 ' the RTC year
I2Cwrite SDA,SCL, $A2, LastYear, [gr[0]]
gr[0] = (gr[0] << 6)+(Date[0] & $3f)
I2Cwrite SDA, SCL, $A2, DayYear, [gr[0],Date[1]]
' store the baseyear
gr[0] = Date[2] & $fc ' the lowest 2 bits are in the RTC year
I2Cwrite SDA,SCL, $A2, BaseYear, [gr[0]]
return
Jerson
Bookmarks