Você está na página 1de 3

PIC Sonar Range Finder Hardware You can use any PIC microcontroller that has an internal CCP

module and enough memory to hold the program. You can program the PIC in circuit through the ICSP connector. The circuit uses a three transistor amplifier and a two transistor output driver (it does not use any special components so it can be constructed out of standard components). The comparator is an LM311 type which is also a standard component (you could use an opamp - with suitable circuit changes). PIC Sonar hardware block diagram

The first two transistor amplifiers use standard biasing to set the output at the collector in the middle of the supply. If you look at the dc conditions the two (100k) input bias resistors across 5V to 0V set the input bias point at 2.5V. When the Vbe voltage is dropped across the transistor's emitter junction the voltage at the emitter is Vbias - 0.6 (approx 2V). So the emitter current =2/2k2 ~ 1mA. Ic=Ie. So the dc bias point is 5V-IcRc 2.7k*1mA ~ 2.5V. The AC gain of these transistors is RC over RE (but at AC the capacitor has impedance of 40Ohms at 40kHz) so the effective Re is the intrinsic transistor emitter resistance (re~25ohms) plus the impedance of the capacitor (re is temperature dependent). So the gain is 2k7/65 ~ 40. If used at different temperatures you will get some gain variation. The last transistor uses fixed biasing to set the bias point. For a more stable amplifier (less affected by Beta variation) use the same amplifier as the other two. I have just used it to see how it works as it can be seen quite often in other circuits and it seems to work well. It will however be dependent on the exact transistor used (its Beta value) and it will also be dependent on temperature - which will both affect its bias point and gain. The comparator is setup as a standard circuit will a small amount of hysteresis (to stop oscillation if the input changes slightly) - the 1M ohm feeds back to set the hysteresis level. Setting up the PIC Sonar range finder: PIC Sonar Oscilloscope setup Using an oscilloscope monitor the signals RB3 and RB0. Use RB3 as the trigger as this is the signal that regularly generates the ultrasound. RB0 is the detected echo.

Set the output of the comparator (RB0) low by turning preset VR2 fully in one direction. Point the transducers at an object at about 1 metre away and turn the preset until a signal appears (at about 6ms after RB3). PIC Sonar Manual setup Set the output of the comparator (RB0) low by turning preset VR2 fully in one direction. Point the transducers at an object at about 1 metre away and turn the preset until a the display generates '100' (approx). Move the board back and forwards to check that it displays a larger and then smaller number. Check the longer distance e.g. point at the ceiling and then a closer object e.g. a wall 20-50cm away. Adjust the preset as necessary. Improvements to the PIC Sonar Range Finder You could remove the comparator and use the internal analogue comparator but this would require more software to set the comparator level. It would require 'Up' and 'Down' buttons to control the level settings. With a temperature sensor you could change the value used for the speed of sound (currently fixed at 340m/s for 20C operation i.e. indoors!). This would make the pic sonar range finder more accurate in different environments. PIC Sonar Range Finder Software Project files for the PIC sonar range finder Compiler project files 16F88_Ultrasonic_ranger.ppc C Source files. 16F88_Ultrasonic_ranger.c Header files. bit.h Output files 16F88_Ultrasonic_ranger.hex PIC Sonar Description 16F88_Ultrasonic_ranger.c This contains all the code except the bit manipulation routines found in bit.h. It enters a continuous a continuous loop calling ulta_gen - the routine that generates the ultrasound at 40kHz. The ultra_gen routine is set up using the simulator to set the timing of the output signal for a period of 25us (40kHz). This is then repeated every 40ms. The required refresh rate of the seven segment display is 20ms so the display update routine (seg_display_int) is called twice over the 40ms period. (I should really say that the display update routine takes 20ms and calling this twice creates the total 40ms delay). The display relies on persistence of vision to make it appear that the display is not flickering - a refresh rate of 50Hz or more does the job ( 1/50Hz = 20ms). In theory the maximum distance that you could measure is 40ms*340m = (13.6) 6.8m (half the round trip time delay ) but in practice this is limited by the signal conditioning circuits. If they were changed you could get more range.

If a capture occurs indicated by gCapInt then the DST calculation is performed and the value of variable val is updated. val is the value displayed by the seven segment display routine 'seg_display_int' so val is continuously refreshed to the seven segment display. PIC Sonar Interrupts The interrupt routine is only enabled when required and when the capture occurs (if it does) only the first capture is stored - so that later reflections are ignored (by resetting gCapOn). The first reflection should be the strongest and therefore the closest object. When captured the variables t_capL,t_capH and t_capO are set to the value of the capture register which will be the value of timer 1 when the capture module triggered. At the moment I have not used t_capO (and should do so) as it accounts for the roll over when timer 1 overflows. All that happens is that occasionally (when an overflow occurs) the wrong value will be generated - for hand held use it is not noticeable at all. A few more notes on the code operation 1. generate ultrasound at 40kHz (a few pulses of a square wave) to the TX 2. Turn on receiver. 3. Count time from end of 40kHz pulses to start of reception of reflected ultrasound 4. Ultrasound takes a set time to travel through air (varies mainly with temperature) 5. Apply D=SxT (Distance, Speed, Time) You know the time taken for the round trip the capture module is started at the end of the TX pulse this just counts pulses of timer1 until it receives an input when it does -the capture interrupt makes the program jump the interrupt routine where the current captured time is stored in: t_capL = CCPR1L; t_capH = CCPR1H; t_capO = T1_O; variable gCapInt = 1; // signal that a capture occurred. indicates to the main program that an echo was captured This is where the distance is calculated calc = ((s1)<<8)+s2; change to 16 bit number Multiply by 340 m/s to get cm divide by 100. divide by 2 to get half the complete round trip delay so calc * (340/100)/2 i.e. calc * 340/200 But I have 4 digits and only want to display on the right 3 so use another divide by 10 to shift the digits along 1 to the right. Final calculation is calc * (((340/100)/2)/10) the same as 340/2000 But you don't want to do this calculation all at once if using only integers as it will overflow so its split into 2 pieces calc = calc * 34; calc = calc / 2000; i.e. its the same as calc * 340/2000 but does not overflow the calc integer store. The variable val is assigned the calculated value and then automatically displayed on the 7segments.

Você também pode gostar