Code:
'****************************************************************
'* Name : ..\_INCLUDE\DS3231.PBP *
'* Author : Michel Jasmin *
'* Notice : NOT Copyright (c) 2015 Michel Jasmin *
'* : All Rights NOT Reserved *
'* Date : 2015-04-11 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
GOTO OverDS3231
'ToDo: Alias pins
'RTC_SDA Var PORTA.4
'RTC_SCL Var PORTA.5
'------------ Const -------------------------
AL1_OpS CON %00001111 'once per second
AL1_S CON %00001110 'when seconds match
AL1_MS CON %00001100 'when minutes and seconds match
AL1_HMS CON %00001000 'when hours, minutes and seconds match
AL1_DtHMS CON %00000000 'when DATE, hours, minutes and seconds match
AL1_DayHMS CON %00010000 'when DAY, hours, minutes and seconds match
AL2_OpM CON %00001110 'once per minute
AL2_M CON %00001100 'when minutes match
AL2_HM CON %00001000 'when hours and minutes match
AL2_DtHM CON %00000000 'when DATE, hours and minutes match
AL2_DayHM CON %00010000 'when DAY, hours and minutes match
'------------ 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
HexValue var byte
DecValue var byte
I2C_Adr VAR BYTE
I2C_Flags VAR BYTE
I2C_NACK VAR I2C_Flags.0
'General Register variable
RTC_Reg var byte
'Aliases for Control Register @ $0E
'EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
RTC_EOsc var RTC_Reg.7 'Enable Oscillator
RTC_BBSQW var RTC_Reg.6 'Battery-Backed Square-Wave Enable
RTC_CONV var RTC_Reg.5 'Convert Temperature
RTC_RS2 var RTC_Reg.4 'Rate Select
RTC_RS1 var RTC_Reg.3 'Rate Select
RTC_INTCN var RTC_Reg.2 'Interrupt Control
RTC_A2IE var RTC_Reg.1 'Alarm 2 Interrupt Enable
RTC_A1IE var RTC_Reg.0 'Alarm 1 Interrupt Enable
'Aliases for Status Register @ $0F
'OSF x x x EN32kHz BSY A2F A1F
RTC_OSF var RTC_Reg.7 'Oscillator Stop Flag
RTC_EN32kHz var RTC_Reg.3 'Enable 32kHz Output
RTC_BSY var RTC_Reg.2 'Busy
RTC_A2F var RTC_Reg.1 'Alarm 2 Flag
RTC_A1F var RTC_Reg.0 'Alarm 1 Flag
' Subroutine to write time to RTC
SetTime:
I2C_NACK = 0
I2C_Adr = $00
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear], I2CNACK
RETURN
' Subroutine to read time from RTC
GetTime:
I2C_NACK = 0
I2C_Adr = $00
I2CRead RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear], I2CNACK
RETURN
SetAlarm1:
'Usage:
'
' set values to RTCSec, RTCMin, RTCHour, [RTCDay | RTCDate]
'
' RTC_Reg = AL1_HMS 'Set alarm type
' gosub SetAlarm1
HSEROUT2 ["Alarm1 at: ", hex2 RTCDate, " ", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec, 13, 10]
'HSEROUT2 ["RTC_Reg = ", bin8 RTC_Reg, 13, 10]
I2C_Adr = $07
I2C_NACK = 0
RTCSec.7 = RTC_Reg.0 'A1M1
RTCMin.7 = RTC_Reg.1 'A1M2
RTCHour.7 = RTC_Reg.2 'A1M3
'Test A1M4
if RTC_Reg.3 = 1 then
'alarm by time only
'Get the alarm1 date
' I2C_Adr = $0A
' I2CRead SDA, RTC_SCL, $D0, I2C_Adr, [RTCDate], I2CNACK
RTCDate.7 = RTC_Reg.3 'A1M4
RTCDate.6 = 0 'DY/DT!
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCSec, RTCMin, RTCHour, RTCDate], I2CNACK
HSEROUT2 ["alarm1 by time only", 13, 10]
'HSEROUT2 [" at: ", bin8 RTCDate, " ", bin8 RTCHour, ":", bin8 RTCMin, ":", bin8 RTCSec, 13, 10]
else
'alarm by time and (DAY or DATE)
if RTC_Reg.4 = 1 then
'alarm by DAY
RTCDay.7 = 0 'A1M4
RTCDay.6 = 1 'DY/DT!
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCSec, RTCMin, RTCHour, RTCDay], I2CNACK
HSEROUT2 ["alarm1 by DAY", 13, 10]
'HSEROUT2 [" at: ", bin8 RTCDay, " ", bin8 RTCHour, ":", bin8 RTCMin, ":", bin8 RTCSec, 13, 10]
else
'alarm by DATE
RTCDate.7 = 0 'A1M4
RTCDate.6 = 0 'DY/DT!
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCSec, RTCMin, RTCHour, RTCDate], I2CNACK
HSEROUT2 ["alarm1 by DATE", 13, 10]
'HSEROUT2 [" at: ", bin8 RTCDate, " ", bin8 RTCHour, ":", bin8 RTCMin, ":", bin8 RTCSec, 13, 10]
endif
endif
RETURN
SetAlarm2:
'Usage:
'
' set values to RTCMin, RTCHour, [RTCDay | RTCDate]
'
' RTC_Reg = AL1_HMS 'Set alarm type
' gosub SetAlarm1
HSEROUT2 ["Alarm2 at: ", hex2 RTCDate, " ", hex2 RTCHour, ":", hex2 RTCMin, 13, 10]
I2C_Adr = $0B
I2C_NACK = 0
RTCMin.7 = RTC_Reg.1 'A1M2
RTCHour.7 = RTC_Reg.2 'A1M3
'Test A1M4
if RTC_Reg.3 = 1 then
'alarm by time only
RTCDate.7 = RTC_Reg.3 'A1M4
RTCDate.6 = 0 'DY/DT!
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCMin, RTCHour, RTCDate], I2CNACK
HSEROUT2 ["alarm2 by time only", 13, 10]
' HSEROUT2 [" at: ", bin8 RTCDate, " ", bin8 RTCHour, ":", bin8 RTCMin, 13, 10]
else
'alarm by time and (DAY or DATE)
if RTC_Reg.4 = 1 then
'alarm by DAY
RTCDay.7 = 0 'A1M4
RTCDay.6 = 1 'DY/DT!
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCMin, RTCHour, RTCDay], I2CNACK
HSEROUT2 ["alarm2 by DAY", 13, 10]
' HSEROUT2 [" at: ", bin8 RTCDay, " ", bin8 RTCHour, ":", bin8 RTCMin, 13, 10]
else
'alarm by DATE
RTCDate.7 = 0 'A1M4
RTCDate.6 = 0 'DY/DT!
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCMin, RTCHour, RTCDate], I2CNACK
HSEROUT2 ["alarm2 by DATE", 13, 10]
' HSEROUT2 [" at: ", bin8 RTCDate, " ", bin8 RTCHour, ":", bin8 RTCMin, 13, 10]
endif
endif
RETURN
'GetAlarm1:
' I2C_Adr = $07
' I2CRead RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate], I2CNACK
' RETURN
ReadControlRegister:
I2C_Adr = $0E
I2C_NACK = 0
I2CRead RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTC_Reg], I2CNACK
return
WriteControlRegister:
I2C_Adr = $0E
I2C_NACK = 0
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTC_Reg], I2CNACK
return
ReadStatusRegister:
I2C_Adr = $0F
I2C_NACK = 0
I2CRead RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTC_Reg], I2CNACK
return
WriteStatusRegister:
I2C_Adr = $0F
I2C_NACK = 0
I2CWrite RTC_SDA, RTC_SCL, $D0, I2C_Adr, [RTC_Reg], I2CNACK
return
I2CNACK:
I2C_NACK = 1
RETURN
Hex2BCD:
DecValue = 10 * (HexValue >> 4)
DecValue = DecValue + (HexValue & $0F)
return
BCD2Hex:
HexValue = (DecValue DIG 0) + ((DecValue DIG 1) << 4)
return
OverDS3231:
Bookmarks