fl { RTC DS1337 driver PropForth 4.6 09/08/2011 22:58:49 RTC DS1337 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 ; \ \ _DS1337_w ( c1 -- ) : _DS1337_w _eewrite if 8007 ERR then ; \ _DS1337_s ( n1 -- ) I2C Start for DS1337 \ n1:register address c1:wr[0]/rd[1] : _DS1337_s _eestart d0 _DS1337_w \ slave address + wr[0] _DS1337_w \ register adress ; \ ( -- ) only Data Read : _DS1337_sr _eestart d1 _DS1337_w \ slave address + rd[1] ; \ _DS1337_time_r ( -- n1 n2 n3 n4 n5 n6 n7 ) read 7 bytes from DS1337 \ 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) \ : _DS1337_time_r 0 _DS1337_s _eestart d1 _DS1337_w 6 0 do 0 _eeread loop -1 _eeread _eestop ; : _DS1337_time_r 0 _DS1337_s _DS1337_sr 6 0 do 0 _eeread loop -1 _eeread _eestop ; \ ( n1 n2 n3 n4 n5 n6 n7 -- ) write 7 bytes to DS1337 : _DS1337_time_w _rtcflip7 0 _DS1337_s 7 0 do _DS1337_w loop 0 _eeread drop _eestop ; \ _DS1337_alm1_r ( -- n1 n2 n3 n4 ) read 4 bytes from DS1337 \ n1 - second (00 - 59) \ n2 - minute (00 - 59) \ n3 - hour (01 - 12 / 00 - 23) \ n4 - day of week or date (1 - 7 / 01 - 31) : _DS1337_alm1_r 7 _DS1337_s _DS1337_sr 3 0 do 0 _eeread loop -1 _eeread _eestop ; \ ( n4 n3 n2 n1 -- ) write 4 bytes to DS1337 : _DS1337_alm1_w 7 _DS1337_s 4 0 do _DS1337_w loop 0 _eeread drop _eestop ; \ _DS1337_alm2_r ( -- n1 n2 n3 ) read 3 bytes from DS1337 \ n1 - minute (00 - 59) \ n2 - hour (01 - 12 / 00 - 23) \ n3 - day of week or date (1 - 7 / 01 - 31) : _DS1337_alm2_r b _DS1337_s _DS1337_sr 2 0 do 0 _eeread loop -1 _eeread _eestop ; \ ( n3 n2 n1 -- ) write 3 bytes to DS1337 : _DS1337_alm2_w b _DS1337_s 3 0 do _DS1337_w loop 0 _eeread drop _eestop ; \ ( -- n1 ) read 1 bytes from DS1337 : _DS1337_control_r e _DS1337_s _DS1337_sr -1 _eeread _eestop ; \ ( n1-- ) write 1 bytes to DS1337 : _DS1337_control_w e _DS1337_s _DS1337_w 0 _eeread drop _eestop ; \ _DS1337_status_r ( -- n1 ) read 1 bytes from DS1337 : _DS1337_status_r f _DS1337_s _DS1337_sr -1 _eeread _eestop ; \ _rtc_dow ( n1 -- ) : _rtc_dow c" MONTUEWEDTHUFRISATSUN" 1+ swap 8 min 1 max 3 u* + 1- dup C@ #C 1- dup C@ #C 1- C@ #C ; \ _rtc_mon ( n1 -- ) : _rtc_mon c" JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC" 1+ swap C min 1 max 3 u* + 1- dup C@ #C 1- dup C@ #C 1- C@ #C ; { : test _DS1337_time_r 7 0 do . loop cr ; \ read current time : test1 0 29 21 1 10 7 11 _DS1337_time_w ; \ write current time 2011 June 10 21:29:00 Sun : test2 _DS1337_alm1_r 4 0 do . loop cr ; \ read alarm1 register : test3 1 2 3 4 _DS1337_alm1_w ; \ write alarm1 register : test4 _DS1337_alm2_r 3 0 do . loop cr ; \ read alarm2 register : test5 1 2 3 _DS1337_alm2_w ; \ write alarm2 register : test6 _DS1337_control_r . cr ; \ read controlregister : test7 18 _DS1337_control_w ; \ write controlregister : test8 _DS1337_status_r . cr ; \ read status register } decimal