RTC: Using Events
The basic principle of using events to trigger actions in your program is that you can test each of the events to determine if it is tripped. An event is tripped if:
- The event is armed. An event gets armed by an
RTCWriteEventor by being tested outside the “golden minute” (the actual minute the event is set for), AND - The current minute (now) is the same as the minute for which the event is set, AND
- The current day is a day for which the event’s Day mask is set, AND
- The event has not already been read as tripped today (except altering event settings re-arms it).
There are 2 instructions for configuring events:
RTCReadEvent n
Reads event register n to the HH, MM and DM registers for display and/or modification
RTCWriteEvent n
Writes HH, MM and DM to event n after modifications
There are 3 instructions for testing events. These are:
GoIfRTCEvent n,Line
If event n has tripped, goes to the named program Line.
WaitForRTCEvent n
Waits for event n to occur. This can only be used in a MultiTrack task.
RTCTestEvent n
If event n has tripped, pushes True into X. If the event has not tripped pushes False into X.
Note that the event number in all cases is subject to IasJ: precode and, if used inside a MultiTrack task, jndexing.
Please refer to the product documentation for your particular controller to find out how many events it supports.
Example:
In an energy management system for a 24-7 food store, the outside lights are to be turned on according to the following monthly schedule:
| Month | On at | Off at |
|---|---|---|
| Jan | 1700 | 0800 |
| Feb | 1730 | 0730 |
| Mar | 1800 | 0700 |
| Apr | 1830 | 0700 |
| May | 1900 | 0630 |
| Jun | 1900 | 0600 |
| Jul | 2000 | 0600 |
| Aug | 2000 | 0530 |
| Sep | 1900 | 0600 |
| Oct | 1800 | 0630 |
| Nov | 1730 | 0700 |
| Dec | 1700 | 0730 |
The program has 3 MultiTrack tasks. The actual turning on and turning off of lights is done by LightsOn and LightsOff. These are simple “3-liners” that simply wait for their respective events then perform their action.
The turnon and turnoff events are maintained by a task called UpdateTimes. UpdateTimes is more interesting. It pre-loads events 0 and 1 according to a schedule stored as a table in NVEM page 0. UpdateTimes is itself run once a day using event 7, which it presets itself to trip at midnight, when first launched.
It is not actually necessary for the times to be refreshed every midnight. If they are set at start-up it will be enough. However by doing it this way we open the door to a system that can have the times changed either through a built in user interface, or by a remote communications facility. Testing events repeatedly in a looping MultiTrack task, like this example does, is a good way of meeting the requirement that an event needs to be tested outside the “golden minute” in order to be re-armed.
OutSideLights oEQU 0
LaunchTask UpdateTimes
LaunchTask LightsOn
LaunchTask LightsOff
RunTasksForever
;Task to turn on the lights at the right time each day
LightsOn:
WaitForRTCEvent 0
On OutSideLights
GoTo LightsOn
;Task to turn off the lights at the right time each day
LightsOff:
WaitForRTCEvent 1
Off OutSideLights
GoTo LightsOff
;Task that keeps the turnon and turnoff event times updated
UpdateTimes:
GoSub SetEvents ;Initialize the events
LoadX 23 ;Preset event 7 to midnight every day
RTCPopHH
LoadX 59
RTCPopMM
LoadX 255
RTCPopDM
RTCWriteEvent 7
UpDateTLoop:
WaitForRTCEvent 7 ;Wait for midnight
GoSub SetEvents ;Refresh the events
GoTo UpDateTLoop
SetEvents:
;Refresh the turnon and turnoff events
NVSetPage 0 ;Select NVEM0 page
NVSetPtr OLTable ;Point to table of on/off times
NVSetRecLen 4 ;4 bytes per table entry
RTCReadDate ;Get the date
RTCPushMM ;Access the month
DecX ;Months are 1-12, table is 0-11
NVPopRecNum ;Point to table entry
RTCReadEvent 0 ;Turn on time
NVPushByte 0 ;Turn on hour
RTCPopHH ;Store Turn on hour
NVPushByte 1 ;Turn on minute
RTCPopHH ;Store Turn on minute
RTCWriteEvent 0 ;Save updated event
RTCReadEvent 1 ;Turn off time
NVPushByte 2 ;Turn off hour
RTCPopHH ;Store Turn off hour
NVPushByte 3 ;Turn off minute
RTCPopHH ;Store Turn off minute
RTCWriteEvent 1 ;Save updated event
Return
;The following must be placed at the end of the program, after all executable code:
NVEM0
;Table of turn-on and turnoff times. Each entry is 4 bytes,
;being HH(on), MM(on), HH(off) and MM(off)
OLTable:
NV0Byte 17,00,08,00 ;Jan
NV0Byte 17,30,07,30 ;Feb
NV0Byte 18,00,07,00 ;Mar
NV0Byte 18,30,07,00 ;Apr
NV0Byte 19,00,06,30 ;May
NV0Byte 19,00,06,00 ;Jun
NV0Byte 20,00,06,00 ;Jul
NV0Byte 20,00,05,30 ;Aug
NV0Byte 19,00,06,00 ;Sep
NV0Byte 18,00,06,30 ;Oct
NV0Byte 17,30,07,00 ;Nov
NV0Byte 17,00,07,30 ;Dec