'Dallas Semiconductor DS3231 Precision Real Time Clock Application Program Interface 'By Anthony Rhodes September 27, 2005 'copyright Anthony Rhodes 2005 'This code may be used freely in all software, but no charge may be made For this module. 'Uses Functions Native to the Z-Basic Family of Processors 'ZX time Format is always 24 hr, and so it will be For the RTC as well. 'NOTE: The DS1337 RTC uses functions that are a Subset of the DS3231. ' This code will work with all supported functions of the DS1337. Public Const I2C50K as Integer = 137 Public Const I2C100K as Byte = 66 '100k is "Standard Speed", 400k is "High Speed" Public Const I2C200K as Integer = 29 'not a Formally defined I2C speed Public Const I2C400K as Integer = 11 'actually 388K, but anything else might exceed 400K limit Public Const I2CMAX as Integer = 10 '409.6K: the max the AVR hardware will allow. 'N.B. DS1307 is rated ONLY at "standard" speed: 100Kbps 'N.B. DS1337, DS3231 work at BOTH "standard" and "high" speeds (400k) 'N.B. Ramtron devices usually work at 1Mbps! 'Pin 11 = I2C SDA For hardware port 'Pin 12 = I2C SCL For hardware port Public Const RTC_Write as Byte = bx1101_0000 'fits all Dallas RTC chips, therefore only 1 Dallas RTC on a bus. Public Const RTC_Read as Byte = bx1101_0001 Public Const RTC_ID as Byte = RTC_Write '-----------CONTROL Byte SETTINGS---------- Public Const RTC_BAT_CLK_Off as Byte = bx1000_0000 Public Const RTC_BAT_CLK_On as Byte = bx0000_0000 Public Const RTC_BAT_SQW_Off as Byte = bx0000_0000 Public Const RTC_BAT_SQW_On as Byte = bx0100_0000 Public Const RTC_Start_Conv as Byte = bx0010_0000 Public Const RTC_No_Conv as Byte = bx0000_0000 Public Const RTC_SQW_1HZ as Byte = bx0000_0000 Public Const RTC_SQW_1KHZ as Byte = bx0000_1000 Public Const RTC_SQW_4KHZ as Byte = bx0001_0000 Public Const RTC_SQW_8KHZ as Byte = bx0001_1000 Public Const RTC_SQW_Enable as Byte = bx0000_0000 'disables the alarm interrupt Public Const RTC_SQW_Disable as Byte = bx0000_0100 'enables the alarm interrupt Public Const RTC_Alm1_Disable as Byte= bx0000_0000 Public Const RTC_Alm1_Enable as Byte = bx0000_0010 Public Const RTC_Alm2_Disable as Byte= bx0000_0000 Public Const RTC_Alm2_Enable as Byte = bx0000_0001 '----------CONTROL/STATUS Byte SETINGS Public Const RTC_Osc_Flag as Byte = bx1000_0000 Public Const RTC_32KHZ_Disable as Byte= bx0000_0000 Public COnst RTC_32KHZ_Enable as Byte = bx0000_1000 Public Const RTC_Conv_Busy as Byte = bx0000_0100 Public Const RTC_Alm2_Flag as Byte = bx0000_0010 Public Const RTC_Alm1_Flag as Byte = bx0000_0001 '-------------ALARM SETTINGS --------------- Public Const Alm1_Every_Second AS Byte = 1 Public Const Alm1_Match_Second as Byte = 2 Public Const Alm1_Match_Min_Sec as Byte = 3 Public Const Alm1_Match_Hr_Min_Sec as Byte = 4 Public Const Alm1_Match_Date_Hr_Min_Sec as Byte = 5 Public Const Alm1_Match_Day_Hr_Min_Sec as Byte = 6 Public Const Alm2_Every_Minute as Byte = 1 Public Const Alm2_Match_Minute as Byte = 2 Public Const Alm2_Match_Hr_Min as Byte = 3 Public Const Alm2_Match_Date_Hr_Min as Byte = 4 Public Const Alm2_Match_Day_Hr_Min as Byte = 5 'N.B. numbers are Binary Coded Decimal! Private Const RTC_Addr_Seconds as Byte = &H00 '00-59: bit6:4 = tens, Bit3:0 = ones Private Const RTC_Addr_Minutes as Byte = &H01 '00-59: bit6:4 = tens, Bit3:0 = ones Private Const RTC_Addr_Hours as Byte = &H02 'Hours: 1-12/1-23: Bit6=1 For 12hour, 0 For 24hour, bit5:4 = tens or PM/AM(1/0)+tens, bit 3:0 = ones Private Const RTC_Addr_WDay as Byte = &H03 '1-7 arbitrary Bit2:0 = ones Private Const RTC_Addr_MDay as Byte = &H04 '01-31: Bit5:4 = tens, Bit3:0 = ones Private Const RTC_Addr_Month as Byte = &H05 '01-12: Bit4 = tens, Bit3:0 = ones Private Const RTC_Addr_Year as Byte = &H06 '00-99: Bit7:4 = tens, Bit3:0 = ones Private Const RTC_ADDR_Alm1_Start as Byte = &H07 'Start of Alarm1 Settings Private Const RTC_Addr_Alm2_Start as Byte = &H0B 'Start of Alarm2 Settings Private Const RTC_Addr_Control as Byte = &H0E 'Bit7 = /EOSC, Bit6=BBSQW, Bit5=Conv, Bit4:3=Rate Select (0:0=1hz, 0:1=1.024KHz, 1:0=4.096KHz, 1:1=8.192KHz Private Const RTC_Addr_Status as Byte = &H0F 'status Private Const RTC_Addr_Age as Byte = &H10 'Aging Offset Private Const RTC_TempH as Byte = &H11 'MSB of temp(this Byte is whole degrees C) Private Const RTC_TempL as Byte = &H12 'LSB of temp: bit 7:6 is in units of 0.25 degrees 'block data For the day of week function Dim month_code as new Bytevectordata ({6,2,2,5,0,3,5,1,4,6,2,4}) 'this code is ZX specIfic. For BX, the data needs to be moved to a datafile, one entry per line 'Then use this command: Call month_code.source("filename.txt") Private RTC_Channel as Byte '______________________________________________________________________ Public Sub RTC_Set_Channel(ByVal Channel as Byte) RTC_Channel = Channel End Sub '_______________________________________________________________________________ Public Sub RTC_Initialize ( ByVal Channel as Byte, _ ByVal Set_Osc as Byte, _ ByVal Set_Bat_SQW as Byte, _ ByVal Set_SQW_Freq as Byte, _ ByVal Set_SQWInt_Enable as Byte, _ ByVal Set_Alm1 as Byte, _ ByVal Set_Alm2 as Byte, _ ByVal Set_32KHZ as Byte) 'use this For a full initialize 'except For the clock Dim Status_Byte as Byte Dim Success as Integer 'Dim Address as Byte 'Address = RTC_Addr_Status RTC_Channel = Channel 'get the status Byte 'Could use RTC_Get_Status(Status_Byte, Success), but this saves stack. 'Success = I2CCmd(Channel, RTC_ID, 1, address, 1, Status_Byte) 'handle lack of Success here...... Call RTC_Set_Control ( Channel,Set_Osc,Set_Bat_SQW,Set_SQW_Freq, _ Set_SQWInt_Enable, Set_Alm1, Set_Alm2, Set_32KHZ, Success) 'check Success here..... 'check For oscillator stoppage in the Status Byte 'If (CBool(Status_Byte AND bx1000_0000)) Then 'If OSF flag set Then need to set time 'debug.print ("resetting the clock") 'Call RTC_PutTimeStamp (5,8,27,0,0,0, Success) 'defaults to the date this was written (For fun) 'End If End Sub '________________________________________________________________ Public Sub RTC_PutTimeStamp ( ByVal year as Byte, _ ByVal month as Byte, _ ByVal MDay as Byte, _ ByVal hour as Byte, _ ByVal minute as Byte, _ ByVal second as Byte, _ ByRef Success as Integer) Dim Write_Data(1 to 8) as Byte Write_Data(1) = RTC_Addr_Seconds Write_Data(2) = RTC_Second(second) Write_Data(3) = RTC_Minute(minute) Write_Data(4) = RTC_Hour(hour) Write_Data(5) = RTC_Calc_Day_of_Week(year,month,Mday) Write_Data(6) = RTC_MDay(Mday) Write_Data(7) = RTC_Month(month) Write_Data(8) = RTC_Year(year) 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 8, write_data(1), 0, 0) End Sub '_____________________________________________________________________ Public Sub RTC_GetTimeStamp(ByRef year as Byte, _ ByRef month as Byte, _ ByRef MDay as Byte, _ ByRef hour as Byte, _ ByRef minute as Byte, _ ByRef second as Byte, _ ByRef Success as Integer) Dim Read_Data(1 to 7) as Byte Dim Address as Byte Address = RTC_Addr_Seconds 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 1, Address, 7, Read_Data(1)) Second = RTC_BCDtoBin(Read_Data(1)) Minute = RTC_BCDtoBin(Read_Data(2)) Hour = RTC_BCDtoBin(Read_Data(3)) Mday = RTC_BCDtoBin(Read_Data(5)) Read_Data(6) = Read_Data(6) AND bx0111_1111 'mask off the century bit in the odd case it is set Month = RTC_BCDtoBin(Read_Data(6)) Year = RTC_BCDtoBin(Read_Data(7)) End Sub '________________________________________________________________ Public Sub RTC_PutTime ( ByVal hour as Byte, _ ByVal minute as Byte, _ ByVal second as Byte, _ ByRef Success as Integer) Dim Write_Data(1 to 4) as Byte Write_Data(1) = RTC_Addr_Seconds Write_Data(2) = RTC_Second(second) Write_Data(3) = RTC_Minute(minute) Write_Data(4) = RTC_Hour(hour) 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 4, write_data(1), 0, 0) End Sub '_____________________________________________________________________ Public Sub RTC_GetTime ( ByRef hour as Byte, _ ByRef minute as Byte, _ ByRef second as Byte, _ ByRef Success as Integer) Dim Read_Data(1 to 3) as Byte Dim Address as Byte Address = RTC_Addr_Seconds 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 1, Address, 3, Read_Data(1)) Second = RTC_BCDtoBin(Read_Data(1)) Minute = RTC_BCDtoBin(Read_Data(2)) Hour = RTC_BCDtoBin(Read_Data(3)) End Sub '________________________________________________________________ Public Sub RTC_Put_Date ( ByVal year as Byte, _ ByVal month as Byte, _ ByVal MDay as Byte, _ ByRef Success as Integer) Dim Write_Data(1 to 5) as Byte Write_Data(1) = RTC_Addr_Wday Write_Data(2) = RTC_Calc_Day_of_Week(year,month,Mday) Write_Data(3) = RTC_MDay(Mday) Write_Data(4) = RTC_Month(month) Write_Data(5) = RTC_Year(year) 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 5, write_data(1), 0, 0) End Sub '_____________________________________________________________________ Public Sub RTC_Get_Date(ByRef year as Byte, _ ByRef month as Byte, _ ByRef MDay as Byte, _ ByRef Success as Integer) Dim Read_Data(1 to 3) as Byte Dim Address as Byte Address = RTC_Addr_MDay 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 1, Address, 3, Read_Data(1)) Mday = RTC_BCDtoBin(Read_Data(1)) Month = RTC_BCDtoBin(Read_Data(2)) Year = RTC_BCDtoBin(Read_Data(3)) End Sub '______________________________________________________________________ Public Sub RTC_Set_Control( ByVal Channel as Byte, _ ByVal Set_Osc as Byte, _ ByVal Set_Bat_SQW as Byte, _ ByVal Set_SQW_Freq as Byte, _ ByVal Set_SQWInt_Enable as Byte, _ ByVal Set_Alm1 as Byte, _ ByVal Set_Alm2 as Byte, _ ByVal Set_32KHZ as Byte, _ ByRef Success as Integer) Dim Write_Data(1 to 3) as Byte Dim RTC_Control as Byte RTC_Control = Set_Osc OR Set_Bat_SQW OR Set_SQW_Freq OR Set_SQWInt_Enable OR Set_Alm1 OR Set_Alm2 'debug.print "control Byte is "; cstr(RTC_control) 'debug.print "made from:" 'debug.print cstr(set_osc) 'debug.print cstr(set_bat_sqw) 'debug.print cstr(set_sqw_freq) 'debug.print cstr(set_sqwint_enable) 'debug.print cstr(set_alm1) 'debug.print cstr(set_alm2) 'debug.print cstr(set_32KHZ) Write_data(1) = RTC_Addr_Control Write_Data(2) = RTC_Control Write_Data(3) = Set_32KHZ Success = I2CCmd(RTC_Channel, RTC_ID, 3, write_data(1), 0, 0) End Sub '__________________________________________________________________________ Public Sub RTC_Get_Status(ByRef Status_Byte as Byte, ByRef Success as Integer) 'bit7 = Osc Stop Flag (stopped clock), Bit1 = Alarm 2 Int Flag, Bit0 = Alarm1 Int Flag Dim Address as Byte Address = RTC_Addr_Status Success = I2CCmd(RTC_Channel, RTC_ID, 1,Address, 1, Status_Byte) End Sub '________________________________________________________________________________ Public Function RTC_Osc_Stopped() as boolean Dim Status as Byte Dim Success as Integer Dim Address as Byte Address = RTC_Addr_Status Success = I2CCmd(RTC_Channel, RTC_ID, 1, address, 1, Status) RTC_Osc_Stopped = CBool(Status AND bx1000_0000) End Function '--------------------------------------------------------------------------- Public Sub RTC_Get_Control(ByRef Control_Byte as Byte, ByRef Success as Integer) Dim Address as Byte Address = RTC_Addr_Control Success = I2CCmd(RTC_Channel, RTC_ID, 1,Address, 1, Control_Byte) End Sub '_______________________________________________________________________________ Public Function RTC_Get_Temp() as Integer 'Gets the last temperature reading, which occurs once a minute unless specIfically instructed Dim Temp_Byte (1 to 2) as Byte Dim Address as Byte Dim Success as Integer Dim Status_Byte as Byte Address = RTC_Addr_Status Do Success = I2CCmd(RTC_Channel, RTC_ID, 1,Address, 1, Status_Byte) 'handle lack of Success here Loop While (Cbool(Status_Byte AND RTC_Conv_Busy)) Address = RTC_TempH Success = I2CCmd(RTC_Channel, RTC_ID, 1,Address, 2, temp_Byte(1)) If (Success > 0) Then 'RTC_Get_Temp = CInt(Temp_Byte(1)) * 256 + Cint(Temp_Byte(2)) RTC_Get_Temp =cint(MakeWord(Temp_Byte(2), Temp_Byte(1))) Else RTC_Get_Temp = 0 End If End Function '------------------------------------------------------------------------- Public Sub Set_Alarm1(ByVal Control as Byte, _ ByVal DyDt as Byte, _ ByVal Hours as Byte, _ ByVal Minutes as Byte, _ ByVal Seconds as Byte, _ ByRef Success as Integer) Dim Settings (1 to 4) as Byte If (hours >23) Then hours = 23 End If If (minutes >59) Then minutes = 59 End If If (Seconds > 59) Then Seconds = 59 End If If (Control = 6) Then 'Check DAY If (DyDt = 0) Then DyDt = 1 elseIf (DyDt > 7) Then DyDt = 7 End If else 'Otherwise Check DATE If (DyDt = 0) Then DyDt = 1 elseIf (DyDt > 31) Then DyDt = 31 'does not check For non-existent dates! End If End If Select Case Control Case 1 Settings(2) = RTC_BinToBCD(Seconds) OR bx1000_0000 Settings(3) = RTC_BinToBCD(Minutes) OR bx1000_0000 Settings(4) = RTC_BinToBCD(Hours) OR bx1000_0000 Settings(5) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 2 Settings(2) = RTC_BinToBCD(Seconds) Settings(3) = RTC_BinToBCD(Minutes) OR bx1000_0000 Settings(4) = RTC_BinToBCD(Hours) OR bx1000_0000 Settings(5) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 3 Settings(2) = RTC_BinToBCD(Seconds) Settings(3) = RTC_BinToBCD(Minutes) Settings(4) = RTC_BinToBCD(Hours) OR bx1000_0000 Settings(5) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 4 Settings(2) = RTC_BinToBCD(Seconds) Settings(3) = RTC_BinToBCD(Minutes) Settings(4) = RTC_BinToBCD(Hours) Settings(5) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 5 Settings(2) = RTC_BinToBCD(Seconds) Settings(3) = RTC_BinToBCD(Minutes) Settings(4) = RTC_BinToBCD(Hours) Settings(5) = RTC_BinToBCD(DyDt) Case 6 Settings(2) = RTC_BinToBCD(Seconds) Settings(3) = RTC_BinToBCD(Minutes) Settings(4) = RTC_BinToBCD(Hours) Settings(5) = RTC_BinToBCD(DyDt) OR bx0100_0000 End Select Settings (1) = RTC_Addr_Alm1_Start 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 5, Settings(1), 0, 0) End Sub '------------------------------------------------------------ Public Sub Set_Alarm2 (ByVal Control as Byte, _ ByVal DyDt as Byte, _ ByVal Hours as Byte, _ ByVal Minutes as Byte, _ ByRef Success as Integer) Dim Settings (1 to 4) as Byte If (hours >23) Then hours = 23 End If If (minutes >59) Then minutes = 59 End If If (Control = 5) Then 'Check DAY If (DyDt = 0) Then DyDt = 1 elseIf (DyDt > 7) Then DyDt = 7 End If else 'Otherwise Check DATE If (DyDt = 0) Then DyDt = 1 elseIf (DyDt > 31) Then DyDt = 31 'does not check For non-existent dates! End If End If Select Case Control Case 1 Settings(2) = RTC_BinToBCD(Minutes) OR bx1000_0000 Settings(3) = RTC_BinToBCD(Hours) OR bx1000_0000 Settings(4) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 2 Settings(2) = RTC_BinToBCD(Minutes) Settings(3) = RTC_BinToBCD(Hours) OR bx1000_0000 Settings(4) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 3 Settings(2) = RTC_BinToBCD(Minutes) Settings(3) = RTC_BinToBCD(Hours) Settings(4) = RTC_BinToBCD(DyDt) OR bx1000_0000 Case 4 Settings(2) = RTC_BinToBCD(Minutes) Settings(3) = RTC_BinToBCD(Hours) Settings(4) = RTC_BinToBCD(DyDt) Case 5 Settings(2) = RTC_BinToBCD(Minutes) Settings(3) = RTC_BinToBCD(Hours) Settings(4) = RTC_BinToBCD(DyDt) OR bx0100_0000 End Select Settings (1) = RTC_Addr_Alm2_Start 'I2CCmd(channel, slave_id, write_count, write_data, read_count, read_data) Success = I2CCmd(RTC_Channel, RTC_ID, 4, Settings(1), 0, 0) End Sub '--------------------------------------------- Public Sub RTC_Set_Alarms (ByVal Alarms as Byte) '0 = NONE, 1 For alarm1, 2 For alarm2, 3 For both Dim Control_Byte as Byte Dim Success as Integer Dim Address as Byte Dim Write_Data (1 to 2) as Byte If (Alarms > 3) Then Alarms = 0 'incorrect data clears the alarms End If Address = RTC_Addr_Control 'read the control Byte Success = I2CCmd(RTC_Channel, RTC_ID, 1, Address, 1, Control_Byte) 'deal with lack of Success here 'make the new Control Byte Write_data(1) = RTC_Addr_Control Write_data(2) = (Control_Byte AND bx1111_1100) OR Alarms 'clear the bits and set now ones 'Write the new Control Byte Success = I2CCmd(RTC_Channel, RTC_ID, 2, Write_Data(1), 0, 0) 'deal with lack of Success here End Sub '----------------------------------------------------------- Public Sub RTC_Enable_Alarm_Interrupt() 'allows the alarms to trigger the interrupt 'this disables the square wave output Dim Control_Byte as Byte Dim Success as Integer Dim Address as Byte Dim Write_Data (1 to 2) as Byte Address = RTC_Addr_Control 'read the control Byte Success = I2CCmd(RTC_Channel, RTC_ID, 1, Address, 1, Control_Byte) 'deal with lack of Success here 'make the new Control Byte Write_data(1) = RTC_Addr_Control Write_data(2) = (Control_Byte OR bx0000_0100) 'set the interrupt control bit 'Write the new Control Byte Success = I2CCmd(RTC_Channel, RTC_ID, 2, Write_Data(1), 0, 0) 'deal with lack of Success here End Sub '--------------------------------------------- Public Sub RTC_Disable_Alarm_Interrupt() 'Disallows the alarms to trigger the interrupt 'Enables the square wave output Dim Control_Byte as Byte Dim Success as Integer Dim Address as Byte Dim Write_Data (1 to 2) as Byte Address = RTC_Addr_Control 'read the control Byte Success = I2CCmd(RTC_Channel, RTC_ID, 1, Address, 1, Control_Byte) 'deal with lack of Success here 'make the new Control Byte Write_data(1) = RTC_Addr_Control Write_data(2) = (Control_Byte AND bx1111_1011) 'set the interrupt control bit 'Write the new Control Byte Success = I2CCmd(RTC_Channel, RTC_ID, 2, Write_Data(1), 0, 0) 'deal with lack of Success here End Sub '-------------------------------------------------- Public Function RTC_Get_Alarm_Flags() as Byte Dim Success as Integer Call RTC_Get_Status(RTC_Get_Alarm_Flags, Success) 'deal with lack of Success here RTC_Get_Alarm_Flags = RTC_Get_Alarm_Flags AND bx0000_0011 End Function '______________________________________________________________________ Private Function RTC_BinToBCD(ByVal data as Byte) as Byte RTC_BinToBCD = (data \ 10) * 16 'shIft left the tens by 4 bits to the high nibble RTC_BinToBCD = RTC_BinToBCD + (data MOD 10) 'low nibble is the ones of the data End Function '______________________________________________________________________ Private Function RTC_BCDToBin(ByVal data as Byte) as Byte RTC_BCDToBin = (data \ 16) *10 RTC_BCDToBin = RTC_BCDToBin + (data AND bx0000_1111) End Function '_______________________________________________________________________ Private Function RTC_Year (ByVal year as Byte) as Byte If (year >99) Then year = 0 End If RTC_Year = RTC_BinToBCD(year) End Function '__________________________________________________________________________ Private Function RTC_Month (ByVal month as Byte) as Byte If (month <1) Then month = 1 End If If (month >12) Then month = 12 End If RTC_Month = RTC_BinToBCD(month) End Function '__________________________________________________________________________ Private Function RTC_Mday (ByVal Mday as Byte) as Byte If (mday <1) Then Mday = 1 End If If (Mday >31) Then Mday = 31 End If RTC_Mday = RTC_BinToBCD(Mday) End Function '__________________________________________________________________________ Private Function RTC_Wday (ByVal Wday as Byte) as Byte If (wday <1) Then wday = 1 End If If (wday >7) Then Wday = 7 End If RTC_Wday = RTC_BinToBCD(Wday) End Function '__________________________________________________________________________ Private Function RTC_Hour (ByVal Hour as Byte) as Byte 'always 24 hour Format to match ZX time functions If (hour >23) Then hour = 23 End If RTC_Hour = RTC_BinToBCD(hour) End Function '_________________________________________________________________________ Private Function RTC_Minute (ByVal minute as Byte) as Byte If (minute >59) Then minute = 59 End If RTC_Minute = RTC_BinToBCD(minute) End Function '_________________________________________________________________________ Private Function RTC_Second (ByVal Second as Byte) as Byte If (second > 59) Then second = 59 End If RTC_Second = RTC_BinToBCD(second) 'RTC_Second = RTC_Second + bx1000_0000 'disable the oscillator End Function '_________________________________________________________________________ 'Private Function RTC_Control() as Byte ' If (RTC_rate_select > 3) Then ' RTC_rate_select = 0 ' End If ' RTC_Control = RTC_Rate_Select 'bits 1:0 range 0-3 ' If (RTC_SQW_Out) Then ' RTC_Control = RTC_Control + bx1000_0000 'Bit7 ' End If ' If (RTC_SQWE) Then ' RTC_Control = RTC_Control + bx0001_0000 'Bit4 ' End If 'End Function '____________________________________________________________________________ 'calculate the day of the week in the 21st century Public Function RTC_Calc_Day_of_Week(ByVal year as Byte, _ ByVal month as Byte, _ ByVal mday as Byte) as Byte 'returns: 1=Sunday, 2=Monday.... 7=Saturday RTC_Calc_Day_of_Week = year + year\4 'max:102 min:0 RTC_Calc_Day_of_Week = RTC_Calc_Day_of_Week + month_code(month) 'max:6 min:0 RTC_Calc_Day_of_Week = RTC_Calc_Day_of_Week + mday 'max:31 min 1 'If Jan/Feb in a leapyear Then Subtract 1 If NOT CBool(year AND 3) Then 'bitwise OR to determine If either bit0 or bit1 are set If ((month = 1) or (month = 2)) Then ' correct For jan & feb in a leapyear RTC_Calc_Day_of_Week = RTC_Calc_Day_of_Week - 1 End If End If RTC_Calc_Day_Of_Week = RTC_Calc_Day_of_Week mod 7 +1 End Function