Sunteți pe pagina 1din 32

ULTRASONIC RANGE METER

BY
CHOR CIK HAH

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING


FACULTY OF ENGINEERING
TUNKU ABDUL RAHMAN UNIVERSITY COLLEGE
KUALA LUMPUR

2017/2018
ACKNOWLEDGEMENTS

I would like to express my appreciation to those who have given valuable aid and support
throughout the process of my project.

Firstly, I would like to express my sincerest gratitude to my project supervisor, Mr.


Kow Cik Choy who has been very supportive and always ready to help whenever problems
occur and provided numerous constructive ideas and solutions in order to overcome
technical problems.

I would also like to thank my friends, Sing Hah and Wai Tong, for giving me fresh
ideas, valuable suggestions and always being there to support me throughout the whole
process of my project

Special thanks also to my family members for being so supportive and patient with
me. Without them, this project may not have been completed is such a short period of time.

ii
ABSTRACT

Non-contact distance measurement is widely used especially in the manufacturing industry


to enable production and quality control, and may also be used as a simple and low cost
method for mobile robot navigation system in colission detection and avoidance. The
project proposes to measure the period between transmission and reception of sound waves
to determine distance. A microcontroller-based ultrasonic range meter is developed with
ultrasonic transducers for transmission and reception of ultrasonic pulses and display of
measured distance using seven-segment displays. The Range meter has a effective range of
3571 cm from 29cm to 3600cm with eight, and is suitable for ranging applications where
low-cost and simplicity is the primary concern with moderate requirements on resolution,
speed and accuracy.

iii
TABLE OF CONTENTS

ACKNOWLEDGEMENTS ................................................................................................................. ii
ABSTRACT..................................................................................................................................... iii
TABLE OF CONTENTS .................................................................................................................... iv
1.0 INTRODUCTION .......................................................................................................................1
1.1 Background ..........................................................................................................................1
1.2 Problem definition: ..............................................................................................................3
1.3 Project aims .........................................................................................................................4
1.4 Project objectives .................................................................................................................4
1.5 Report Outline......................................................................................................................5
2.0 PROJECT HARDWARE...............................................................................................................5
2.1 Circuit Drawing.....................................................................................................................5
2.2 Receiver Circuit ....................................................................................................................5
2.2.1 Signal amplification circuit .............................................................................................5
2.2.2 Detection circuit ............................................................................................................7
2.2.3 Signal detector...............................................................................................................8
2.2.4 Signal holding circuit......................................................................................................8
2.3 Transmitter Circuit ...............................................................................................................9
2.4 7-segment LED display circuit ...............................................................................................9
2.5 Temperature revision voltage generating circuit................................................................. 10
2.6 Resonator........................................................................................................................... 11
2.7 Power supply circuit ........................................................................................................... 11
3.0 PROJECT SOFTWARE .............................................................................................................. 12
3.1 Range Meter Operation ...................................................................................................... 12
3.2 Ultrasonic Pulse Sending Out Process ................................................................................. 12
3.3 Capture interruption process .............................................................................................. 14
4.0 TESTS, RESULTS AND DISCUSSION ......................................................................................... 14
4.1 Detection operation confirmation ...................................................................................... 15
4.2 Measuring range ................................................................................................................ 16
4.3 Temperature calibration feature ........................................................................................ 16
5.0 CONCLUSIONS AND RECOMMMENDATIONS ......................................................................... 17
5.1 Conclusions ........................................................................................................................ 17
5.2 Recommendations ............................................................................................................. 17

iv
REFERENCES ................................................................................................................................ 18
APPENDIX A: Source code file of Ultrasonic Range Meter ........................................................... 18
Appendix B – Student-Supervisor Log-Book ............................................................................... 25

v
1.0 INTRODUCTION

1.1 Background

Non-contact distance measurement is widely used especially in the manufacturing industry


to enable production and quality control. General measurement systems are namely: (i)
optical triangulation, (ii) light stripe, (iii) photogrammetry, (iv) time of flight, and (v)
interference (laser trackers) [1].

Optical Triangulation is one of the oldest forms of distance measurement. All that is
required is that the angle between a single point on an object and two separate views can
be measured together with distance between the two view-points as shown in Figure 1.0.

Figure 1.0: Single point optical triangulation configuration [1].

For the light stripe method, several hundred 2-D measurements of an object surface are
produced up to sixty times per second. A light stripe system uses a 2-D CCD sensor and a
light stripe generator as shown in Figure 1.1. The image formed of the projected light
stripe will reveal the shape of the surface if viewed from an angle. The shape of the imaged
stripe vary depending on distance each point on the surface is away from the instrument.

Figure 1.1: Light stripe system configuration [1].

1
Photogrammetry is a non-contact multiple point measuring tool that is capable of high
accuracy measurement of 10 to more than 1000 points simultaneously. It typically provides
high accuracy results between 1 part in 5000 to 1 part in 150,000 of the largest dimension
of the object being measured. Photogrammetry uses images of an object taken from a
variety of directions as shown in Figure 1.2.

Figure 1.2: Configuration of a photogrammetric system [1].

