fl { RTC DS3231 driver PropForth 4.6 14/08/2011 08:14:35 RTC DS3231 Propeller scl P28 0x1c sda P29 0x1d } hex \ bcd> ( n1 -- n2 ) convert bcd byte n1 to hex byte n2 [ifndef bcd> : bcd> dup F and swap f0 and 1 rshift dup 2 rshift + + ; ] \ bcd> ( n1 -- n2 ) convert bcd byte n1 to hex byte n2 [ifndef bcd> : bcd> dup F and swap f0 and 1 rshift dup 2 rshift + + ; ] \ \ >bcd ( n1 -- n2 ) convert hex byte n1 to bcd byte n2 [ifndef >bcd : >bcd A u/mod 4 lshift + ; ] \ _rtcflip7 ( n7 n6 n5 n4 n3 n2 n1 -- n1 n2 n3 n4 n5 n6 n7 ) flip the top 7 items on the stack : _rtcflip7 rot >r rot >r rot >r rot >r rot >r swap r> r> r> r> r> rot >r rot >r rot >r swap r> r> r> swap rot ; \ \ _DS3231_w ( c1 -- ) : _DS3231_w _eewrite if 8007 ERR then ; \ _DS3231_s ( n1 -- ) I2C Start for DS3231 \ n1:register address c1:wr[0]/rd[1] : _DS3231_s _eestart d0 _DS3231_w \ slave address + wr[0] _DS3231_w \ register adress ; \ ( -- ) only Data Read : _DS3231_sr _eestart d1 _DS3231_w \ slave address + rd[1] ; \ _DS3231_time_r ( -- n1 n2 n3 n4 n5 n6 n7 ) read 7 bytes from DS3231 \ n1 - second (00 - 59) \ n2 - minute (00 - 59) \ n3 - hour (01 - 12 / 00 - 23) \ n4 - day of week (1 - 7) \ n5 - day (01 - 31) \ n6 - month (01 - 12) \ n7 - yesr (00 - 99) \ : _DS3231_time_r 0 _DS3231_s _eestart d1 _DS3231_w 6 0 do 0 _eeread loop -1 _eeread _eestop ; : _DS3231_time_r 0 _DS3231_s _DS3231_sr 6 0 do 0 _eeread loop -1 _eeread _eestop ; \ ( n1 n2 n3 n4 n5 n6 n7 -- ) write 7 bytes to DS3231 : _DS3231_time_w _rtcflip7 0 _DS3231_s 7 0 do _DS3231_w loop 0 _eeread drop _eestop ; \ _DS3231_alm1_r ( -- n1 n2 n3 n4 ) read 4 bytes from DS3231 \ n1 - second (00 - 59) \ n2 - minute (00 - 59) \ n3 - hour (01 - 12 / 00 - 23) \ n4 - day of week or date (1 - 7 / 01 - 31) : _DS3231_alm1_r 7 _DS3231_s _DS3231_sr 3 0 do 0 _eeread loop -1 _eeread _eestop ; \ ( n4 n3 n2 n1 -- ) write 4 bytes to DS3231 : _DS3231_alm1_w 7 _DS3231_s 4 0 do _DS3231_w loop 0 _eeread drop _eestop ; \ _DS3231_alm2_r ( -- n1 n2 n3 ) read 3 bytes from DS3231 \ n1 - minute (00 - 59) \ n2 - hour (01 - 12 / 00 - 23) \ n3 - day of week or date (1 - 7 / 01 - 31) : _DS3231_alm2_r b _DS3231_s _DS3231_sr 2 0 do 0 _eeread loop -1 _eeread _eestop ; \ ( n3 n2 n1 -- ) write 3 bytes to DS3231 : _DS3231_alm2_w b _DS3231_s 3 0 do _DS3231_w loop 0 _eeread drop _eestop ; \ ( -- n1 ) read DS3231's control-register : _DS3231_control_r e _DS3231_s _DS3231_sr -1 _eeread _eestop ; \ ( n1-- ) write data to DS3231's control-register : _DS3231_control_w e _DS3231_s _DS3231_w 0 _eeread drop _eestop ; \ _DS3231_status_r ( -- n1 ) read DS3231's status^register : _DS3231_status_r f _DS3231_s _DS3231_sr -1 _eeread _eestop ; \ ( -- n1 ) read Aging-Offset : _read_offset 10 _DS3231_s _DS3231_sr -1 _eeread _eestop ; \ ( n1-- ) write Aging-Offset : _write_offset 10 _DS3231_s _DS3231_w 0 _eeread drop _eestop ; \ read Temperature inside chip \ ( -- n1 n2 ) n1:integer portion n2:fractional portion : _read_temp 11 _DS3231_s _DS3231_sr 0 _eeread -1 _eeread _eestop ; \ ( n1 -- ) write week_name data to pad>out : _rtc_dow c" MONTUEWEDTHUFRISATSUN" 1+ swap 8 min 1 max 3 u* + 1- dup C@ #C 1- dup C@ #C 1- C@ #C ; \ ( n1 -- ) write Month_name data to pad>out : _rtc_mon c" JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC" 1+ swap C min 1 max 3 u* + 1- dup C@ #C 1- dup C@ #C 1- C@ #C ; { : test _DS3231_time_r 7 0 do . loop cr ; \ read current time : test1 0 29 21 1 10 7 11 _DS3231_time_w ; \ write current time 2011 June 10 21:29:00 Sun : test2 _DS3231_alm1_r 4 0 do . loop cr ; \ read alarm1 register : test3 1 2 3 4 _DS3231_alm1_w ; \ write alarm1 register : test4 _DS3231_alm2_r 3 0 do . loop cr ; \ read alarm2 register : test5 1 2 3 _DS3231_alm2_w ; \ write alarm2 register : test6 _DS3231_control_r . cr ; \ read controlregister : test7 18 _DS3231_control_w ; \ write controlregister : test8 _DS3231_status_r . cr ; \ read status register } decimal