fl { PropForth5.2 1-Wire Devices 02/09/2012 09:05:03 } hex \ ------- DS18B20 (Digital Termometer) -------------------------------------- \ Copy scratchpad to tepm-area \ ( -- ) : copy_scratch rom 9 0 do dup readByte swap C! 1+ loop drop ; \ Display datas from DS18B20's ScratcgPad memory \ ( n -- ) n:device number : DS18B20_rd_scratch reset if calc_ROM_addr \ Get address of ROM Code matchDevice \ Activate only desired device READ_SCRATCHPAD writeByte \ Read 9bytes from scratch-pad copy_scratch rom 9 crc8 0= \ caluculation crc8 if hex rom dup C@ ." Temperature LSB: " . cr 1+ dup C@ ." Temperature MSB: " . cr 1+ dup C@ ." TH register : " . cr 1+ dup C@ ." TL register : " . cr 1+ dup C@ ." Configuration : " . cr 1+ dup C@ ." Reserved(FFH) : " . cr 1+ dup C@ ." Reserved : " . cr 1+ dup C@ ." Reserved(10H) : " . cr 1+ C@ ." CRC : " . cr decimal else ." crc error" cr then then ; \ Write data to DS18B20's ScratcgPad memory \ ( n1 n2 n3 n4 -- ) n1:Configuration n2:Alarm TL n3:Alarm TH n4:device number : DS18B20_wr_scratch dup >r reset if calc_ROM_addr \ Get address of ROM Code matchDevice \ Activate only desired device WRITE_SCRATCHPAD writeByte writeByte writeByte writeByte then r> DS18B20_rd_scratch ; \ Convert Temp(12bit) to degree \ ( n1 n2 -- n3 ) n1:Temp n2:resolution(3:12bit 2:11bit 1 10bit 0:9bit) n3:degree : conv_degree 0 over = if 3 \ 9bit else 1 over = if 2 \ 10bit else 2 over = if 1 \ 11bit else 0 \ 12bit thens rot swap rshift over 0 over = if 2 else 1 over = if 4 else 2 over = if 8 else 10 thens swap drop u/mod \ quotient . ." ." \ remainder swap 0 over = if 1388 else 1 over = if 9C4 else 2 over = if 4E2 else 271 thens swap drop u* . ; \ Do parasite power \ (n1 -- ) n1:address : parasite reset if dup matchDevice \ Activate only desired device { outa COG@ ow_mask or outa COG! \ Set _OW-pin to Hi(output register) CONVERT_T writeByte dira COG@ ow_mask or dira COG! \ Set _OW-pin to Hi(dira register) 400 delms dira COG@ ow_mask andn dira COG! outa COG@ ow_mask andn outa COG! } CONVERT_T writeByte dira COG@ fet_mask or dira COG! \ Activate FET rom 4+ C@ 5 rshift dup 0= if 5E \ Res=9bit 94msec else dup 1 = if BC \ Res=10bit 188msec else dup 2 = if 177 \ Res=11bit 375msec else 2EE \ Res=12bit 750msec then then then swap drop delms dira COG@ fet_mask andn dira COG! reset if matchDevice \ Activate only desired device READ_SCRATCHPAD writeByte \ Read 5bytes from scratch-pad copy_scratch rom 9 crc8 0= \ caluculation crc8 if rom W@ \ conversion data rom 4+ C@ 5 rshift \ Resolution conv_degree ." degree" else ." crc error" cr then then then ; \ Read temperature \ ( n1 -- n2 ) n1:device number n2:temperature : readTemp reset if calc_ROM_addr \ Get address of ROM Code dup dup matchDevice \ Activate only desired device CONVERT_T writeByte begin 1 readBits 0> until reset if matchDevice \ Activate only desired device >r \ Push address READ_SCRATCHPAD writeByte \ Read 9bytes from scratch-pad copy_scratch rom 9 crc8 0= \ caluculation crc8 if rom W@ dup FFFF = if drop r> parasite else r> drop rom 4+ C@ 5 rshift conv_degree ." degree" then else ." crc error" cr then then then ; \ Copy scrachPad(3bytes) memory to eeprom \ ( n -- ) n1:device number : DS18B20_copy_eeprom reset if calc_ROM_addr \ Get address of ROM Code matchDevice \ Activate only desired device COPY_SCRATCHPAD writeByte \ Copy 3bytes to eeprom dira COG@ fet_mask or dira COG! A delms dira COG@ fet_mask andn dira COG! then ; \ Display temperature on all DS18B20 : allTemp DS18B20 flag W! \ Set family code deviceAddr 8 search begin dup 0 do ." device" i 1+ . ." : " i 1+ readTemp loop cr fkey? swap drop until drop 0 flag W! ; decimal