\ \ DS1302 on PPDB \ Routines based on SPIN Example \ #P17 |< CONSTANT CE #P18 |< CONSTANT I/O #P19 |< CONSTANT CLK DECIMAL \ I usually work in decimal $ prefix for hex, % prefix for binary \ \ DS1302 Command codes %10000000 CONSTANT wrtsecreg %10000001 CONSTANT rdsecreg %10000010 CONSTANT wrtminreg %10000011 CONSTANT rdminreg %10000100 CONSTANT wrthrsreg %10000101 CONSTANT rdhrsreg %10000110 CONSTANT wrtdatereg %10000111 CONSTANT rddatereg %10001000 CONSTANT wrtmonreg %10001001 CONSTANT rdmonreg %10001100 CONSTANT wrtyrreg %10001101 CONSTANT rdyrreg %10001110 CONSTANT wrtctrlreg %10001111 CONSTANT rdctrlreg %10010000 CONSTANT wrttcreg %10010001 CONSTANT rdtcreg %10111110 CONSTANT wrtbrstreg %10111111 CONSTANT rdbrstreg \ BCD support routines - clock chip data is in BCD : BCD>DEC ( BCD -- n ) DUP $F0 AND 4 SHR 10 * SWAP $0F AND + ; : DEC>BCD ( n -- BCD ) 10 U/MOD 4 SHL OR ; \ \ DS1302 write and read routines \ : DS1302! ( data -- ) I/O OUTPUTS \ set I/O PIN to OUTPUT (normally redundant but needed for SHROUT) I/O SWAP \ set up the I/O pin on DS1302 (pinmask data -- ) 8 FOR SHROUT \ put the bit out there CLK OUTSET \ data input to DS1302 on RISING edge 1 us \ let the clock stabilize CLK OUTCLR \ falling edge NEXT 2DROP ; : DS1302@ ( -- data ) I/O DUP INPUTS \ set I/O PIN to INPUT 0 ( mask data -- ) 8 FOR SHRINP CLK OUTSET \ rising edge 1 us CLK OUTCLR \ falling edge - data is output from DS1302 NEXT NIP ; \ DS1302 initialization - set up the control parameters you want/need \ : INIT_DS1302 CLK OUTCLR 10 us CE OUTSET wrtctrlreg DS1302! 0 DS1302! \ clear the write protect bit in ctrl register wrtsecreg DS1302! 0 DS1302! \ clear the CLOCK HALT bit (upper bit in seconds register) wrthrsreg DS1302! 0 DS1302! \ clear the 24HR mode flag - the single digit clock likes 24 HR time wrttcreg DS1302! 0 DS1302! \ clear out the trickle charge flags (not used here) CE OUTCLR ; : SET_SECONDS ( n -- ) DEC>BCD CE OUTSET wrtsecreg DS1302! DS1302! CE OUTCLR ; : GET_SECONDS ( -- n ) CE OUTSET rdsecreg DS1302! DS1302@ CE OUTCLR 24 SHR BCD>DEC ; : SET_MINUTES ( n -- ) DEC>BCD CE OUTSET wrtminreg DS1302! DS1302! CE OUTCLR ; : GET_MINUTES ( -- n ) CE OUTSET rdminreg DS1302! DS1302@ CE OUTCLR 24 SHR BCD>DEC ; : SET_HOURS ( n -- ) DEC>BCD CE OUTSET wrthrsreg DS1302! DS1302! CE OUTCLR ; : GET_HOURS ( -- n ) CE OUTSET rdhrsreg DS1302! DS1302@ CE OUTCLR 24 SHR BCD>DEC ;