define I2c_scl porta,3 define I2C_SDA porta,2 'define SER2_odd 1 state var byte house var byte unit var byte Inital_connection var byte 'SDA = porta.2 ' I2C SDA pin 'SCL = porta.3 ' I2C SCL pin I2C_WR CON $D0 ' I2C write address I2C_RD CON $D1 ' I2C read address Readtime VAR byte ' Set/Read time flag Second VAR Byte ' Store second value Minute VAR Byte ' Store minute value Hour VAR Byte ' Store hour value Day VAR Byte ' Store day value Date VAR Byte ' Store date value Month VAR Byte ' Store month value Year VAR Byte ' Store year value x var byte y var byte default_time var byte default_slot var byte unit_1_Hour_on var byte unit_1_Minute_on var byte unit_1_Hour_Off var byte unit_1_Minute_Off var byte unit_2_Hour_on var byte unit_2_Minute_on var byte unit_2_Hour_Off var byte unit_2_Minute_Off var byte unit_3_Hour_on var byte unit_3_Minute_on var byte unit_3_Hour_Off var byte unit_3_Minute_Off var byte unit_4_Hour_on var byte unit_4_Minute_on var byte unit_4_Hour_Off var byte unit_4_Minute_Off var byte unit_5_Hour_on var byte unit_5_Minute_on var byte unit_5_Hour_Off var byte unit_5_Minute_Off var byte unit_6_Hour_on var byte unit_6_Minute_on var byte unit_6_Hour_Off var byte unit_6_Minute_Off var byte unit_7_Hour_on var byte unit_7_Minute_on var byte unit_7_Hour_Off var byte unit_7_Minute_Off var byte unit_8_Hour_on var byte unit_8_Minute_on var byte unit_8_Hour_Off var byte unit_8_Minute_Off var byte unit_9_Hour_on var byte unit_9_Minute_on var byte unit_9_Hour_Off var byte unit_9_Minute_Off var byte unit_10_Hour_on var byte unit_10_Minute_on var byte unit_10_Hour_Off var byte unit_10_Minute_Off var byte unit_11_Hour_on var byte unit_11_Minute_on var byte unit_11_Hour_Off var byte unit_11_Minute_Off var byte unit_12_Hour_on var byte unit_12_Minute_on var byte unit_12_Hour_Off var byte unit_12_Minute_Off var byte unit_13_Hour_on var byte unit_13_Minute_on var byte unit_13_Hour_Off var byte unit_13_Minute_Off var byte unit_14_Hour_on var byte unit_14_Minute_on var byte unit_14_Hour_Off var byte unit_14_Minute_Off var byte unit_15_Hour_on var byte unit_15_Minute_on var byte unit_15_Hour_Off var byte unit_15_Minute_Off var byte unit_16_Hour_on var byte unit_16_Minute_on var byte unit_16_Hour_Off var byte unit_16_Minute_Off var byte house =0 unit = 0 'debug "start ",cr gosub get_data MAIN: serin2 portA.4,16780,5000,No_Data, [dec Inital_connection] enable debug debug dec Inital_connection ,cr disable debug if Inital_connection = 250 then 'set time goto settime endif if Inital_connection = 150 then 'turn radio button on goto unit_on endif if Inital_connection = 151 then 'turn radio button off goto unit_off endif if Inital_connection = 152 then 'unit 1 default_slot = 0 goto unit_SetTime endif if Inital_connection = 153 then 'unit 2 default_slot = 4 goto unit_SetTime endif if Inital_connection = 154 then 'unit 3 default_slot = 8 goto unit_SetTime endif if Inital_connection = 155 then 'unit 4 default_slot = 12 goto unit_SetTime endif if Inital_connection = 156 then 'unit 5 default_slot = 16 goto unit_SetTime endif if Inital_connection = 157 then 'unit 6 default_slot = 20 goto unit_SetTime endif if Inital_connection = 158 then 'unit 7 default_slot = 24 goto unit_SetTime endif if Inital_connection = 159 then 'unit 8 default_slot = 28 goto unit_SetTime endif if Inital_connection = 160 then 'unit 9 default_slot = 32 goto unit_SetTime endif if Inital_connection = 161 then 'unit 10 default_slot = 36 goto unit_SetTime endif if Inital_connection = 162 then 'unit 11 default_slot = 40 goto unit_SetTime endif if Inital_connection = 163 then 'unit 12 default_slot = 44 goto unit_SetTime endif if Inital_connection = 164 then 'unit 13 default_slot = 48 goto unit_SetTime endif if Inital_connection = 165 then 'unit 14 default_slot = 52 goto unit_SetTime endif if Inital_connection = 166 then 'unit 15 default_slot = 56 goto unit_SetTime endif if Inital_connection = 167 then 'unit 16 default_slot = 60 goto unit_SetTime endif if Inital_connection = 240 then 'get unit times goto Get_Times endif goto main No_Data: I2Cwrite porta.2 ,porta.3, I2C_WR, [0] I2Cread porta.2 ,porta.3, I2C_RD, [Second,Minute,Hour,Day,Date,Month,Year] enable debug DEBUG CR, "The time is ",HEX2 Month,"/",HEX2 Date, "/20",HEX2 Year, " ",hex2 Hour,":",HEX2 Minute,".",HEX2 Second,CR,CR disable debug 'unit 1 if unit_1_Hour_on = Hour and unit_1_Minute_on = Minute then Xout porta.0, porta.1, [0\0,0\unitOn] endif If unit_1_hour_off = hour and unit_1_minute_off = Minute then Xout porta.0, porta.1, [0\0,0\unitOff] endif 'unit 2 if unit_2_Hour_on = Hour and unit_2_Minute_on = Minute then Xout porta.0, porta.1, [0\1,0\unitOn ] endif If unit_2_hour_off = hour and unit_2_minute_off = Minute then Xout porta.0, porta.1, [0\1,0\unitOff] endif 'unit 3 if unit_3_Hour_on = Hour and unit_3_Minute_on = Minute then Xout porta.0, porta.1, [0\2,0\unitOn ] endif If unit_3_hour_off = hour and unit_3_minute_off = Minute then Xout porta.0, porta.1, [0\2,0\unitOff] endif 'unit 4 if unit_4_Hour_on = Hour and unit_4_Minute_on = Minute then Xout porta.0, porta.1, [0\3,0\unitOn ] endif If unit_4_hour_off = hour and unit_4_minute_off = Minute then Xout porta.0, porta.1, [0\3,0\unitOff] endif 'unit 5 if unit_5_Hour_on = Hour and unit_5_Minute_on = Minute then Xout porta.0, porta.1, [0\4,0\unitOn ] 'debug "ON",cr endif If unit_5_hour_off = hour and unit_5_minute_off = Minute then Xout porta.0, porta.1, [0\4,0\unitOff] endif 'unit 6 if unit_6_Hour_on = Hour and unit_6_Minute_on = Minute then Xout porta.0, porta.1, [0\5,0\unitOn ] endif If unit_6_hour_off = hour and unit_6_minute_off = Minute then Xout porta.0, porta.1, [0\5,0\unitOff] endif 'unit 7 if unit_7_Hour_on = Hour and unit_7_Minute_on = Minute then Xout porta.0, porta.1, [0\6,0\unitOn ] endif If unit_7_hour_off = hour and unit_7_minute_off = Minute then Xout porta.0, porta.1, [0\6,0\unitOff] endif 'unit 8 if unit_8_Hour_on = Hour and unit_8_Minute_on = Minute then Xout porta.0, porta.1, [0\7,0\unitOn ] endif If unit_8_hour_off = hour and unit_8_minute_off = Minute then Xout porta.0, porta.1, [0\7,0\unitOff] endif 'unit 9 if unit_9_Hour_on = Hour and unit_9_Minute_on = Minute then Xout porta.0, porta.1, [0\8,0\unitOn ] endif If unit_9_hour_off = hour and unit_9_minute_off = Minute then Xout porta.0, porta.1, [0\8,0\unitOff] endif 'unit 10 if unit_10_Hour_on = Hour and unit_10_Minute_on = Minute then Xout porta.0, porta.1, [0\9,0\unitOn ] endif If unit_10_hour_off = hour and unit_10_minute_off = Minute then Xout porta.0, porta.1, [0\9,0\unitOff] endif 'unit 11 if unit_11_Hour_on = Hour and unit_11_Minute_on = Minute then Xout porta.0, porta.1, [0\10,0\unitOn ] endif If unit_11_hour_off = hour and unit_11_minute_off = Minute then Xout porta.0, porta.1, [0\10,0\unitOff] endif 'unit 12 if unit_12_Hour_on = Hour and unit_12_Minute_on = Minute then Xout porta.0, porta.1, [0\11,0\unitOn ] endif If unit_12_hour_off = hour and unit_12_minute_off = Minute then Xout porta.0, porta.1, [0\11,0\unitOff] endif 'unit 13 if unit_13_Hour_on = Hour and unit_13_Minute_on = Minute then Xout porta.0, porta.1, [0\12,0\unitOn ] endif If unit_13_hour_off = hour and unit_13_minute_off = Minute then Xout porta.0, porta.1, [0\12,0\unitOff] endif 'unit 14 if unit_14_Hour_on = Hour and unit_14_Minute_on = Minute then Xout porta.0, porta.1, [0\13,0\unitOn ] endif If unit_14_hour_off = hour and unit_14_minute_off = Minute then Xout porta.0, porta.1, [0\13,0\unitOff] endif 'unit 15 if unit_15_Hour_on = Hour and unit_15_Minute_on = Minute then Xout porta.0, porta.1, [0\14,0\unitOn ] endif If unit_15_hour_off = hour and unit_15_minute_off = Minute then Xout porta.0, porta.1, [0\14,0\unitOff] endif 'unit 16 if unit_16_Hour_on = Hour and unit_16_Minute_on = Minute then Xout porta.0, porta.1, [0\15,0\unitOn ] endif If unit_16_hour_off = hour and unit_16_minute_off = Minute then Xout porta.0, porta.1, [0\15,0\unitOff] endif Goto main Settime: serin2 portA.4,16780, [hex2 hour] Hour = Hour & %00111111 ' Disable century serin2 portA.4,16780, [hex2 minute] serin2 portA.4,16780, [hex2 second] Second = Second & %01111111 ' Enable oscillator serin2 portA.4,16780, [hex2 day] serin2 portA.4,16780, [hex2 date] serin2 portA.4,16780, [hex2 month] serin2 portA.4,16780, [hex2 year] I2Cwrite porta.2 ,porta.3, I2C_WR, [0,Second,Minute,Hour,Day,Date,Month,Year] 'enable debug 'DEBUG CR, CR, "The current time has been successfully set!", CR, CR 'disable debug goto main END unit_on: serin2 portA.4,16780, [dec unit] serin2 portA.4,16780, [dec house] 'debug CR,dec unit,cr 'debug cr,dec House , cr Xout porta.0, porta.1, [house\unit,house\unitOn] 'Xout porta.0, porta.1, [house\unitoff] goto main unit_off: serin2 portA.4,16780, [dec unit] serin2 portA.4,16780, [dec house] Xout porta.0, porta.1, [house\unit,house\unitOff] 'xout porta.0, porta.1, [house\unitoff] goto main unit_SetTime: for y = 1 to 4 serin2 portA.4,16780, [hex2 default_time ] write default_slot, default_time enable debug debug hex2 default_time, cr debug "Slot:" ,dec default_slot, cr default_slot = default_slot + 1 next gosub get_data 'debug " Time has been set in eeprom",cr disable debug goto main get_times: serin2 portA.4,16780,[dec unit] serin2 portA.4,16780, [dec house] if unit = 0 and house =0 then 'unit 1 default_slot = 0 goto get_unit_time endif if unit =1 and house =0 then 'unit 2 default_slot = 4 goto get_unit_time endif if unit =2 and house =0 then 'unit 3 default_slot = 8 goto get_unit_time endif if unit =3 and house =0 then 'unit 4 default_slot = 12 goto get_unit_time endif if unit =4 and house =0 then 'unit 5 default_slot = 16 goto get_unit_time endif if unit =5 and house =0 then 'unit 6 default_slot = 20 goto get_unit_time endif if unit =6 and house =0 then 'unit 7 default_slot = 24 goto get_unit_time endif if unit =7 and house =0 then 'unit 8 default_slot = 28 goto get_unit_time endif if unit =8 and house =0 then 'unit 9 default_slot = 32 goto get_unit_time endif if unit =9 and house =0 then 'unit 10 default_slot = 36 goto get_unit_time endif if unit =10 and house =0 then 'unit 11 default_slot = 40 goto get_unit_time endif if unit =11 and house =0 then 'unit 12 default_slot = 44 goto get_unit_time endif if unit =12 and house =0 then 'unit 13 default_slot = 48 goto get_unit_time endif if unit =13 and house =0 then 'unit 14 default_slot = 52 goto get_unit_time endif if unit =14 and house =0 then 'unit 15 default_slot = 56 goto get_unit_time endif if unit =15 and house =0 then 'unit 16 default_slot = 60 goto get_unit_time endif goto main get_unit_time: enable debug for y = 1 to 4 ' debug dec y , cr read default_slot, default_time serout2 portA.6,16780, [hex2 default_time] debug hex2 default_time ,cr debug dec default_slot,cr serin2 portA.4,16780,1000,main, [x] default_slot = default_slot +1 pause 100 next disable debug goto main get_data: 'unit 1 read 0, unit_1_Hour_on read 1, unit_1_Minute_on read 2, unit_1_Hour_Off read 3, unit_1_Minute_Off 'unit 2 read 4, unit_2_Hour_on read 5, unit_2_Minute_on read 6, unit_2_Hour_Off read 7, unit_2_Minute_Off 'unit 3 read 8, unit_3_Hour_on read 9, unit_3_Minute_on read 10,unit_3_Hour_Off read 11,unit_3_Minute_Off 'unit 4 read 12, unit_4_Hour_on read 13, unit_4_Minute_on read 14, unit_4_Hour_Off read 15, unit_4_Minute_Off 'unit 5 read 16, unit_5_Hour_on read 17, unit_5_Minute_on read 18, unit_5_Hour_Off read 19, unit_5_Minute_Off 'unit 6 read 20, unit_6_Hour_on read 21, unit_6_Minute_on read 22, unit_6_Hour_Off read 23, unit_6_Minute_Off 'unit 7 read 24, unit_7_Hour_on read 25, unit_7_Minute_on read 26, unit_7_Hour_Off read 27, unit_7_Minute_Off 'unit 8 read 28, unit_8_Hour_on read 29, unit_8_Minute_on read 30, unit_8_Hour_Off read 31, unit_8_Minute_Off 'unit 9 read 32, unit_9_Hour_on read 33, unit_9_Minute_on read 34, unit_9_Hour_Off read 35, unit_9_Minute_Off 'unit 10 read 36, unit_10_Hour_on read 37, unit_10_Minute_on read 38, unit_10_Hour_Off read 39, unit_10_Minute_Off 'unit 11 read 40, unit_11_Hour_on read 41, unit_11_Minute_on read 42, unit_11_Hour_Off read 43, unit_11_Minute_Off 'unit 12 read 44, unit_12_Hour_on read 45, unit_12_Minute_on read 46, unit_12_Hour_Off read 47, unit_12_Minute_Off 'unit 13 read 48, unit_13_Hour_on read 49, unit_13_Minute_on read 50, unit_13_Hour_Off read 51, unit_13_Minute_Off 'unit 14 read 52, unit_14_Hour_on read 53, unit_14_Minute_on read 54, unit_14_Hour_Off read 55, unit_14_Minute_Off 'unit 15 read 56, unit_15_Hour_on read 57, unit_15_Minute_on read 58, unit_15_Hour_Off read 59, unit_15_Minute_Off 'unit 16 read 60, unit_16_Hour_on read 61, unit_16_Minute_on read 62, unit_16_Hour_Off read 63, unit_16_Minute_Off return