Time of flight services use the time taken for light or sound to travel to a surface and
return. The biggest advantage of these systems over triangulation systems is the sent and
returned light rays are usually co-axial. Ultrasonic rangefinders use sound to measure
distance with an accuracy of a few centimeters, over a range of a few meters, at a rate of
milliseconds to several seconds per sample. Sound waves propagate from the transmitter
and bounce off objects, returning an echo to the receiver as shown in Figure 1.3.

Figure 1.3: Ultrasonic device configuration [1].

2
Interference measuring techniques that use interference usually provide relative
measurements rather than absolute, The distance to an object can only be known relative to
another by zeroing the counter to a reference position. Laser trackers as shown in Figure
1.4 provide a relatively fast, precise and intuitive method of measuring large objects in
industrial environments.

Figure 1.4 Laser tracker configuration [1].

1.2 Problem definition:

It is the intention of the project to develop a non-contact distance measurement system as


part of the navigation system for a low-cost multi-function mobile robot. From the
overview of distance measurement systems in section 1.1, the methods may be classified to
light or sound based. Sound based methods are more economical than light based methods
as ultrasonic transducers are generally less costly that optical sources.

The features of ultrasonic ranging which makes it suitable for mobile robot navigation in
various terrains include the ability to work with almost any surface type, resistant to
vibration, radiation, background light and noise, largely unaffected by dust or dirt and long
distance measurements of up to 10 meters.

The disadvantages of ultrasonic ranging in terms of moderate accuracy (0.1 to 2% of


range) [1], poor object resolution from large beam fan angle, limited speed from high
accuracy measurement and requirement of near perpendicular object surface angle may be
accepted as mobile robot navigation does not require high accuracy, speed and resolution.
The primary purpose is the detection and avoidance of obstacles.

3
1.3 Project aims

The project involves developing a PIC16F873 microcontroller-based application. The


system block diagram is as shown in Figure 1.5. Minimal circuitry for basic
microcontroller operation will be constructed and tested. The peripheral interfacing
circuitry to the microcontroller will be developed for two 40 kHz ultrasonic transducers
(one each for transmitter and receiver) and three 7-segment displays. The microcontroller
firmware will be developed using MPLAB IDE assembler for the distance measurement
application. Initial test programs will be developed to test and verify hardware operation.
The ultrasonic range meter performance will be verified by confirming detection operation,
characterizing the measuring range and temperature calibration to offset the variation in
ambient temperature.

Figure 1.5: Microcontroller-based ultrasonic range meter system block diagram.

1.4 Project objectives

To develop a microcontroller-based ultrasonic range meter:


(i) To develop the interface circuitry for microcontroller to ultrasonic transducers.
(ii) To develop an application for distance measurement using ultrasonic pulses.

4
1.5 Report Outline

The report consists of five chapters: (i) Introduction, (ii) Project hardware, (iii) Project
software, (iv) Tests, results, and discussion, and (v) Conclusions and recommendations.
Chapter 1 provides an overview of the project background, problem definition, aims, and
objectives. Chapter 2 discusses the hardware involved in terms of the circuits and parts
used. Chapter 3 discusses the program and processing used while chapter 4 explains the
performance testing. Chapter 5 concludes on the project work carried out.

2.0 PROJECT HARDWARE

2.1 Circuit Drawing

Figure 2.1 shows the circuit drawing of the developed ultrasonic range meter system. The
circuit consists of six parts, namely: (i) receiver circuit, (ii) transmitter circuit, (iii) 7-
segment LED display circuit, (iv) temperature revision voltage generating circuit, (v)
resonator, and (vi) power supply circuit. Figure 2.2 shows the pattern drawing for the
ultrasonic range meter. Figure 2.3 shows the developed prototype.

2.2 Receiver Circuit

The receiver circuit consists of four subcircuits: (i) signal amplification circuit, (ii)
detection circuit, (iii) signal detector, and (iv) signal holding circuit.

2.2.1 Signal amplification circuit

The ultrasonic signal which was received with the reception sensor is amplified by 1000
times(60dB) of voltage with the operational amplifier with two stages as shown in Figure
2.4. It is 100 times at the first stage (40dB) and 10 times (20dB) at the next stage.

5
Figure 2.1: Circuit diagram of overall ultrasonic range meter system

Figure 2.2: PCB pattern drawing for the ultrasonic range meter.

6
Figure 2.3: Ultrasonic range meter prototype.

Generally, the positive and negative power supply are used for the opamp. The circuit this
time works with the single power supply of +9 V. Therefore, for the positive input of the
operational amplifiers, the half of the power supply voltage is appied as the bias voltage.
Then the alternating current signal can be amplified on 4.5V central voltage. When using
the operational amplifier with the negative feedback, the voltage of the positive input
terminal and the voltage of the negative input terminal become equal approximately. This
is called virtual grounding. So, by this bias voltage, the side of the positive and the side of
the negative of the alternating current signal can be equally amplified. When not using this
bias voltage, the distortion causes the alternating current signal. This technique is often
used when using the opamp which needs two kinds of powers in the single power.

2.2.2 Detection circuit

The detection is done to detect the received ultrasonic signal. This is the half-wave
rectification circuit with Shottky barrier diodes as shown in Figure 2.5. The DC voltage
according to the level of the detection signal is output to the capacitor behind the diode.
The Shottky barrier diodes are used because the high frequency characteristic is good.

