Hempl/RTC
Introduction
[edit | edit source]The Real Time Clock chip on Mizar32's add-on ethernet board keeps track of the time of day and the date, whether the Mizar32 is powered on or not. It does this by having a small rechargeable battery and a crystal that vibrates at 32768 cycles per second, which it divides down to get a pulse that beats exactly once per second and uses this signal to keep the seconds, minutes and hours up to date, as well as the date, month and year.
Hardware view
[edit | edit source]The ethernet board has a 32768 Hz crystal and a DS1337 or a PCF8563 chip which is connected on the I2C bus. The two different chips have the same pin-out and almost identical functions; the reason we support both is due to a production error by one of our suppliers who printed DS1337 on the casing of a batch of PCF8563 chips.
Which actual chip is present is identified by the slave address that it responds to in the main I2C bus: the DS1337 responds to 7-bit decimal address 104, while the PCF8563 responds to slave address 81.
Both chips respond up to a speed of up to 400kHz on the I2C bus. We recommend using 100kHz, the default I2C that is set in Hempl.
For the register layout and how to address the devices using the raw I2C protocol, please see their datasheets.
Software view
[edit | edit source]SimpleMachines has added a module to Hempl to set the time and to read it.
Setting the time
[edit | edit source]You set the time using the function:
(mizar32-rtc-set param value)
where, param
can be any one of the following global PicoLisp symbols:
Field | Value | Meaning |
---|---|---|
*mizar32-rtc-sec* |
0-59 | Seconds |
*mizar32-rtc-min* |
0-59 | Minutes |
*mizar32-rtc-hour* |
0-23 | Hours (24-hour clock) |
*mizar32-rtc-day* |
1-31 | Day of month |
*mizar32-rtc-month* |
1-12 | Calendar month |
*mizar32-rtc-year* |
1900-2099 | Year |
*mizar32-rtc-wday* |
1-7 | Day of week |
param
is field to set and value
is the value at which param
is set.
In PicoLisp,
(mizar32-rtc-set *mizar32-rtc-year* 2014) (mizar32-rtc-set *mizar32-rtc-hour* 20)
The day of the week field, wday
, uses the following values to represent the seven days of the week:
Value | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Meaning | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
It is up to you to write the correct value into this field if you wish to use it; it is not set automatically from the date.
Reading the current time and date
[edit | edit source]The following function can be used to read time:
Function | Return value | Example: Print year |
---|---|---|
(mizar32-rtc-get) |
A list with the seven fields described above | (prinl "Year: " (car (nth (mizar32-rtc-get) 3)) )
|
The following code example makes a clock appear on the LCD display.
(de rtc-get (L N) (car (nth L N)) ) (mizar32-lcd-reset) (loop (setq time (mizar32-rtc-get)) (mizar32-lcd-goto 1 5) (mizar32-lcd-prinl (rtc-get time *mizar32-rtc-hour*) ":" (rtc-get time *mizar32-rtc-min*) ":" (rtc-get time *mizar32-rtc-sec*) ) (mizar32-lcd-goto 2 4) (mizar32-lcd-prinl (rtc-get time *mizar32-rtc-day*) "/" (rtc-get time *mizar32-rtc-month*) "/" (rtc-get time *mizar32-rtc-year*) ) )