[Menu]>[Guide to use the PIC]>[Circuits Gallery]>[Ultrasonic Range Meter]
This range meter detects a reflected wave from the object after sending out a ultrasonic pulse.
By measuring the time which returns after emitting a sound wave, a distance to the object is measured. The operation of the following figure is repeatedly executed. LED display processing is executed in parallel with this operation. Label definition ;**************** Label Definition ******************** cblock h'20' s_count ;Send-out pulse count adr endc
When using this directive, workareas which were defined between CBLOCK and ENDC are automatically allocated in the order from the address which was specified by the operand of CBLOCK. It is convenient because it is possible to prevent the double allocation of the area. To confirm an allocated address, you confirm it by the assembly result. Lighting-up segment data for 7 segments LED are designated by EQU. The data from 0 to 9 is used for digital display. However, the 10th is used for the detection error display. At first, I make light up only center segment. But I turned off all segments because the display was confusing. The 11th is used for the interruption error display. It is for the debugging. Environment designating and others
The following specification is done as configuration word.
The result is 3F72h. Initialization process ;**************** Initial Process *********************
RC2/CCP1 of C port is set to the input mode because it is used for input of capture. Ultrasonic transmission period timer(TMR0) initialization
So, the time-out time of timer0 is about 65 milliseconds. Capture mode initialization
At the time of the initialization, it makes CCP1 OFF to prevent from a malfunction. A/D converter initialization
Because the higher rank side of the A/D converted result are used, the storage of the result is made right justification (ADFM=0). Because A porta are used for the output of the digit specification of the LED, ports except RA0/AN0 are made digital specification. LED display period timer(TMR2) initialization
The time-out of timer2 is about 10 milliseconds. The interruption eable bits of the capture and timer2 are set. Interruption initialization
The interruption of the capture and timer2 doesn't occur when not making peripheral device interruption possible. By this process, interruption operation is started. When the initialization process ends, the interruption is waited for. It executes same address repeatedly. Interruption process ;*************** Interruption Process *****************
When the kind of the interruption is unsettled, processing is stopped. To confirm illegal interruption with the actual circuit, an in-circuit simulator is needed. There is a way of resetting but it isn't improved because it does the same operation even if it resets. A global interruption enable bit (the GIE bit) is automatically cleared when the interruption occurs. So, the interruption never occurs while processing in the interruption. Illegal interruption process ;*************** Illegal interruption *****************
This process is the process for debugging and is usable with the other checking, too. Interruption ending process ;************ END of Interruption Process **************
In the interruption ending process, GIE bit is set by RETFIE instruction to enable interruption. Ultrasonic pulse sending-out process ;*************** Pulse send-out Process ****************
Clear the interruption display
I clear the count area of timer0 because of the sure operation. Check the detection error
Stop the reflected wave detector
Start the capture operation
Send-out the 40KHz pulse
Because 4-MHz clock is used at the circuit this time, the instruction execution time is 1 microsecond. So, correct 40KHz can not be sent out. When doing ON, OFF respectively with the 12 µ-sec, it is 41.7KHz. In case of 13 µ-sec, it is 38.5KHz. This time is adjusted by changing the number of the steps in the pulse sending-out processing. It is 0.5 milliseconds when sending out 20 pulses. (20pulses x 0.025milliseconds/pulse) Take-in the display revision data
Error detection prevention
Start the reflected wave detection
Capture interruption process ;****************** Capture Process ********************
Clear the interruption display
Distance conversion processing
For example, I will explaine in case to have been reflected from the 1-m distance. In the time that the sound wave goes and returns in 1 m, it is 2m/343m/sec=5831 microseconds in case of 20°C. Because the clock of timer0 is 1µ-sec/count, the value of the timer0 when a capture is done is 5831. It is to be OK if using 58 as the divisor to make display this value as 100(It displays 1 m). The value to take in by the A/D converter is used for a divisor. So, when the temperature is different, the display can be revised if changing the input voltage to the A/D converter. But, because it is changed in the digital, it isn't possible to do fine adjustment. Display setting processing
Binary number is converted into the decimal number and is set to each workarea(100th, 10th, 1st). When 100th digit exceeds 9, it makes error display. LED display process ;**************** LED display control *****************
One LED is controlled at each period. So, only 1 digit is displayed at the same time. Display is done every 10 milliseconds using timer2. When the display flickers, you should make the set value of timer2 more little and it quickens a period. |