Make your own free website on Tripod.com
 

This is from an article in BYTE magazine.  Steve Ciarcas circuit cellar. 
There are definite typing errors in the below text (introduced by me 
from typing it in), so it may be inaccurate and may not work.

1 REM APPLE II VERSION RANGING SYSTEM
10 POKE -19293,0
15 :::REM SHUT OFF INIT LINE
30 PRINT CHR$(4);"BLOAD RANGE.0"
35 :::REM LOAD OBJECT PROGRAM
40 POKE 12328,0   ($3028)
45 :::REM CLEAR COUNT
50 CALL 12288
55 :::REM CALL RANGING SUBROUTINE
60 C=PEEK(12328)
65 :::REM GET COUNTER VALUE
70 F=0:I=0:O=0
75 :::REM CLEAR FEET,INCHES,OVERFLOW
80 IF C=255 THEN O=1:GOTO 120
85 :::REM SET OVERFLOW IF COUNT = $FF
90 F=INT(C/4)
95 :::REM CALCULATE FEET
100 I=(C-(F*4))*3
105 :::REM AND INCHES
120 REM APPLICATION CODE STARTS HERE
130 IF O=1 THEN PRINT "OVERFLOW":GOTO 40
140 PRINT F;" FEET ";I;" INCHES"
150 PRINT C;" .455-ms PERIODS":PRINT
160 GOTO 40
165 :::REM AND MAKE ANOTHER PASS


      ORG $3000
RANGE EQ  *
      LDA $C05B  ;TURN ON INIT LINE 49243
DELAY EQU *
      LDA #184   ;SET DELAY COUNT TO 184
LOOP EQU *
      ADC #1     ;ADD 1 TO DELAY COUNTER
      NOP        ;NO OPERATION FOR TIMING
      BCC LOOP   ;LOOP UNTIL CARRY
      CLC        ;CLEAR CARRY FLAG
      LDA COUNT  ;PICK UP OLD COUNT
      ADC #1     ;ADD 1 TO IT
      STA COUNT  ;AND SAVE IT
      BCC ECHO   ;IF NO WRAP CHECK ECHO

                 ;WRAP OF PERIOD HAS OCCURRED, SET OVERFLOW
      LDA #$FF   ;GET A $FF
      STA COUNT  ;WRITE TO COUNTER
      JMP EXIT   ;AND LEAVE THIS ROUTINE
ECHO  EQU *
      LDA $C061  ;PICK UP ECHO BYTE
      AND #$80   ;TEST ECHO BIT
      BEQ DELAY  ;IF NO ECHO, DO ANOTHER LOOP
EXIT  EQU *
      LDA $C05A  ;SHUT OFF INIT LINE
      RTS        ;RETURN TO BASIC

*----------------------------------------
COUNT .BS $1 ;COUNTER DATA BYTE
*----------------------------------
*----------------------------------
*END OF RANGE SUBROUTINE
*----------------------------------
ZZZZ .EQ *

Apple version ranging system

A 1.78-ms period indicates a round trip of 1 foot.  Maximum range assumed for programming purposes to be 40 feet.  Counter for periods is 8 bits wide, yielding 255 counts.  Each Period count will represent 1/4 foot or 3 inches.  Valid range counts are 1 to 254.  A count of 255 indicates overflow.  1.78 ms/4 = 0.455-ms period.

Apple program loop-time calculation
System Clock = 1.023X10^5 Hz
1 machine cycle = 1 clock cycle

Code-period calculation:
for every execution of the delay loop, overflow, and echo test:
number of cycles is (2+2+4+2+2+4+2+4+1+2)+((2+1+2)*(256-170)
           = 25+(5*86)
           =455 cycles

455*(1/1.023 X 10^5)= 0.44477028 ms

Apple program uses game I/O connector for I/O
AN1=INIT(OUT)
SW1=ECHO(IN)
AN2=ALARM-activated output(OUT)