Figure 2.4: Signal amplification circuit Figure 2.5: Detection Circuit

7
2.2.3 Signal detector

The signal detector circuit as shown in Figure 2.6 is the circuit which detects the ultrasonic
which returns from the measurement object. The output of the detection circuit is detected
using the comparator. At the circuit this time, the operational amplifier of the single power
supply is used instead of the comparator. The operational amplifier amplifies and outputs
the difference between the positive input and the negative input.

In case of the operational amplifier which doesn't have the negative feedback, the output
becomes the saturation state by a little input voltage. Generally, the operational amplifier
has over 10000 times of mu factors. So, when the positive input becomes higher a little
than the negative input, the difference is tens of thousands of times amplified and the
output becomes the same as the power supply almost.(It is the saturation state) Oppositely,
when the positive input becomes lower a little than the negative input, the difference is tens
of thousands of times amplified and the output becomes 0 V almost.(It is in the OFF
condition) This operation is the same as the operation of the comparator. However,
because the inner circuit of the comparator is different from the operational amplifier, the
comparator can not be used as the operational amplifier.

2.2.4 Signal holding circuit

The signal holding circuit of Figure 2.7 is the holding circuit of detected signal. SR ( Set
and Reset ) flip-flop is used. The detector is made to be not operate in the constant
time(About 1.5 milliseconds) after sending out a transmission pulse to prevent from the
wrong detection which is due to the influence of the transmission pulse. This operation is
controlled with the software of PIC.

When using the capture feature of PIC, this circuit isn't indispensable. Capture operation is
done by the change of the capture input in the once. The reason for using this circuit is to
confirm signal detection operation within the reflected signal detection time(About 65
milliseconds). When sending out next ultrasonic pulse, the output of this circuit is checked.
And when the output is L level, an error display is done because the reflected signal could
not be detected.

8
Figure 2.6: Signal detector circuit.
Figure 2.7: Signal holding circuit

2.3 Transmitter Circuit

The inverter in Figure 2.8 is used for the drive of the ultrasonic sensor. The two inverters
are connected in parallel because of the transmission electric power increase. The phase
with the voltage to apply to the positive terminal and the negative terminal of the sensor
has been 180 degrees shifted. Because it is cutting the direct current with the capacitor,
about twice of voltage of the inverter output are appied to the sensor.

The power supply voltage of this drive circuit is +9V. It is converting voltage with the
transistor to make control at the operating voltage of PIC(+5V). Because C-MOS inverters
are used, it is possible to do ON/OFF at high speed comparatively.

2.4 7-segment LED display circuit

Three 7 segment LEDs are used for 3-digit display as shown in Figure 2.9. As for the
lighting-up of the LED, 1 digit is displayed in the order with the software of PIC. At the
circuit this time, The LEDs light up it when the terminal of PIC is L level. So, ANODE
COMMON type is used as the LED. The anode common type is the type which the side of
the positive(Anode) of the LED is connected inside. It lights up when grounding(L level) a
cathode in the segment to want to make light up.

9
Figure 2.8: Transmitter Circuit Figure 2.9: 7-segment LED display
circuit

2.5 Temperature revision voltage generating circuit

The temperature revision voltage generating circuit is as shown in Figure 2.10. The sound
wave propagation speed in air is changed by the temperature. At 0°C, it is 331.5m/sec. At
40°C, it is 355.5m/sec. This range meter calculates a distance by dividing the propagation
time which was measured by the capture feature.

Figure 2.10: Temperature revision voltage generating circuit.

The time which the sound wave takes to go and return is 2m/331.5m/sec = 0.006033
seconds = 6.033 milliseconds. The contents of the counter by the capture feature is 6033
within 1 microsecond. For the conversion of this to distance (cm), it divides by 60. It is
6033/60=100.55. The following of the decimal point is cut off. It becomes a measurement
error. This conversion value (60) depends on the temperature. It isn't related with the
distance to measure. In case of 9 m, it is 54298/60=904.9. The conversion error becomes
big when the distance becomes long. This is because it isn't possible to do conversion

10
below the decimal point. In case of 40°C, it is 2m/355.5m/sec = 5625 microseconds and
the conversion value is 56. The conversion value should be changed by the ambient
temperature. At this range meter, a conversion value is generated using the A/D conversion
feature. The A/D converter converts input voltage into the 10-bit digital data. Using upper
3 bits, the A/D conversion result from 0V to 5V is the value from 0 to 7. And 54 is added
to this value. Then, the conversion value range is from 54 to 61.

2.6 Resonator

A 4-MHz resonator is used in the relation of the timer in the count time as shown in Figure
2.11. When using 4-MHz clock, it is 1 microsecond per count for the counter count up
time. Timer1 to use for capture is a maximum of 65535 counts(16 bits). So, a maximum of
65.535 milliseconds count is made. The propagation speed of the sound in air is 343
m/second in case of 20°C. In the time which goes and returns in the 10-m distance, it is
20m/343m/sec = 0.0583 seconds (58.3 milliseconds). As the range meter this time, it is an
exactly good value.

2.7 Power supply circuit

