The CGMMSTICK hardware does not have a built in real time clock, which means that the time is lost when the power is removed. Using the MMBasic 4.5 RTC GETTIME command and a PCF8563 real time clock integrated circuit you can automatically reset its clock on start up.
The PCF8563 is popular and cheap and will keep accurate time to about ±50 ppm even with the power removed (it is battery backed). It can be purchased for as cheap as $3 on eBay and complete modules using the PCF8563 along with a battery can be found for as little as $8.
The PCF8563 is an I2C device and should be connected to the I2C I/O pins of the CGMMSTICK . Also, because the PCF8563 draws very little current (even when communicating via I2C) it can be permanently connected to the battery (typical battery life is 15 years).
This circuit below shows a typical application.
The 32pF adjustable capacitor should be used to trim the crystal frequency for very accurate timekeeping but that can be tedious as it will involve checking the time for drift over days and weeks. If you don’t want to do that you can substitute a 10pF capacitor or leave it out completely and the timekeeping will still be reasonably accurate.
Before you can use the PCF8563 its time must be first set. That is done with the RTC SETTIME command which uses the format RTC SETTIME year, month, day, hour, minute, second. Note that the year is just the last two digits (ie, 14 for 2014) and hour is in 24 hour format. For example, the following will set the PCF8563 to 4PM on the 10th November 2014:
RTC SETTIME 14, 11, 10, 16, 0, 0
To get the time you use the RTC GETTIME command which will read the time from the PCF8563 and set the clock inside the CGMMSTICK . Normally this command will be placed at the beginning of the program so that the time is set on power up.
CGMICROMITE2 (CGMICROKIT) and CGMICROBOARD
Using the MMBasic 5.0 RTC GETTIME command it is easy to get the current time from a PCF8563, DS1307, DS3231 or DS3232 real time clock as well as compatible devices such as the M41T11. These integrated circuits are popular and cheap, will keep accurate time even with the power removed and can be purchased for $2 to $8 on eBay. Complete modules including the battery can also be purchased on eBay for a little more.
The PCF8563 and DS1307 will keep time to within a minute or two over a month while the DS3231 and DS3232 are particularly precise and will remain accurate to within a minute over a year. These chips are I2C devices and should be connected to the I2C I/O pins of the CGMICROMITE2 or CGMICROBOARD. This circuit shows a typical circuit for the DS1307. The other chips
have similar circuits (check their data sheets).
Internal pullup resistors (100KΩ) are applied to the I2C I/O pins so in many cases external resistors (as shown in the diagram) would not be needed. Before you can use the chip its time must be first set. That is done with the RTC SETTIME command which uses the format RTC SETTIME year, month, day, hour, minute, second. Note that the year is just the last two digits (ie, 15 for 2015) and hour is in 24 hour format.
For example, the following will set the real time clock to 4PM on the 10th November 2016:
RTC SETTIME 16, 11, 10, 16, 0, 0
To get the time you use the RTC GETTIME command which will read the time from the real time clock chip and set the CGMICROMITE2 or CGMICROBOARD internal clock. Normally this command will be placed at the beginning of the program or in the subroutine MM.STARTUP so that the time is set on power up.
The internal clock can drift by up to two or three seconds in an hour so to keep it over a long period the RTC can be polled at regular intervals using the SETTICK command.
' set the time at startup
' interrupt every 12 hours
SETTICK 12 * 3600000, SetTime, 4
< normal program >
' interrupt called every 12 hours
RTC GETTIME ' reset the time