Building a Sonar System
overrode the 20 MHz crystal you see in Schematic 2
with the PIC18F2620’s internal oscillator.
When the rising edge of the PW ranging signal triggers
an interrupt, we immediately read the value of the CCP1
holding registers and preserve that value in the pulsestart
variable. The next step we perform is to configure the CCP1
input to trigger on a falling edge. Meanwhile, the TIMER1
clock is still counting. When the falling edge occurs,
another interrupt is generated and we preserve the
contents of the CCP1 holding registers in the pulseend
variable.
The pulse width is the difference in the value of the
pulseend variable and the pulsestart variable. Since the
pulsewidth value is in microseconds, we can easily
determine the target distance in inches by dividing the
pulsewidth value by 147. Here’s a look at the PW pulse
capture interrupt handler code:
void interrupt MEASURE(void)
{
if(CCP1IE && CCP1IF)
{
//interrupt caused by rising edge of PW
if(flags.rising_edge)
{
//save start count
pulsestart = make16(CCPR1H,CCPR1L);
CCP1CON = 0b00000100;
//capture falling edge
flags.rising_edge = 0;
//setup for falling edge
}
else
//interrupt caused by falling edge of PW
{
//save end count
pulseend = make16(CCPR1H,CCPR1L);
+6VDC to +9VDC
LM2940
VR1
5.0V LM3940
VR2
5.0V
3.3V
J1
C1 +
.1uF C2
220uF
IN OUT
GND
C3
220uF
+ C4
.1uF
IN OUT
GND
PIC VOLTAGE SELECT JUMPER
R1 + 3.3V
390
C5
220uF
+ C6
.1uF
JP2
VCC
LED1
+ 5.0V
VCC
ICSP CONNECTOR
VCC
+ 5.0V
63
56 32
45 21
41
R3
10K
C8
.1uF
OPTIONAL
390
LV-Max Sonar-EZ0
R1
100
C7
.1uF
OPTIONAL
ACTIVITY LED
BW
PW
AN
RX
TX
VCC
GND
R2 1K
U2
7
6
5
4
3
2
1
1
MCLR
2 AN
RA0 3
RA1 4
RA2 5
RA3 6
RA4/T0CKI 7
RA5 10
OSC2/RA6
C11 20pF
Y1 20 MHz
28
RB7/PGD
27 9
RB6/PGD OSC1/RA7 21 RX
RB0 22 BW
RB1 23
RB2 24
RB3 25
RB4 26
RB5 11
VCC RC0 12
RC1/CCP2 13 PW
20 RC2/CCP1 14
C9 VDD RC3 15
RC4 16
.1uF 8 RC5 17
19 GND RC6/TX 18 RX
GND RC7/RX
C12 20pF
VCC
R5
10K
3
1 BC846B
PIC18F2620/PIC18LF2620
SCHEMATIC 2. As you have come to expect, there’s no rocket science
in my PIC design. All of the advanced hardware and firmware resides
within the confines of the LV-MaxSonar-EZ0 ultrasonic rangefinder.
46 SERVO 06.2008
R4 1K
Q1
2