The voltage of +5V and +9V are made with +12V power supply using the 3 terminal
regulator as shown in Figure 2.12. +9V are used for the transmitter and the receiver. 100-
mA type is used because few needed electric currents.

Figure 2.11 Resonator circuit. Figure 2.12: Power supply circuit

In other circuit, +5V is used. +5V voltage is used for the lighting-up of LEDs, because they
are controlled by PIC. The about 10-mA electric current per segment flows through the
LED. So, it becomes about 80 mA when all segments(eight) light up.

11
3.0 PROJECT SOFTWARE

3.1 Range Meter Operation

The 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 as shown in Figure 3.1 is repeatedly executed. LED
display processing is executed in parallel with this operation.

Figure 3.1: Range meter operation.

3.2 Ultrasonic Pulse Sending Out Process

In the pulse sending-out process, the following processes iare done:

(i) Clear the interruption display


The interruption flag of TMR0 is cleared. When not clearing this flag, the interruption
occurs at once when interruption processing is ended without waiting a regular time-out.

(ii) Check the detection error


When the detection of the reflected wave isn't done from the pulse sending-out in the last
time until the pulse sending-out this time, it means measurement impossible and display is
turned off. The previous data is displayed in the measurement impossible condition when
not doing this process and the mistake occurs to the measurement.

12
(iii) Stop the reflected wave detector
Immediately after letting out a transmission pulse, the influence occurs to the receiver
circuit and there is possibility of wrong detection. To prevent this, the operation of the
reflected wave detector is stopped. RA4 is an open type and an external resistor is needed.

(iv) Start the capture operation


The count area of timer1 and the contents of the capture register are cleared. the rising
edge detection mode capture is set and a capture interruption enable bit is set. And an
interruption flag is cleared because of the sure operation.

(v) Send-out the 40KHz pulse


The 40KHz pulse is the pulse which has 12.5 µ-sec as ON time and 12.5 µ-sec as OFF
time in 1 cycle. 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)

(vi) Take-in the display revision data


To make operate A/D converter, the GO bit of the ADCON0 register is set. When
switching the input channel of A/D converter, after switching the input, about 20
microseconds waiting time is needed. This time, because the input channel is fixed on 0,
waiting time isn't necessary. The completion of the A/D conversion is judged by the
checking the GO bit. When GO bit is cleared, the conversion is completion. The upper 3
bits is picked up and add 54 as the conversion value.

(vii) Error detection prevention


A detector is stopped until the influence ends to the receiver after sending out a pulse. This
time is set to about 1 millisecond. When few influences occur, it is to be OK even if it is
shorter. When many influences occur, it is necessary to make longer.

(viii) Start the reflected wave detection


After the wrong detection prevention time elapse, it is made a enable condition in the
reflected wave detection.

13
3.3 Capture interruption process

In the capture interruption process, the following process is done:

(i) Clear the interruption display


Capture interruption flag is cleared.

(ii) Distance conversion processing


The count value of the timer0 which was stopped by the capture operation is proportional
to the propagation time of the sound wave. It isn't possible to use for the display just as it
is. In the processing this time, division by some value is done and is converted into the
distance numerical value. For example, reflect 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.

(iii) Display setting processing


Because the type of distance value which is converted is binary, it can not be displayed on
the LED just as it is. 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.

4.0 TESTS, RESULTS AND DISCUSSION

The following tests were performed to test the performance of the Ultrasonic Range Meter:
(i) Detection operation confirmation
(ii) Measuring range
(iii) Temperature calibration feature

14
4.1 Detection operation confirmation

The photograph shown in Figure 4.1 is the one to have observed the signal when
measuring a 2.36m distance. Observation points are shown below.
(1) is 1st pin of IC1 ( It is a signal after the amplification with the operational amplifier. )
(2) is 8th pin of IC3 ( It is a detection holding circuit gate signal by PIC. )
(3) is 11th pin of IC3 ( It is an output signal of the detection holding circuit. )

Figure 4.1: Signal observation when measuring a 2.36m distance.

When (2) is an L level, the detector stops. You can find that a malfunction by the
transmission pulse is prevented. The prevention time of this circuit is about 1.5
milliseconds. There is possibility to malfunction when short any more.

Some feeble reflection signals are received until it detects a reception pulse. Because the
threshold voltage of the signal detector of IC2 is working effectively, those feeble signals
are not detected.

The signal of (3) changes to H level from L level when detecting a reception pulse. This
signal is detected by the capture feature of PIC and the propagation time of the ultrasonic is
measured.

15
4.2 Measuring range

The ultrasonic transmission time of this Meter is 0.5 milliseconds. So, it isn't possible to do
long-range measurement.

Most short : 29 cm
distance This distance is decided by the time to prevent from an influence by
the transmission pulse. When measuring a long distance, the sending-
out time of the transmission pulse must be expanded. In this case, it is
necessary to expand in the prevention time, too, and the shortest
measurement distance becomes long.
Most long distance : 3.6 m
This distance is decided by the length of the transmission pulse. The
ultrasonic energy becomes big when the transmission time is long and
the long-range measurement is possible. However, as the explaining
at the most short distance, the shortest measurement distance becomes
long.

