Building a Sonar System
SCREENSHOT 3. The analogdistance value is actually the number of 4.858 mV
steps measured by the PIC18F2620’s analog-to-digital converter subsystem.
To get the distance, we must multiply the number of steps and the voltage per
step values and divide the product by the AN scaling factor.
48 SERVO 06.2008
SCREENSHOT 2. The proof is in the
pudding. The distance value matches our
CleverScope observation.
disable_GLOBALint;
RX = 0;
//disable
//interrupts
//cease ranging
//compute pulsewidth and distance
//in inches
pulsewidth = pulseend - pulsestart;
// in ms
distance = pulsewidth / 147;
//147us = 1”
flags.captured = 1;
reset_TIMER1();
} while(1);
//do it all
//again
The result of running the PW code
through a ranging cycle is captured in
Screenshot 2. Enough said.
Ranging with an Analog-to-Digital Converter
I put my Meterman DM73B multimeter
leads across the + 5 volt power supply of
the rangefinder support hardware you see
in Photo 3. My + 5 volt power supply is
actually producing + 4.97 VDC. Using the
AN portal scaling factor (Vcc/512) and my
actual power supply voltage ( 4.97 volts)
yields a conversion factor of 9.707 mV per
inch. With an A-to-D reference voltage
equal to the power supply voltage, the
PIC18F2620’s A-to-D step voltage works
out to 4.858 mV per step.
To get the ranging information from
the EZ0’s AN output pin into the
PIC18F2620, I attached it to the
PIC18F2620’s RA0 analog input. To obtain
the target distance, I sprinkled in a bit of
A-to-D code onto our PW code:
do{
GODONE = 1; //trigger an AD
//conversion
while(GODONE); //wait for the
//conversion to
//end
} while(!flags.captured);
Rather than just spin around doing
SCREENSHOT 4. It doesn’t get much easier
than this. All I had to do was add a transistor
and some PIC18F2620 EUSART code to get
this result.