fl { RTC DS1337 driver This serve only basic function(setting time and reading time) PropForth 5.5 2013/05/19 20:38:57 RTC DS1337 Propeller scl ---- P28 0x1c sda ---- P29 0x1d } \ Slave addres for DS1337 hD0 wconstant slave \ bcd> ( n1 -- n2 ) convert bcd byte n1 to hex byte n2 [ifndef bcd> : bcd> dup hF and swap hF0 and 1 rshift dup 2 rshift + + ; ] \ \ >bcd ( n1 -- n2 ) convert hex byte n1 to bcd byte n2 [ifndef >bcd : >bcd d10 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 ; \ error message \ ( -- ) : err_msg ." I2C error" cr ; \ Write data \ ( n1 -- n2 ) n1:data n2:t/f true=success : DS1337_w _eewrite if 0 else 1 then ; \ Read 7 bytes from DS1337 [Write pointer, then read] \ ( -- n1 n2 n3 n4 n5 n6 n7 ) \ n1 - second (00 - 59) \ n2 - minute (00 - 59) \ n3 - hour (00 - 23) \ n4 - day of week (1 - 7) \ n5 - day (01 - 31) \ n6 - month (01 - 12) \ n7 - yesr (00 - 99) : DS1337_time_r \ Start I2C for DS1337 _eestart \ Write slave address[wr] slave DS1337_w if \ Write register-addr 0 0 DS1337_w if \ Set read _eestart slave 1 or DS1337_w if \ Read 6bytes 6 0 do 0 _eeread loop \ Read 1byte ,then Stop reading return ACK -1 _eeread \ Stop I2C _eestop else err_msg then else err_msg then else err_msg then ; wvariable err \ Write 7 bytes to DS1337 \ ( n1 n2 n3 n4 n5 n6 n7 -- ) \ n1 - yesr (00 - 99) \ n2 - month (01 - 12) \ n3 - day (01 - 31) \ n4 - day of week (1 - 7) \ n5 - hour (00 - 23) \ n6 - minute (00 - 59) \ n7 - second (00 - 59) : DS1337_time_w 0 err W! \ Re-alighnment _rtcflip7 \ Start I2C for DS1337 _eestart \ Write slave address[wr] slave DS1337_w if \ Write register-addr 0 0 DS1337_w if \ Write 7bytes 7 0 do DS1337_w 1 <> if i 1+ err W! leave then loop \ err W@ if err W@ 7 < if 7 err W@ - 0 do drop loop then \ Drop data on stack err_msg else \ return Not-ACK 0 _eeread drop \ Stop I2C _eestop then else 3drop 3drop drop err_msg then else 3drop 3drop drop err_msg then ; \ Read Hr/Min from Ds1337 ( -- n1 n2 ) n1:Minute n2:Hour : read_hrmin DS1337_time_r 3drop drop bcd> swap bcd> rot drop swap ; \ Read&convert all time-data from DS1337 \ ( -- n1 n2 n3 n4 n5 n6 n7 ) \ n1 - second (00 - 59) \ n2 - minute (00 - 59) \ n3 - hour (00 - 23) \ n4 - day of week (1 - 7) \ n5 - day (01 - 31) \ n6 - month (01 - 12) \ n7 - yesr (2000 - 2099) : read_time DS1337_time_r bcd> d2000 + >r \ Year bcd> >r \ Month bcd> >r \ Day >r \ week bcd> >r \ Hour bcd> >r \ Minute bcd> \ Second r> r> r> r> r> r> ; \ Set time to DS1337 \ ( n1 n2 n3 n4 n5 n6 -- ) \ n1 - yesr (00 - 99) \ n2 - month (01 - 12) \ n3 - day (01 - 31) \ n4 - day-of-week (Mon:1 Tue:2 Wed:3 Thur:4 Fri:5 Sat:6 San:7) \ n4 - hour (00 - 23) \ n5 - minute (00 - 59) : set_time >bcd >r \ minute >bcd >r \ hour >r \ day-of-week >bcd >r \ day >bcd >r \ month d2000 - >bcd \ year r> r> r> r> r> 0 _rtcflip7 DS1337_time_w read_time . . . . . . . ; { \ Set time to 2013 5/19 20:39 Prop0 Cog6 ok 2013 5 19 7 20 39 set_time 2013 5 19 7 20 39 0 Prop0 Cog6 ok \ Read time read_time . . . . . . . 2013 5 19 7 20 39 11 Prop0 Cog6 ok }