'{$STAMP BS2} '{$PBASIC 2.5} ' 2008 mlab.taik.fi/paja ' Sending data to a Serial port ' ---[ I/O Definitions ]--- pRX PIN 10 pTX PIN 11 pPing PIN 7 ' ---[ Variables ]--- wTime VAR WORD ' ---[ Constants ]--- cTrigger CON 5 ' trigger pulse = 10 uS for BS2 ' ---[ Initialization ]--- DEBUG CLS, "--- PING))) Ultrasonic Sensor", CR ' ---[ Main Code ]--- Main: DO GOSUB ReadPing wTime = wTime/10 SEROUT pTX, 84, [DEC5 wTime] DEBUG DEC wTime, CR PAUSE 100 LOOP END ' ---[ Subroutines ]--- ReadPing: ' PULSOUT Pin, Duration ' pin: Pin number (0-15) ' Duration: specifies the duration of the pulse. (BS2: a unit = 2us) PULSOUT pPing, cTrigger ' PULSIN Pin, State, Variable ' Pin: Pin number (0 - 15) ' State: Specifies whether the puse to be mesured is low(0) or high(1) ' Variable: the measured pulse duration will be stored. PULSIN pPing, 1, wTime RETURN