4.3 Temperature calibration feature

The test was conducted for a distance measurement of 1 m as shown in Figure 4.2. The
temperature to display 1.00 is a calculation value. At this equipment, it isn't possible to do
an adjustment with detailed calibration value. It is 8 steps. So, it is a reference value. To
change a calibration range, the change of the software processing by the value which A/D
converted is needed.

When doing this confirmation, the opposite connection of the calibration resistor was
erroneous. The connection in the center is right but the connection in the both edges is
opposite. It was intended to turn to the right when the ambient temperature was high.
However, the actual operation is opposite. Because it is not in the problem in case of
practical use, it was therefore not changes. A schematic and a pattern drawing are
corrected.

16
Dial Temperature
Display
condition for displaying 1.00

Full turn
to right 0.98 8.6°C

Full turn
to left 1.11 83.0°C

Figure 4.2: Temperature calibration

5.0 CONCLUSIONS AND RECOMMMENDATIONS

5.1 Conclusions

A microcontroller-based ultrasonic range meter was successfully developed with the


capability to measure distances within the range of 29 cm to 3.6 m. The range meter
includes a system to compensate for ambient temperature variation with 8 adjustable levels
of calibration. Both project objectives of interfacing a microcontroller to ultrasonic
transducers and developing a distance measuring application based on ultrasonic pulses
was achieved. The non-contact distance measurement system developed can be
implemented in a mobile robot navigation system by converting the distance data to
suitable ASCII format and transmitting the data via serial communication to the
processor/microcontroller managing the navigation system. The developed range meter
may also be similarly employed to a variety of distance measuring systems requiring a
digital output, where the main concern is on low-cost and simplicity of design, with
moderate requirements for accuracy, resolution and speed.

5.2 Recommendations

When wanting to do long-range measurements, it is possible to change software even if it


doesn’t change hardware of it. In this case, it is better when putting a sound horn to
concentrate ultrasonic pulses such that larger distances may be traversed.

17
REFERENCES

[1] Clarke, T. and Williams, M. (1999), Buyers Guide to Six Non-contact Distance
Measuring Techniques. Retrieved May 12, 2012, from http://www.optical-metrology-
centre.com: http://www.optical-metrology-
centre.com/Downloads/Papers/Quality%20Today%201999%20Buyers%20Guide.pdf

[2] “PIC16F87XA Datasheet”, Microchip Technology Inc, 2003.

[3] Padmanabhan, K. (2008), Microcontroller-based Ultrasonic Distance Meter.


Retrieved May 12, 2012, from
http://kitsnspares.com/admin/pdffiles/Ultrasonic%20Distance%20Meter.pdf

APPENDIX A: Source code file of Ultrasonic Range Meter


;********************************************************
;
; Ultrasonic Range Meter
;
; Device : PIC16F873
; Author : Seiichi Inoue
;********************************************************

list p=pic16f873
include p16f873.inc
__config _hs_osc & _wdt_off & _pwrte_on & _lvp_off

;**************** Label Definition ********************


cblock h'20'
s_count ;Send-out pulse count adr
s_adj ;Adjustment data address
s_adj_count ;Rotate value save adr
s_digit ;Digit cont work address
g_time1 ;Guard timer address 1
g_time2 ;Guard timer address 2
p_countl ;Propagation L cnt adr
p_counth ;Propagation H cnt adr
digit_cnt ;Digit counter head adr
disp_ha ;Digit head address
disp_u ;1st digit address
disp_t ;10th digit address
disp_h ;100th digit address
seg7_ha ;7 segLED table head adr
seg70 ;Pattern 0 set adr
seg71 ;Pattern 1 set adr
seg72 ;Pattern 2 set adr
seg73 ;Pattern 3 set adr
seg74 ;Pattern 4 set adr
seg75 ;Pattern 5 set adr
seg76 ;Pattern 6 set adr
seg77 ;Pattern 7 set adr
seg78 ;Pattern 8 set adr
seg79 ;Pattern 9 set adr

18
seg7a ;Pattern A set adr
seg7b ;Pattern B set adr
endc

ra1 equ h'01' ;RA1 port designation


ra2 equ h'02' ;RA2 port designation
ra3 equ h'03' ;RA3 port designation
ra5 equ h'05' ;RA5 port designation

ccp1 equ h'02' ;CCP1(RC2) designation

seg7_0 equ b'01000000' ;-gfedcba Pattern 0


seg7_1 equ b'01111001' ; Pattern 1
seg7_2 equ b'00100100' ; Pattern 2
seg7_3 equ b'00110000' ; Pattern 3
seg7_4 equ b'00011001' ; Pattern 4
seg7_5 equ b'00010010' ; Pattern 5
seg7_6 equ b'00000010' ; Pattern 6
seg7_7 equ b'01111000' ; Pattern 7
seg7_8 equ b'00000000' ; Pattern 8
seg7_9 equ b'00010000' ; Pattern 9
seg7_a equ b'01111111' ; Detect error
seg7_b equ b'00100011' ; Illegal int

;**************** Program Start ***********************


org 0 ;Reset Vector
goto init
org 4 ;Interrupt Vector
goto int

;**************** Initial Process *********************


init

