'Sonar Pinger ************************************************************ ' ' This program uses a SUN 660 sonar board connected to the BOTBoard 2. ' Init = PD5 ' BINH = PD4 ' POWER= PD3 ' ECHO = PA1 (IC3) ' Uses no interrupts, but does use IC3 to catch the pulse width. ' Uses Marvin Green's LCD driver to $7000 for the print routines. ' DLC rev 9/4/2000 Copywrite Dennis Clark 9/2000, more or less. include "regs11.lib" 'Always initialize your variables! declare ptime =0 declare sttime =0 declare pdstart =0 declare pdout =0 declare n = 0 Ping: sttime= peek(tcnt) 'get clock time do ptime=peek(tcnt) loop until ptime-sttime >* 50000 '25ms delay pokeb portd, pdstart AND %11110111 'clr bit Power sttime= peek(tcnt) 'get clock time do ptime=peek(tcnt) loop until ptime-sttime > 15000 '5ms delay pokeb tflg1, %00000001 'clear flag bit pokeb portd, %00100000 'set bit INIT 'without special hacks, the BINH function never works. 'But, leave it in, the timing's good and you might get it to work... sttime= peek(tcnt) 'get clock time do ptime=peek(tcnt) loop until ptime-sttime > 1000 '500us delay pokeb portd, %00110000 'set bit BINH sttime = peek(tcnt) 'get start time 'Now we can leave really and look any time we want... waituntil tflg1,$01 'tflg1,IC3F set pokeb portd,pdstart 'reset SONAR board ptime = peek(tic3) - sttime 'get trigger time ptime = ptime + 1020 'about 500us code offset return main: 'configure port D outputs pokeb ddrd,%00111000 'PD3,4,5 outputs 'set initial port D conditions, power high,BINH,INIT low pdstart = %00001000 'get initial port D settings pokeb portd,pdstart 'configure IC registers for no interrupt and positive edge on IC3 pokeb tctl2,%0000001 'trigger on positive edge pokeb tmsk1, %00000000 'no interrupt 'wait for power up on LCD to be done for sttime = 1 to* 60000 next outch $10 outch $01 print "SONAR Tests. " for sttime = 1 to* 60000 next do gosub Ping outch($02) ' home cursor 'each tic is 500ns, sound travels about 148us/in round trip 'so 148us/500ns = about 297, so tics/297 = approx dist in inches printu ptime;"= ";(ptime/297);"inches";" "; for n=1 to* 65000 next loop end