**** Working Code but subject to change as more features are added, basic functions working ****
**** Update Sept 2019 ****
For this example of using the DS1302 RTC Module we will be interfacing with it via the Z80 PIO, the code should be able to be adapted to other interface options.
Quick run down of the DS1302 Real Time Clock chip, for this example we have the Chip in a pre-made module with a 3-volt lithium Battery, the chip allows for also the use of Rechargeable Batteries or Super Capacitor though a trickle charge feature which can be found on Datasheet.
The chip has the following time keeping Functions: 12/24 hour mode, AM/PM, Hours, Minutes, seconds, day of the week, date, Month, year, time is stored in BCD format, Write protect Bit, and Oscillator enable bit, 31 bytes of 8 bit RAM storage
The chip is accessed by a Three wire interface, CLOCK, RESET (CE), DATA, since the DATA is both input and output, the connection to it must be bi-directional. The Z80 PIO mode 3 allows you to select which bits are inputs and outputs, this allows the DATA pin to be switch between a output to input during data read's.
Note from DS1302 Datasheet if power has been removed or not initialized for the first time the Write protect bit and Clock Oscillator enable bit are in a indeterminate state. At start up of program just to be sure you should write those bit's to zero to enable Clock.
The Clock timing diagram and Register address and definitions below will be used for creating our read and write routines.
Sample write routine:
Note I use custom defines for the Z80 and custom port Bit write/read routine that works similar to Arduino pin function. The function z80_pio_write_bit(port, bit 7-0, 1 or 0) and z80_pio_read_bit(port, bit 7-0) adds some overhead in code but helps in following the code.
ds1303_rtc_z80pio.c
/* DS1303 RTC Z80 PIO interface version */ds1303_rtc_z80pio.h
/* DS1303 RTC Z80 PIO interface version *//* header file */Sample code using the above functions:
/* Z80 PIO RTC ds1302 clock module */