;*** Port initialization


bsf status,rp0 ;Change to Bank1
movlw b'00000001' ;AN0 to input mode
movwf trisa ;Set TRISA register
clrf trisb ;RB port to output mode
movlw b'00000100' ;RC2/CCP1 to input mode
movwf trisc ;Set TRISC register

;*** Ultrasonic sending period initialization (Timer0)


movlw b'11010111' ;T0CS=0,PSA=0,PS=1:256
movwf option_reg ;Set OPTION_REG register
bcf status,rp0 ;Change to Bank0
clrf tmr0 ;Clear TMR0 register

;*** Capture mode initialization (Timer1)


movlw b'00000001' ;Pre=1:1 TMR1=Int TMR1=ON
movwf t1con ;Set T1CON register
clrf ccp1con ;CCP1 off

;*** A/D converter initialization


movlw b'01000001' ;ADCS=01 CHS=AN0 ADON=ON
movwf adcon0 ;Set ADCON0 register
bsf status,rp0 ;Change to Bank1
movlw b'00001110' ;ADFM=0 PCFG=1110
movwf adcon1 ;Set ADCON1 register
bcf status,rp0 ;Change to Bank0

;*** Display initialization (Timer2)


movlw disp_u ;Set digit head address
movwf disp_ha ;Save digit head sddress
movlw h'0a' ;"Detect error" data
movwf disp_u ;Set 1st digit
movwf disp_t ;Set 10th digit
movwf disp_h ;Set 100th digit
movlw d'3' ;Digit counter

19
movwf digit_cnt ;Set digit counter
movlw seg70 ;Set 7seg head address
movwf seg7_ha ;Save 7seg head address
movlw seg7_0 ;Set 7segment pattern 0
movwf seg70 ;Save pattern 0
movlw seg7_1 ;Set 7segment pattern 1
movwf seg71 ;Save pattern 1
movlw seg7_2 ;Set 7segment pattern 2
movwf seg72 ;Save pattern 2
movlw seg7_3 ;Set 7segment pattern 3
movwf seg73 ;Save pattern 3
movlw seg7_4 ;Set 7segment pattern 4
movwf seg74 ;Save pattern 4
movlw seg7_5 ;Set 7segment pattern 5
movwf seg75 ;Save pattern 5
movlw seg7_6 ;Set 7segment pattern 6
movwf seg76 ;Save pattern 6
movlw seg7_7 ;Set 7segment pattern 7
movwf seg77 ;Save pattern 7
movlw seg7_8 ;Set 7segment pattern 8
movwf seg78 ;Save pattern 8
movlw seg7_9 ;Set 7segment pattern 9
movwf seg79 ;Save pattern 9
movlw seg7_a ;Set 7segment pattern A
movwf seg7a ;Save pattern A
movlw seg7_b ;Set 7segment pattern B
movwf seg7b ;Save pattern B
movlw b'00011110' ;OPS=1:4,T2=ON,EPS=1:16
movwf t2con ;Set T2CON register
bsf status,rp0 ;Change to Bank1
movlw d'157' ;157x64=10048usec
movwf pr2 ;Set PR2 register
bsf pie1,tmr2ie ;TMR2IE=ON
bcf status,rp0 ;Change to Bank0

;*** Interruption control


movlw b'11100000' ;GIE=ON,PEIE=ON,T0IE=ON
movwf intcon ;Set INTCON register

wait
goto $ ;Interruption wait

;*************** Interruption Process *****************


int
movfw pir1 ;Read PIR1 register
btfsc pir1,ccp1if ;Capture occurred ?
goto capture ;Yes. "Capture"
btfsc pir1,tmr2if ;TMR2 time out ?
goto led_cont ;Yes. "LED display"
movfw intcon ;Read INTCON register
btfsc intcon,t0if ;TMR0 time out ?
goto send ;Yes. "Pulse send"

;*************** Illegal interruption *****************


illegal
movlw h'0b' ;Set Illegal disp digit
addwf seg7_ha,w ;Seg7 H.Adr + digit
movwf fsr ;Set FSR register
movfw indf ;Read seg7 data
movwf portb ;Write LED data
bcf porta,ra1 ;RA1=ON
bcf porta,ra2 ;RA2=ON
bcf porta,ra3 ;RA3=ON
goto $ ;Stop

;************ END of Interruption Process **************


int_end

20
retfie

;*************** Pulse send-out Process ****************


send
bcf intcon,t0if ;Clear TMR0 int flag
clrf tmr0 ;Timer0 clear

;*** Received Pulse detection check


movfw portc ;Read PORTC register
btfsc portc,ccp1 ;Detected ?
goto detect_off ;Yes. Detected
movlw h'0a' ;"Detect error" data
movwf disp_u ;Set 1st digit
movwf disp_t ;Set 10th digit
movwf disp_h ;Set 100th digit

;*** Receive pulse detector off


detect_off
bcf porta,ra5 ;Set detector OFF

;*** Capture start


