circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

Issue 150 January 2003
The PSoC RangeFinder
A Simple Ultrasonic Distance Meter


THE HARDWARE

The RangeFinder’s circuitry is quite simple (see Figure 3). The most important part is U1, which is a CY8C26443 PSoC microcontroller. U1 does all of the work with its internal analog and digital blocks.

(Click here to enlarge)

Figure 3—Building the circuitry for the PSoC RangeFinder isn’t complicated . All of the functions are concentrated in the C48C26443 PSoC microcontroller, which is clearly the most important part of the design.

Two capacitors, C1 and C2, suppress high- and low-frequency noise on the 5-V supply line. R1 is a 100-kW resistor that holds the DC input voltage of the receiving stage to AGND (2, 5 V). R2 regulates the LCD contrast. I selected this value for a good contrast in the LCDs used in prototypes. It can be changed to adjust the contrast using different LCD modules. In addition, a 10-kW trimmer with the wiper to the contrast input and other pins connected to 5 V and GND can replace it. This allows for decent LCD contrast regulation.

Without an LCD, the circuit draws approximately 23 mA from a 5-V power supply. If you use an LCD, the current consumption is 25 mA. The optional LCD is a standard 2 × 16 model.

THE SOFTWARE

Figure 4 is a flowchart depicting the microcontroller’s software. The main program sets up the analog and digital blocks before testing JP1 to determine Normal or Calibration mode. If JP1 is shorted, the control program runs the Normal mode procedure. Otherwise, the calibration routine is executed.

(Click here to enlarge)

Figure 4—A flowchart will help you understand the software associated with this project. Note that there are three relevant routines: the main program, the calibration routine, and the TimeBase 8-bit counter interrupt subroutine.

In Normal mode, the software continuously runs the transmitted ultrasonic burst (ping). After a blanking time, it waits for the returned ultrasonic signal (pong). The time between the start of a transmitted burst and the start of a received burst is proportional to the distance between the RangeFinder and the obstacle. By polling the comparator bus register, the software measures this time and stores it in a RAM location. Finally, the range value is written to the LCD (if present), sent to the serial interface, and the PWM duty cycle is set to a value that’s proportional to the distance.

The software in Calibration mode is similar to that in Normal mode; however, the measured value is compared with the constant value 50, and the resultant offset is stored in nonvolatile EEPROM and used to calculate the measured range in Normal mode.

TimeBase_int is the interrupt subroutine for the TimeBase 8-bit counter. This is the most important portion of code (see Listing 1). When time1 is greater than the value of blank time (blank time prevents false echoes caused by the lateral receiving of transmitted 40-kHz bursts), the software tests the logical value of the comparator. If a pong is received, the comparator output logic level is one, the time1 value is stored in RAM location range, and the TimeBase interrupt is disabled. As a result, the value stored in the range location represents the measured distance.

If the comparator logic level output is equal to zero, then the PGA_1 gain is dynamically incremented in 16 steps from one to 16 by modifying the corresponding gain register; therefore, the far echoes are much more amplified.

As you can see in Table 1, the PGA_1 gain increment is not linear. This is not a problem, because more amplification is required for long distances. As you can see in Listing 1, the PGA_1_SetGain routine is called to change the amplifier gain and the value stored in the relative configuration register.