' ========================================================================= ' ' File...... PING.SXB ' Purpose... ' Author.... Jon Williams ' E-mail.... jwilliams@efx-tek.com ' Started... ' Updated... ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Conditional Compilation Symbols ' ------------------------------------------------------------------------- '{$DEFINE English} ' measure in inches ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- ID "Ping)))" DEVICE SX28, OSCXT2, BOR42 FREQ 20_000_000 ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- PingIO PIN RA.0 ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- distance VAR Word ' distance tmpB1 VAR Byte ' for subs/funcs tmpB2 VAR Byte tmpW1 VAR Word ' ------------------------------------------------------------------------- ' Subroutine / Function / Task Declarations ' ------------------------------------------------------------------------- GET_DISTANCE FUNC 1, 0 ' read from Ping))) DELAY_MS SUB 2, 2, Word ' shell for PAUSE DELAY_US SUB 2, 2, Word ' shell for PAUSEUS ' ========================================================================= PROGRAM Start ' ========================================================================= Start: PLP_A = %0001 PLP_B = %0000_0000 PLP_C = %0000_0000 Main: distance = GET_DISTANCE WATCH distance, 8, UDEC ' use Debug --> Run BREAK DELAY_MS 100 GOTO Main ' ------------------------------------------------------------------------- ' Subroutine / Function / Task Code ' ------------------------------------------------------------------------- ' Use: result = GET_DISTANCE ' -- reads distance from Ping))) sensor ' -- units based on Conditional Compilation directive FUNC GET_DISTANCE result VAR tmpW1 HIGH PingIO ' "ping" the sensor DELAY_US 10 PingIO = 0 DELAY_US 5 ' let output settle INPUT PingIO ' setup for return pulse result = 0 ' clear return value \ JNB PingIO, $ ' wait for leading edge DO ' time the echo INC result '{$IFDEF English} PAUSEUS 73.746 ' measure inches '{$ELSE} PAUSEUS 29.034 ' measure centimeters '{$ENDIF} LOOP UNTIL PingIO = 0 ' stop on falling edge result = result >> 1 ' remove return trip RETURN result ENDFUNC ' ------------------------------------------------------------------------- ' Use: DELAY_MS duration ' -- shell for PAUSE SUB DELAY_MS '{$IFUSED DELAY_MS} PAUSE __wparam12 '{$ENDIF} ENDSUB ' ------------------------------------------------------------------------- ' Use: DELAY_US duration ' -- shell for PAUSEUS SUB DELAY_US '{$IFUSED DELAY_US} PAUSEUS __wparam12 '{$ENDIF} ENDSUB ' ========================================================================= ' User Data ' =========================================================================