clrf tmr1h ;Clear TMR1H register
clrf tmr1l ;Clear TMR1L register
clrf ccpr1h ;Clear CCPR1H register
clrf ccpr1l ;Clear CCPR1L register
movlw b'00000101' ;CCP1M=0101(Capture)
movwf ccp1con ;Set CCP1CON register
bsf status,rp0 ;Change to Bank1
bsf pie1,ccp1ie ;CCP1 interruptin enable
bcf status,rp0 ;Change to Bank0
bcf pir1,ccp1if ;Clear CCP1 int flag

;*** 40KHz pulse send ( 0.5 msec )


movlw d'20' ;Send-out pulse count
movwf s_count ;Set count
s_loop
call pulse ;Call pulse send sub
decfsz s_count,f ;End ?
goto s_loop ;No. Continue

;*** Get adjustment data


bsf adcon0,go ;Start A/D convert
ad_check
btfsc adcon0,go ;A/D convert end ?
goto ad_check ;No. Again
movfw adresh ;Read ADRESH register
movwf s_adj ;Save converted data

movlw d'5' ;Set rotate value


movwf s_adj_count ;Save rotate value
ad_rotate
rrf s_adj,f ;Rotate right 1 bit
decfsz s_adj_count,f ;End ?
goto ad_rotate ;No. Continue
movfw s_adj ;Read rotated value
andlw b'00000111' ;Pick-up 3 bits
addlw d'54' ;(0 to 7) + 54 = 54 to 61
movwf s_adj ;Save adjustment data

;*** Capture guard timer ( 1 milisecound )


movlw d'2' ;Set loop counter1
movwf g_time1 ;Save loop counter1
g_loop1 movlw d'124' ;Set loop counter2
movwf g_time2 ;Save loop counter2
g_loop2 nop ;Time adjust
decfsz g_time2,f ;g_time2 - 1 = 0 ?
goto g_loop2 ;No. Continue
decfsz g_time1,f ;g_time1 - 1 = 0 ?

21
goto g_loop1 ;No. Continue

;*** Receive pulse detector on


bsf porta,ra5 ;Set detector ON

goto int_end

;*************** Pulse send-out Process ****************


pulse
movlw b'00010000' ;RC4=ON
movwf portc ;Set PORTC register
call t12us ;Call 12usec timer
clrf portc ;RC4=OFF
goto $+1
goto $+1
nop
return

;*************** 12 microseconds timer *****************


t12us
goto $+1
goto $+1
goto $+1
goto $+1
nop
return

;****************** Capture Process ********************


capture
bcf pir1,ccp1if ;Clear CCP1 int flag

clrf p_countl ;Clear L count


clrf p_counth ;Clear H count
clrf ccp1con ;CCP1 off

division
movfw s_adj ;Read adjustment data
subwf ccpr1l,f ;Capture - adjust
btfsc status,z ;Result = 0 ?
goto division2 ;Yes. "R = 0"
btfsc status,c ;Result < 0 ?
goto division1 ;No. "R > 0"
goto division3 ;Yes."R < 0"

division1 ;( R > 0 )
movlw d'1' ;Set increment value
addwf p_countl,f ;Increment L count
btfss status,c ;Overflow ?
goto division ;No. Continue
incf p_counth,f ;Increment H count
goto division ;Jump next

division2 ;( R = 0 )
movfw ccpr1h ;Read CCPR1H
btfss status,z ;CCPR1H = 0 ?
goto division1 ;No. Next
movlw d'1' ;Set increment value
addwf p_countl,f ;Increment L count
btfss status,c ;Overflow ?
goto digit_set ;Jump to digit set
incf p_counth,f ;Increment H count
goto digit_set ;Jump to digit set

division3 ;( R < 0 )
movfw ccpr1h ;Read CCPR1H
btfss status,z ;CCPR1H = 0 ?
goto division4 ;No. Borrow process
goto digit_set ;Jump to digit set

22
division4
decf ccpr1h,f ;CCPR1H - 1
movlw d'255' ;Borrow value
addwf ccpr1l,f ;CCPR1L + 255
incf ccpr1l,f ;CCPR1L + 1
goto division1 ;Next

;**************** Digit Set Process ********************


digit_set
clrf disp_u ;Clear 1st digit
clrf disp_t ;Clear 10th digit
clrf disp_h ;Clear 100th digit

;*** 100th digit


digit_h
movlw d'100' ;Divide value
subwf p_countl,f ;Digit - divide
btfsc status,z ;Result = 0 ?
goto digit_h2 ;Yes. "R = 0"
btfsc status,c ;Result < 0 ?
goto digit_h1 ;No. "R > 0"
goto digit_h3 ;Yes."R < 0"

digit_h1 ;( R > 0 )
incf disp_h,f ;Increment 100th count
goto digit_h ;Jump next

digit_h2 ;( R = 0 )
movfw p_counth ;Read H counter
btfss status,z ;H counter = 0 ?
goto digit_h1 ;No. Next
incf disp_h,f ;Increment 100th count
goto digit_t ;Jump to 10th digit pro

digit_h3 ;( R < 0 )
movfw p_counth ;Read H counter
btfss status,z ;H counter = 0 ?
goto digit_h4 ;No. Borrow process
movlw d'100' ;Divide value
addwf p_countl,f ;Return over sub value
goto digit_t ;Jump to 10th digit pro

