AN597 Implementing Ultrasonic Ranging Author: THEORY OF OPERATION Robert Schreiber Logic Products Division Ultrasonic ranging entails transmitting a sound wave and measuring the time that it takes for the sound wave to reflect off of an object and back to the origin. The reflection time is proportional to the distance that the object is from the source. In this implementation, the sound wave is transmitted and received from the same transducer. Therefore, a blanking interval is required between signal transmission and reception to eliminate false echoes (i.e., a transmitted signal being detected as its own echo). INTRODUCTION Object ranging is essential in many types of systems. One of the most popular ranging techniques is ultrasonic ranging. Ultrasonic ranging is used in a wide variety of applications including: • • • • • Auto focus cameras Motion detection Robotics guidance Proximity sensing Object ranging CIRCUIT CONFIGURATION In this implementation, a PIC16C74 is connected to the ranging module as shown in Figure 1. The RE0 and RE1 I/O pins are configured as digital outputs and are tied to INIT and BINH, respectively. The CCP1 pin is configured as a digital input and is tied to ECHO through a pull-up resistor. The pull-up resistor is needed since the ECHO signal is an open-collector output. The CCP1 pin is configured for capture mode (CCP1CON). Figure 2 shows the timing relationship for VDD and the three signal lines (INIT, BINH, and ECHO). This application note describes a method of interfacing PIC16CXX microcontrollers to the Polaroid 6500 Ranging Module. This implementation uses a minimum of microcontroller resources, a CCP module and two I/O pins. The two major components of the system are: • Microcontroller • Polaroid 6500 Ranging Module Note: The microcontroller performs the intelligence and arithmetic functions for ultrasonic ranging, while the Polaroid 6500 Ranging Module performs the ultrasonic signal transmissions and echo detection. FIGURE 1: The ranging module requires 5.0 milliseconds to stabilize during power-up. RANGING MODULE INTERFACE RE1 BINH RE0 INIT TRANSMIT ECHO POLAROID 6500 RANGING MODULE CCP1 TRANSDUCER PIC16C74 1995 Microchip Technology Inc. DS00597A-page 1 Thi d t t d ith F M k 404 AN597 FIGURE 2: TIMING DIAGRAM OF RANGING MODULE CONTROL LINES VDD INIT 5 4 1 2 2 BINH 3 3 ECHO Parameter Number Symbol 1 2 3 4 5 Tpu TBINH TECHO TINIT_H TINIT_L Characteristic Ranging Module Stabilization Time Blank Inhibit Time Echo Time High Time for INIT Low Time for INIT The PIC16C74 is configured to use one of its internal timers, Timer1, in capture mode to measure the time between signal transmission and echo detection. The resolution of the timer is determined by the microcontroller clock frequency. For this application, a 4 MHz external oscillator was used, giving a resolution of 1 ms per bit. The PIC16C74 initiates a ranging cycle by first clearing Timer1. Timer1 is then enabled and INIT is immediately asserted on the ranging module. When INIT is asserted, the ranging module transmits a series of 16 pulses on the transducer at 49.4 kHz. The transmitted pulses reflect off the object and are received back at the transducer. The transducer is used for both transmitting and receiving sound waves. A blanking interval is needed to ensure that the transmitted signal has decayed on the transducer, in order not to receive false echoes. In normal operation, the ranging module has a blanking interval of 2.38 milliseconds, which corresponds to a minimum detection distance of approximately 17 inches. However, the BINH (blank inhibit) signal can be manipulated to reduce the blanking time on the transducer to allow for object ranging as close as 6 inches. In this implementation, the PIC16C74 asserts the BINH signal approximately 0.9 milliseconds after signal transmission. This enables the transducer to receive reflections off objects at a distance of 6 inches. The ranging module asserts the ECHO signal when a valid reflection has been detected. The PIC16C74 uses the ECHO signal to trigger a capture of the Timer1 value. The capture register contains the 16-bit value DS00597A-page 2 Min Typ Max Units 5.0 0.9 – 100 100 – 2.38 – – – – – – – – ms ms – ms ms representing the elapsed time between signal transmission and echo detection. The PIC16C74 then calculates object distance based on the Timer1 value, microcontroller clock speed, and the velocity of sound in the atmosphere. The basic equation for calculating distance is given below: Distance (inches) = TECHO time / 147.9 microseconds Note: The minimum high and low time for INIT is 100 milliseconds, as seen in Figure 2. DESIGN CONSIDERATIONS There are several design considerations which must be taken into account and are listed below. The absolute measuring distance supported by the ranging module is 6 inches to 35 feet with an accuracy of +/- 1%. The distance output from the ranging module can be averaged over time to filter distance calculations. In some applications, the gain of the receiver amplifier may be too low or too high and may need to be adjusted. For example, if the transducer is mounted in a cylinder, the gain may need to be lowered to reduce false echoes within the cylinder. In this case, R1 (refer to the Polariod Ultrasonic Ranging System manual) may be replaced with a 20 kΩ potentiometer to tweak the gain of the receiver amplifier to reduce false echoes. In order for the Polaroid 6500 ranging module to operate properly, the power supply must be capable of handling high current transients (2.5 A) during the 1995 Microchip Technology Inc. AN597 transmit pulse. The instantaneous drain on the power supply can be mitigated by installing a storage capacitor across the power lines at the ranging module. A value of 500 microfarads is recommended. A 200 millisecond interval is recommended between ranging cycles (Figure 2) to allow the transducer to clear. The ECHO line requires a pull-up resistor (4.7 kΩ was used in this application). There must be a common ground between the PIC16C74 circuitry and the ranging module. Some applications may not need the resources of the higher end PIC16CXX devices. It is still possible to do this application using a device that does not contain a CCP module (for ECHO timing). The capture function can be implemented in firmware. The effect of a firmware implementation is that the resolution of the ECHO time would be 3 Tcy cycles versus 1 Tcy cycle for the CCP module. Also, the firmware implementation would not allow other tasks to be performed while the capture function was occurring. Refer to Appendix A for general ranging module specifications. 1995 Microchip Technology Inc. DS00597A-page 3 AN597 APPENDIX A: POLAROID MODULE SPECIFICATIONS Note: This appendix contains general specifications from the Polaroid Ultrasonic Ranging System Manual. Please refer to the current Polaroid Ultrasonic Ranging System Manual for current information regarding ranging module design considerations. Resolution a) b) c) d) Accuracy: (again, you must have a well defined target) Temperature Compensate DESIGN CONSIDERATIONS IN ULTRASONICS a) Range: (with user custom designed processing electronics) Farther a) b) c) Above all, know the target and range well, and design a system with them in mind. Use a higher transmit frequency. Look at phase differences of a given cycle of the transmitted signal and received echo (as opposed to using and integration technique). Increase the clock frequency of the timer. b) Use an acoustic horn to “focus” the sound (narrowing the beamwidth). Use two transducers – 1 receiver and 1 transmitter – facing each other. Lower the transmitting frequency (which will decrease the attenuation in air). c) Use a second small target, as a reference, at a known distance in the ranging path (such as a 1/4” rod several feet away), process both echoes, then normalize the second distance with respect to the first, since t1/d1 = t2/d2. Incorporate a temperature sensing integrated circuit to drive a VCO to do the distance interval clocking. To increase sensitivity of detection circuit change the value of C4 from 3300 pF to 1000 pF on the 6500 Series Ranging Module. Beam Width: Closer Increase a) a) b) c) a) Use a shorter transmit signal (such as four cycles). Use two transducers – one to transmit, one to receive (eliminates waiting for damping time). Use an acoustic lens (to disperse the signal). Decrease the transmitting frequency. Use several transducers to span an area. Decrease a) b) TABLE 1: Use an acoustic horn (to focus the sound). Increase the transmitting frequency. RECOMMENDED OPERATING CONDITIONS Supply Voltage, VCC High-level input voltage, VIH BINH, INIT Low-level input voltage, VIL BINH, INIT Min. Max. Unit 4.5 6.8 V 2.1 ECHO and OSC output voltage V 0.6 V 6.8 V Delay time, power up to INIT high 5 ms Recycle period 80 ms Operating free-air temperature, TA 0 DS00597A-page 4 40 °C 1995 Microchip Technology Inc. AN597 TABLE 2: ELECTRICAL CHARACTERISTICS OVER RECOMMENDED RANGES OF SUPPLY VOLTAGE AND OPERATING FREE-AIR TEMPERATURE (UNLESS OTHERWISE NOTED) Parameter Test Conditions Min. Typ. Max. Unit Input current BINH, INIT V1 = 2.1V 1 mA High-level output current, IOH ECHO, OSC VOH = 5.5V 100 µA Low-level output voltage, VOL ECHO, OSC IOL = 1.6 mA 0.4 V Transducer bias voltage TA = 25°C 200 V Transducer output voltage (peak-to-peak) TA = 25°C 400 V Number of cycles for XDCR output to reach 400V C= 500 pF Internal blanking interval 7 2.38* Frequency during 16-pulse transmit period OSC output 49.4* XMIT output 49.4* Frequency after 16 pulse transmit period OSC output 93.3* XMIT output 0 ms kHz kHz During transmit period 2000 After transmit period 100 mA Supply current, ICC * These typical values apply for a 420 kHz ceramic resonator. 1995 Microchip Technology Inc. DS00597A-page 5 AN597 Please check the Microchip BBS for the latest version of the source code. For BBS access information, see Section 6, Microchip Bulletin Board Service information, page 6-3. APPENDIX B: FIRMWARE LISTING MPASM 01.02 Released XDCR.ASM 11-14-1994 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0000 0190 0001 3005 0002 0097 0003 0004 0005 0006 0007 0185 0186 0187 0188 0189 0008 1683 0009 3007 000A 009F 000B 000C 000D 000E 000F 0010 0011 0012 0185 0186 3004 0087 0188 0189 1283 0012 1010 0013 018C 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0029 0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 0064 0065 0066 0067 0068 DS00597A-page 6 ; ; ; ; ; ; ; ; ; ; ; ; ; ; 9:29:15 PAGE 1 XDCR.ASM This routine continually executes ranging cycles in the following order: 1) 2) 3) 4) Timers and Flags are cleared Ranging Cycle Executes Distance is Calculated (to 0.5 inch) HW is re-initialized for next cycle The processor uses a 4MHz oscillator, so all timing calculations are referenced to that. The calculated distance is a 16-bit result in the ACCbHI:ACCbLO registers. LIST P=16C74, F=INHX8M ; ;****************** ; Bank 0 Registers ;****************** ; ; TMR1 is off, Prescaler is 1 for a capture timeout of 65 msec clrf T1CON ; Set to capture on every rising edge movlw 0x05 movwf CCP1CON ; Clear the Ports clrf PORT_A clrf PORT_B clrf PORT_C clrf PORT_D clrf PORT_E ; ;****************** ; Bank 1 Registers ;****************** ; bsf STATUS,RP0 ; Set RP0 ; Port A is Digital, Port E is Digital movlw 0x07 movwf ADCON1 ; Configure CCP1 (RC2) as an input, and all other ports ; as Outputs, (RE0 = INIT, RE1 = BINH) clrf TRIS_A clrf TRIS_B movlw 0x04 movwf TRIS_C clrf TRIS_D clrf TRIS_E bcf STATUS,RP0 ; Clear RP0 Xdcr ; ; Initialize Timers and Flags ; bcf T1CON,0 ; Disable TMR1 clrf PIR1 ; Clear Timer1 Overflow Flag & Timer1 Capture Flag 1995 Microchip Technology Inc. AN597 0014 0015 0016 0017 0018 0019 001A 001B 001C 001C 001D 001E 001F 0020 0021 0022 018E 018F 0195 0196 1409 1410 21F3 1489 0069 clrf TMR1L ; Clear TMR1L 0070 clrf TMR1H ; Clear TMR1H 0071 clrf CCPR1L ; Clear CCPR1L 0072 clrf CCPR1H ; Clear CCPR1H 0073 bsf PORT_E,0 ; Set INIT High on Ranging Module 0074 bsf T1CON,0 ; Enable TMR1 0075 call DEL_9 ; Delay 0.9 msec for transducer to stabilize 0076 bsf PORT_E,1 ; Enable Transducer to Receive (BINH) 0077 chk_t1 190C 0078 btfsc PIR1,2 ; Check for Capture 2822 0079 goto chk_done ; Jump if Capture 1C0C 0080 btfss PIR1,0 ; Check for TMR1 Overflow 281C 0081 goto chk_t1 ; Loop if nothing happened 1010 0082 bcf T1CON,0 ; Turn off TMR1 2833 0083 goto ovr_flo ; Capture event did not occur 0084 chk_done 0085 ; 0086 ; Calculate distance to 0.5 inch resolution 0087 ; 0022 1010 0088 bcf T1CON,0 ; Turn off TMR1 0023 0815 0089 movf CCPR1L,W ; Move LSB into W 0024 00A2 0090 movwf ACCbLO ; Move LSB into ACCbLO 0025 0816 0091 movf CCPR1H,W ; Move MSB into W 0026 00A3 0092 movwf ACCbHI ; Move MSB into ACCbHI 0027 304A 0093 movlw 0x4A ; Move 75usec/0.50in into W 0028 00A0 0094 movwf ACCaLO ; Move LSB into ACCaLO 0029 01A1 0095 clrf ACCaHI ; Clear MSB (ACCaHI) 002A 208F 0096 call D_divF ; Call 16-bit/8-bit routine 0097 ; which is described in 0098 ; Application Note 544 002B 3025 0099 movlw 0x25 ; Check remainder to see if 002C 0224 0100 subwf ACCcLO,W ; we should round up... 002D 1803 0101 btfsc STATUS,CARRY ; If Remainder < (0.5 * Divisor), skip 002E 0AA2 0102 incf ACCbLO,F ; Round up 002F 1903 0103 btfsc STATUS,Z ; Check low byte for wrap around 0030 0AA3 0104 incf ACCbHI,F ; If LSB wrapped, increment high byte 0031 1D03 0105 btfss STATUS,Z ; Check high byte for wrap around 0032 2835 0106 goto done ; High byte didn't wrap 0033 0107 ovr_flo 0033 01A2 0108 clrf ACCbLO 0034 01A3 0109 clrf ACCbHI 0035 0110 done 0035 21FD 0111 call DEL_100 ; Wait 100 msec before clearing HW. 0036 1009 0112 bcf PORT_E,0 ; Disable INIT 0037 1089 0113 bcf PORT_E,1 ; Disable BINH 0038 21FD 0114 call DEL_100 ; Wait 100 msec before enabling HW. 0039 2812 0115 goto Xdcr 0116 0120 0149 0150 end 0151 MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXX-----0040 : ---------------- ---------------- ---------------- ---------------All other memory blocks unused. Errors : 0 Warnings : 0 Messages : 0 1995 Microchip Technology Inc. DS00597A-page 7 WORLDWIDE SALES & SERVICE AMERICAS AMERICAS (continued) EUROPE Corporate Office Microchip Technology Inc. 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 602 786-7200 Fax: 602 786-7277 Atlanta Microchip Technology Inc. 500 Sugar Mill Road, Suite 200B Atlanta, GA 30350 Tel: 404 640-0034 Fax: 404 640-0307 Boston Microchip Technology Inc. Five The Mountain Road, Suite 120 Framingham, MA 01701 Tel: 508 820-3334 Fax: 508 820-4326 After July 31, 1995: Tel: 508 480-9990 Fax: 508 420-8575 Chicago Microchip Technology Inc. 333 Pierce Road, Suite 180 Itasca, IL 60143 Tel: 708 285-0071 Fax: 708 285-0075 Dallas Microchip Technology Inc. 14651 Dallas Parkway, Suite 816 Dallas, TX 75240-8809 Tel: 214 991-7177 Fax: 214 991-8588 Dayton Microchip Technology Inc. 35 Rockridge Road Englewood, OH 45322 Tel: 513 832-2543 Fax: 513 832-2841 Los Angeles Microchip Technology Inc. 18201 Von Karman, Suite 455 Irvine, CA 92715 Tel: 714 263-1888 Fax: 714 263-1338 New York Microchip Technology Inc. 150 Motor Parkway, Suite 416 Hauppauge, NY 11788 Tel: 516 273-5305 Fax: 516 273-5335 San Jose Microchip Technology Inc. 2107 North First Street, Suite 590 San Jose, CA 95131 Tel: 408 436-7950 Fax: 408 436-7955 United Kingdom Arizona Microchip Technology Ltd. Unit 6, The Courtyard Meadow Bank, Furlong Road Bourne End, Buckinghamshire SL8 5AJ Tel: 44 0 1628 851077 Fax: 44 0 1628 850259 France Arizona Microchip Technology SARL 2 Rue du Buisson aux Fraises 91300 Massy - France Tel: 33 1 69 53 63 20 Fax: 33 1 69 30 90 79 Germany Arizona Microchip Technology GmbH Gustav-Heinemann-Ring 125 D-81739 Muenchen, Germany Tel: 49 89 627 144 0 Fax: 49 89 627 144 44 Italy Arizona Microchip Technology SRL Centro Direzionale Colleoni Palazzo Pegaso Ingresso No. 2 Via Paracelso 23, 20041 Agrate Brianza (MI) Italy Tel: 39 039 689 9939 Fax: 39 039 689 9883 ASIA/PACIFIC Hong Kong Microchip Technology Inc. Unit No. 3002-3004, Tower 1 Metroplaza 223 Hing Fong Road Kwai Fong, N.T. Hong Kong Tel: 852 2 401 1200 Fax: 852 2 401 3431 Korea Microchip Technology Korea 168-1, Youngbo Bldg. 3 Floor Samsung-Dong, Kangnam-Ku, Seoul, Korea Tel: 82 2 554 7200 Fax: 82 2 558 5934 Singapore Microchip Technology Inc. 200 Middle Road #10-03 Prime Centre Singapore 0718 Tel: 65 334 8870 Fax: 65 334 8850 Taiwan Microchip Technology Taiwan 10F-1C 207 Tung Hua North Road Taipei, Taiwan, ROC Tel: 886 2 717 7175 Fax: 886 2 545 0139 JAPAN Microchip Technology Intl. Inc. Benex S-1 6F 3-18-20, Shin Yokohama Kohoku-Ku, Yokohama Kanagawa 222 Japan Tel: 81 45 471 6166 Fax: 81 45 471 6122 Printed in the USA, 6/6/95 1995, Microchip Technology Inc. *Information contained in this publication regarding device applications and the like is intended by way of suggestion only. No representation of warranty is given and no liability is assumed by Microchip Technology Inc. with respect to the accuracy or use of such information.Use of Microchip’s products as critical components in life support systems is not authorized except with express written approval by Microchip.The Microchip logo and name are trademarks of MicrochipTechnology Incorporated.All rights reserved.All other trademarks mentioned herein are the property of their respective companies. DS00597A-page 8 1995 Microchip Technology Inc.