fl { MTOF171000C0:ToF(Time Of Flight)module PropForth 5.5 2019/12/26 21:56:03 MTOF171000C0 --- 3V3 ----|VDD| GND ----|GND| |TXD| GND ----|RXD| P28 ----|SCL| P29 ----|SCA| --- } \ ******************* \ Constants \ ******************* \ Slave addres h52 for MTOF171000C0 hA4 wconstant ToF_module \ ******************* \ Variables \ ******************* \ =========================================================================== \ standard i2c \ =========================================================================== : err_msg ." I2C error" ; \ If error, print message \ ( n1 -- ) n1:t/f : err? if err_msg cr then ; \ Start i2c-commnication \ This also can use SMBus device. \ ( -- ) lockdict create _eestart forthentry $C_a_lxasm w, h122 h113 1- tuck - h9 lshift or here W@ alignl h10 lshift or l, z1[ixnW l, z1[ixnX l, z2WyP[U l, z20iPak l, z3ryPW0 l, z1bixnW l, z2WyP[V l, z20iPak l, z3ryPW0 l, z1bixnX l, z1SV01X l, zl0 l, zCW l, zW0000 l, zG0000 l, freedict \ Re-defined RepeatedStart \ ( -- ) : Sr _eestart ; \ Stop i2c-commnication \ ( -- ) : _eestop _scli \ Release scl _sdai \ Release sda ; \ std_eewrite ( c1 -- t/f ) write c1 to the eeprom, true if there was an error \ Received acknowledge from i2c-device during scl is high \ scl/sda use pull-up resistor at hi \ clock:100kHz lockdict create std_eewrite forthentry $C_a_lxasm w, h12C h113 1- tuck - h9 lshift or here W@ alignl h10 lshift or l, z2WyPW8 l, z1YVPQ0 l, z1rixnd l, z1Sy\C] l, z1[ixne l, z1Sy\C] l, z1bixne l, zfyPO1 l, z3[yP[K l, z1[ixnd l, z1Sy\C] l, z1[ixne l, z1Sy\C] l, z1YF\Nl l, z1viPR6 l, z1bixne l, z1Sy\C] l, z1bixnd l, z1SV01X l, z2WyPh3 l, z20iPik l, z3ryPb0 l, z1SV000 l, zW0000 l, zG0000 l, freedict \ std_eeread ( t/f -- c1 ) flag should be true is this is the last read \ scl/sda use pull-up resistor at hi \ clock:100kHz lockdict create std_eeread forthentry $C_a_lxasm w, h12D h113 1- tuck - h9 lshift or here W@ alignl h10 lshift or l, z2WiPZB l, z2WyPO0 l, z1[ixne l, z2WyPj8 l, z1Sy\Ka l, z1[ixnf l, z1Sy\Ka l, z1XF\Vl l, znyPO1 l, z1bixnf l, z3[yPnN l, z26VPW0 l, z1rixne l, z1Sy\Ka l, z1[ixnf l, z1Sy\Ka l, z1bixnf l, z1bixne l, z1Sy\Ka l, z1SV01X l, z2WyPh3 l, z20iPik l, z3ryPb0 l, z1SV000 l, zW0000 l, zG0000 l, freedict \ Write data to register in i2c_device \ ( n1 n2 n3 -- ) n1:data n2:register n3:slave_address : std_i2c_wr \ Start I2C _eestart \ Write slave address[wr], then receive Acknowledge-bit(ACK:Lo NACK:Hi) std_eewrite \ ( n1 n2 t/f ) \ Write register swap std_eewrite or \ ( n1 t/f ) \ Write data swap std_eewrite or \ ( t/f ) \ Stop I2C _eestop err? \ ( -- ) ; \ Read distance data \ ( n1 n2 n3 -- n4 . . nn ) n1:number n2:command n3:slave_address n4..nn:series data : std_i2c_rd_multi \ Start I2C _eestart \ Write slave address[wr], then receive Acknowledge-bit(ACK:Lo NACK:Hi) tuck std_eewrite \ ( n1 n3 n2 t/f ) \ Write register swap std_eewrite or \ ( n1 n3 t/f ) swap \ ( n1 t/f n3 ) \ Repeated Start read_process Sr \ Write slave address[rd], then receive Acknowledge-bit(ACK:Lo NACK:Hi) 1 or std_eewrite or \ ( n1 t/f ) \ Read n1 byte >r \ Push flag ( n1 ) 0 do lasti? std_eeread \ ( t/f n4..nn ) loop r> \ Pop flag ( n4..nn t/f ) \ Stop I2C _eestop err? \ ( n4..nn ) ; \ Display connected i2c-device's slave-address \ This also can use SMBus device. \ ( -- ) : std_i2c_detect hex \ Print lower ." 0 1 2 3 4 5 6 7 8 9 A B C D E F" cr \ ( n1 n2 ) n1:reserve address 's count n2:i2c_device's count n3:i2c_address 0 0 0 8 0 do \ Print 1-line dup .byte h3A emit space d16 0 do \ Start I2C _eestart \ Write slave address[wr], then receive Acknowledge-bit(ACK:Lo NACK:Hi) dup i + 1 lshift std_eewrite 0= if \ there is ACK dup i + dup .byte \ Get slave-addres dup 7 > swap h78 < and \ 0 to 7 and h78 to h7F if swap 1+ swap \ Count up device (eliminate reserve number) else rot 1+ rot2 then else ." --" then space \ Stop I2C _eestop loop cr \ next line d16 + loop drop decimal ." i2c_device:" . cr 0> if ." [0 - 7] and [h78 - h7F] are reserve-address" then cr cr ; \ ******************* \ Main \ ******************* \ Reset sensor \ ( -- ) : resetToF \ Start I2C _eestart \ Write slave address[wr], then receive Acknowledge-bit(ACK:Lo NACK:Hi) ToF_module std_eewrite \ ( t/f ) \ Write command hF5 std_eewrite or \ ( t/f ) \ Stop I2C _eestop err? \ ( -- ) ; \ Displat distance \ ( -- ) : demo begin 2 hD3 ToF_module std_i2c_rd_multi swap 8 lshift or dup h8888 <> if . ." mm" else drop ." abnormal!!" then cr 100 delms fkey? swap drop until ;