digit_h4
decf p_counth,f ;H counter - 1
movlw d'255' ;Borrow value
addwf p_countl,f ;L counter + 255
incf p_countl,f ;L counter + 1
goto digit_h1 ;Next

;*** 10th digit


digit_t

;*** Range over check


movfw disp_h ;Read 100th digit
sublw d'9' ;9 - (100th digit)
btfsc status,z ;Result = 0 ?
goto digit_t0 ;Yes. "R = 0"
btfsc status,c ;Result < 0 ?
goto digit_t0 ;No. "R > 0"
movlw h'0a' ;"Detect error" data
movwf disp_u ;Set 1st digit
movwf disp_t ;Set 10th digit
movwf disp_h ;Set 100th digit
goto int_end

digit_t0
movlw d'10' ;Divide value

23
subwf p_countl,f ;Digit - divide
btfsc status,z ;Result = 0 ?
goto digit_t1 ;Yes. "R = 0"
btfsc status,c ;Result < 0 ?
goto digit_t1 ;No. "R > 0"
goto digit_t2 ;Yes."R < 0"

digit_t1 ;( R >= 0 )
incf disp_t,f ;Increment 10th count
goto digit_t ;Jump next

digit_t2 ;( R < 0 )
movlw d'10' ;Divide value
addwf p_countl,f ;Return over sub value
goto digit_u ;Jump to 1st digit pro

;*** 1st digit


digit_u
movfw p_countl ;Read propagetion counter
movwf disp_u ;Save 1st count

goto int_end

;**************** LED display control *****************


led_cont
bcf pir1,tmr2if ;Clear TMR2 int flag

movfw digit_cnt ;Read digit counter


movwf s_digit ;Save digit counter
decfsz s_digit,f ;1st digit ?
goto d_check1 ;No. Next
bsf porta,ra1 ;RA1=OFF
bsf porta,ra2 ;RA2=OFF
bcf porta,ra3 ;RA3=ON
goto c_digit ;Jump to digit cont
d_check1
decfsz s_digit,f ;10th digit ?
goto d_check2 ;No. 100th digit
bsf porta,ra1 ;RA1=OFF
bcf porta,ra2 ;RA2=ON
bsf porta,ra3 ;RA3=OFF
goto c_digit ;Jump to digit cont
d_check2
bcf porta,ra1 ;RA1=ON
bsf porta,ra2 ;RA2=OFF
bsf porta,ra3 ;RA3=OFF

c_digit
decf digit_cnt,w ;Digit count - 1
addwf disp_ha,w ;Digit H.Adr + count
movwf fsr ;Set FSR register
movfw indf ;Read digit
addwf seg7_ha,w ;Seg7 H.Adr + digit
movwf fsr ;Set FSR register
movfw indf ;Read seg7 data
movwf portb ;Write LED data

decfsz digit_cnt,f ;Digit count - 1


goto int_end ;Jump to interrupt end
movlw d'3' ;Initial value
movwf digit_cnt ;Set initial value
goto int_end ;Jump to interrupt end

;********************************************************
; END of Ultrasonic Range Meter
;********************************************************

en

24
Appendix B – Student-Supervisor Log-Book

Week Date of Time & Supervisor


Log (Statement of the Progress Achieved) Action Required
No. Meeting Venue signature

8am,
1. 24/9
C300 Determined the project, ultrasonic range meter, and required resources none, progress achieved

8am,
2. 1/10
C300 submission of draft proposal Did not state the project schedule, please indicate project schedule.

8am,
3. 8/10
C300 Resubmission of proposal none, progress achieved

8am, Construct minimal microcontroller circuitry and verify operation, develop interfacing
4. 15/10
C300 circuitry to 7-segment displays, verify operation,
none, progress achieved

8am, Develop interfacing circuitry to ultrasonic transducers and verify operation using
5. 22/10
C300 oscilloscope.
circuit problem, need to troubleshoot

* Log-Book Sheets to be attached to project report appendix

25
Appendix B – Student-Supervisor Log-Book

Week Date of Time & Supervisor


Log (Statement of the Progress Achieved) Action Required
No. Meeting Venue signature

8am, Successfully troubleshoot interfacing circuitry to ultrasonic transducers and verify


6. 29/10
C300 operation using oscilloscope. none, progress achieved

8am,
7. 5/11
C300 Develop an application to transmit and detect ultrasonic pulses. none, progress achieved

8am,
8. 12/11
C300 Develop an application for distance measurement using ultrasonic pulses. inaccurate results, check temperature effect

8am, included temperature compensation in application none, progress achieved


9. 19/11
C300

8am,
10. 26/11
C300 System integration, testing and verification
none, progress achieved

* Log-Book Sheets to be attached to project report appendix

26
Appendix B – Student-Supervisor Log-Book

Week Date of Time & Supervisor


Log (Statement of the Progress Achieved) Action Required
No. Meeting Venue signature

8am, Prototype fabrication


11. 3/12
C300
none, progress achieved

8am,
12. 10/12
C300 Report submission
minor modifications and corrections.

8am, Report resubmission none, progress achieved


13. 17/12
C300

8am,
14. 24/12
C300 Presentation
none, progress achieved

* Log-Book Sheets to be attached to project report appendix

27

S-ar putea să vă placă și