This is the code that I use to manage 8 time clock events.
I store the start and stop times in the DM[] array. The use of the DM[] array allowed me to make the stop/start times easily changeale (not hard coded). In fact, I actually store the stop/start times in EEPROM and copy the values from EEPROM into DM[1..17] on start up.
The 8 relays that are affected by the routine are in sequential order are relays #17 through #24. These 8 relays are accessed using the setbit and clrbit.
In this example I am only worried about starting and stopping things with a resolution of 1 minute so I only look at time[1] and time[2]. This routine is called every 30 seconds to ensure that I don't miss an event (Nyquest limit).
If you need to make your time clock accurate to 1 second, then you will need to take time[3] into account. You will need to call your routine at least twice each second.
Please note that I use ">=" and "<" to compare my stop/start times against the current time and do not use "=". This ensures that if the PLC is started (or reset) in the middle of a start/stop time period that the outputs (relays) will be set/cleared correctly and not have to wait 24 hrs for the exact "start time".
Please also note that I worry about start/stop events that span midnight (00:00:00) so that these events are handled correctly.
'TimeChk - Called periodically to manage relays 17..23
'Relays are set on/off based on the current time and the
'stop/start times stored in dm[1..17]
'
t=time[1]*100+time[2] ' t is current time in scaled minutes
n=1 ' index into dm[] for start/stop pairs
for i = 0 to 7 ' each relay
if dm[n]<=dm[n+1]
'ON period does not span midnight
if (t>=dm[n]) and (t<dm[n+1])
setbit RELAY[2],i
else
clrbit RELAY[2],i
endif
else
'ON period spans midnight
if (t>=dm[n+1]) and (t<dm[n])
clrbit RELAY[2],i
else
setbit RELAY[2],i
endif
endif
n=n+2 ' next stop/start pair
next