fl { PropForth5.2 1-Wire Devices 03/09/2012 22:52:36 } hex \ ------- DS18B20 (Digital Termometer) -------------------------------------- EC wconstant ALARM_SEARCH BE wconstant READ_SCRATCHPAD 44 wconstant CONVERT_T 4E wconstant WRITE_SCRATCHPAD 48 wconstant COPY_SCRATCHPAD B8 wconstant RECALL_EE B4 wconstant READ_POWER_SUPPLY \ 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 copy_scratch \ Read 9bytes from scratch-pad 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 else ." Device is not present." cr 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 else ." Device is not present." cr 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(External power)" cr then then else ." Device is not present." cr 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 1 delms matchDevice \ Activate only desired device >r \ Push address READ_SCRATCHPAD writeByte copy_scratch \ Read 9bytes from scratch-pad rom 9 crc8 0= \ caluculation crc8 if rom W@ dup 550 = if drop r> parasite else r> drop rom 4+ C@ 5 rshift conv_degree ." degree" then else r> drop ." crc error(External power)" cr then then else ." Device is not present." cr 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! else ." Device is not present." cr then ; \ Display temperature on all DS18B20 : allTemp DS18B20 set_family \ 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! ; \ ------- DS2502 (1Kb Add-Only Memory) -------------------------------------- F0 wconstant READ_MEMORY AA wconstant READ_STATUS C3 wconstant READ_DATA&CRC 0F wconstant WRITE_MEMORY 55 wconstant WRITE_STATUS \ Display READSTATUS \ ( n -- ) n:device number : epromStatus reset if calc_ROM_addr \ Get address of ROM Code matchDevice \ Activate only desired device READ_STATUS writeByte \ Read EPROM status data 0 writeByte \ Write (T7:T0) 0 writeByte \ Write (T15:T8) readByte \ Get crc8 rom dup dup READ_STATUS swap C! 1+ 0 swap C! 2+ 0 swap C! rom 3 crc8 = \ check crc if rom 8 0 do dup readByte swap C! 1+ loop drop \ Copy receiving data to tepm-area rom 8 crc8 readByte = \ check crc if ." WRITE PROTECT" cr rom C@ 4 0 do ." PAGE" i 1+ . ." :" dup i 1+ and if ." 1" else ." 0" then cr loop drop cr ." PAGE ADDRESS REDIRECTION BYTE"cr rom 4 0 do ." PAGE" i 1+ . ." :" dup C@ . cr 1+ loop drop else ." CRC INCORRECT(STATUS BYTE's contents is incorrect)" then else ." CRC INCORRECT(Device not receive propery command" cr then else ." Device is not present." cr then ; \ PAGE area variable page 1C allot \ Read 1PAGE(328bytes) from memory \ ( n1 n2 -- ) n1:Page number(0-3) n2:device number : readPage reset if calc_ROM_addr \ Get address of ROM Code matchDevice \ Activate only desired device READ_DATA&CRC writeByte \ Read EPROM memory 3 and 20 u* dup >r writeByte \ Write (T7:T0) 0 writeByte \ Write (T15:T8) readByte \ Get crc8 page dup dup READ_DATA&CRC swap C! 1+ r> dup >r swap C! 2+ 0 swap C! page 3 crc8 = \ check crc if page 4 0 do 8 0 do dup readByte swap C! 1+ loop \ Copy receiving data to PAGE area loop drop page 20 crc8 readByte = \ check crc if ." PAGE" r> dup 0= if drop ." 0" else 20 u/ . then \ pop page number cr page 4 0 do 8 0 do dup C@ . 1+ loop cr loop drop else r> drop ." CRC INCORRECT(Receiving data are incorrect)" then else r> drop ." CRC INCORRECT(Device not receive propery command" cr then else ." Device is not present." cr then ; \ Write 1PAGE(32bytes) from memory \ ( n1 n2 n3 -- ) n1:data n2:Page number(0-3) n3:device number : writePage reset if calc_ROM_addr \ Get address of ROM Code matchDevice \ Activate only desired device WRITE_MEMORY writeByte \ Write EPROM memory WRITE_MEMORY page C! 3 and 20 u* page 1+ C! 0 page 2+ C! page 3 + C! 20 0 do WRITE_MEMORY writeByte \ Write command page 1+ C@ writeByte \ Write (T7:T0) 0 writeByte \ Write (T15:T8) page 3 + C@ writeByte \ Write data readByte \ Get crc8 page 4 crc8 \ Calculate crc8 = \ check crc if dira COG@ prog_mask or dira COG! \ Activate 12V 1D 0 do 1 1 u* drop loop \ wait 480usec dira COG@ prog_mask andn dira COG! readByte drop else ." CRC INCORRECT(Device not receive propery command" cr 1F seti then loop else ." Device is not present." cr then ; decimal