\ PING))) words for Propforth V5.0 and up \ based on Brian Riley - Underhill Center, VT, USA PING.fth for Tachyon from Aug 18, 2012 \ which was based on Spin PING))) code form Chris Savage/Jeff Martin - Parallax \ \ ported to PropForth by Rick Post (aka mindrobots) - August 22, 2012 \ PING))) signal pin connected to Propeller I/O pin via 1K resistor \ PING))) +5v and GND as indicated \ \ marker for forgetting 1 constant ping_words \ conversion constants for room temperature as defined in the PING))) manual 73746 constant toinches 29034 constant tocms \ delus (n1 -- ) delay n1 micro-seconds \ PropForth does not have a micro-second delay word, so we need to create one \ based on delms definition in PropForth kernel : delus h7FFFFFFF clkfreq 1000000 u/ u/ min 1 max clkfreq 1000000 u/ u* cnt COG@ + begin dup cnt COG@ - 0< until drop ; \ pticks (pin -- usecs) - I/O PIN connected to PING))) is expected on the stack, usec for one-way trip \ to object detected by PING))) is returned on stack : pticks dup pinout \ set the pin for output and bring low for 1ms dup pinlo 1 delms dup pinhi 3 delus \ send pulse > 2us on PING))) pin dup pinlo dup pinin \ switch the pin to input dup >m dup waitpeq \ and wait for PING))) to set it HIGH cnt COG@ swap \ grab the current CNT value (start time) (swap to keep pin on TOS) >m dup waitpne \ wait for the (create mask and then DUP the mask - don't need pin # anymore) cnt COG@ swap - \ grab the count value (end time) and calculate the difference (end - start) clkfreq 1000000 / / \ turn ticks into us 2 / \ divide by 2 since PING))) measured the round trip and we want \ the one-way trip ticks - leave useconds on the stack ; \ pingmm (pin -- distmm) - distance from PING))) to object in mm : pingmm pticks 10000 * tocms / ; \ pingcm (pin -- distcm) - distance from PING))) to object in cm : pingcm pingmm 10 / ; \ pingin (pin -- distin) - distance fromPING))) to object in inches : pingin pticks 1000 * toinches / ;