Download ultrasonic radar based

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Islanding wikipedia , lookup

Spark-gap transmitter wikipedia , lookup

Electrical substation wikipedia , lookup

Peak programme meter wikipedia , lookup

Decibel wikipedia , lookup

History of electric power transmission wikipedia , lookup

Stray voltage wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Variable-frequency drive wikipedia , lookup

Time-to-digital converter wikipedia , lookup

Sound level meter wikipedia , lookup

Alternating current wikipedia , lookup

Power inverter wikipedia , lookup

Printed circuit board wikipedia , lookup

Transformer types wikipedia , lookup

Pulse-width modulation wikipedia , lookup

Voltage regulator wikipedia , lookup

Power electronics wikipedia , lookup

Buck converter wikipedia , lookup

Voltage optimisation wikipedia , lookup

Oscilloscope history wikipedia , lookup

Metadyne wikipedia , lookup

Mains electricity wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Rectifier wikipedia , lookup

Opto-isolator wikipedia , lookup

Liquid-crystal display wikipedia , lookup

Transcript
MINOR PROJECT
ON
ULTRASONIC RADAR BASED
FLUID LEVEL CONTROLLER
CONTENTS
1. Introduction
2. Block diagram
3. Circuit diagram
4. Working of circuit
5. PCB layout
6. Parts and price list
7. Ultrasonic Radar
8. Microcontroller
9. LCD
10. Power Supply
11. Applications
12. Advantage
13. Disadvantage
14. Conclusion
15. References
16. Datasheets
INTRODUCTION
Sonic is the sound we can hear. Ultrasonic is the sound above human hearing range.
Human can hear maximum up to a frequency of 20 KHz. Ultrasonic frequencies are
above 20 KHz. Ultrasonic waves are used to measure level of liquids and solid objects in
industries. Ultrasonic level measurement is contactless principle and most suitable for
level measurements of hot, corrosive and boiling liquids. The normal frequency range
used for ultrasonic level measurements is within a range of 40
200 KHz. Ultrasonic
waves detect an object in the same way as Radar does it. Ultrasonic uses the sound
waves, and Radar uses radio waves. When ultrasonic pulse signal is targeted towards an
object, it is reflected by the object and echo returns to the sender. The time travelled by
the ultrasonic pulse is calculated, and the distance of the object is found. Bats use well
known method to measure the distance while travelling. Ultrasonic level measurement
principle is also used to find out fish positions in ocean, locate submarines below water
level, also the position of a scuba diver in sea.
We will refer to Fig-1 and make an effort to understand the technicalities of ultrasonic
level transmitter. An ultrasonic level transmitter is fixed at the top of a tank half filled
with liquid. The reference level for all measurements is the bottom of the tank. Level to
be detected is marked as “C”, and “B” is the distance of the ultrasonic sensor from the
liquid level. Ultrasonic pulse signals are transmitted from the transmitter, and it is
reflected back to the sensor. Travel time of the ultrasonic pulse from sensor to target
and back is calculated. Level “C” can be found by multiplying half of this time with the
speed of sound in air. The measuring unit final result can be centimeters, feet, inches
etc. Level = Speed of sound in air x Time delay / 2
The above principle of measurement looks quite straightforward and true only in theory.
In practice, there are some technical difficulties which are to be taken care to get correct
level reading.
a. Velocity of sound changes due to the variation of air temperature. An integrated
temperature sensor is used to compensate for changes in velocity of sound due to
temperature variations.
b. There are some interference echoes developed by the edges, welded joints etc.
This is taken care by the software of the transmitter and called interference echo
suppression.
c. Calibration of the transmitter is crucial. Accuracy of measurement depends on the
accuracy of calibration. The Empty distance “A” and measurement span “D” is to
be ascertained correctly for inclusion in calibration of the transmitter.
d. The transient characteristics of the sensor will develop a Blocking distance as
shown in Fig-1. Span “D” should never extend to the blocking distance.
Ultrasonic Sensor is the heart of the ultrasonic level Transmitter instrument. This sensor
will translate electrical energy into ultrasound waves. Piezoelectric crystals are used for
this conversion process. Piezoelectric crystals will oscillate at high frequencies when
electric energy is applied to it. The reverse is also true. These piezoelectric crystals will
generate electrical signals on receipt of ultrasound. These sensors are capable of
sending ultrasound to an object and receive the echo developed by the object. The echo
is converted into electrical energy for onward processing by the control circuit.
We will refer to Fig-3 Functional Block Diagram for clarify physical structures of an
Ultrasonic Level Transmitter.
A micro-controller based Control Circuit monitors all the activities of the ultrasonic level
transmitter. There are twoPulse Transmission Circuits, one for transmitter pulse and the
other one for receiver pulse. The pulse generated by the transmitter pulse is converted
to Ultrasound pulses by the Ultrasonic Sensor (Transmitter) and targeted towards the
object.
This ultrasound pulse is reflected back as an echo pulse to the Ultrasonic Sensor
(Receiver). The receiver converts this Ultrasonic pulse to an electrical signal pulse
through the pulse generator. The time elapsed, or the reflection time is measured by the
counter. This elapsed time has relation to the level to be measured. This elapsed time is
converted to level by the Control Circuit. There is a Timing Generator Circuit which is
used to synchronize all functions in the ultrasonic level measurement system.
The level is finally converted to 4-20mA signal. 4mA is 0% level, and 20mA is the 100%
level (see Fig-1). This 4-20mA output signal carrying the level data can be transmitted
to long distance to Process Control Instruments.
Ultrasonic level transmitter has no moving parts, and it can measure level without
making physical contact with the object. This typical characteristic of the transmitter is
useful for measuring levels in tanks with corrosive, boiling and hazardous chemicals. The
accuracy of the reading remains unaffected even after changes in the chemical
composition or the dielectric constant of the materials in the process fluids.
Ultrasonic level transmitters are the best level measuring devices where the received
echo of the ultrasound is of acceptable quality. It is not so convenient if the tank depth
is high or the echo is absorbed or dispersed. The object should not be sound absorbing
type. It is also unsuitable for tanks with too much smoke or high density moisture.
BLOCK DIAGRAM:
POWER SUPPLY
CIRCUIT
5V
5V
ULTRASONIC
RADAR U40
5V
MICROCONTROLLER
PIC16F877A
2*16 LCD
JHD162A
Sending data in 4 bit mode
4 KEY
KEYPAD
RELAY
WATER
PUMP
SCHEMATIC DIAGRAM
CONTROL CIRCUIT
PCB LAYOUT
COMPONENTS LAYOUT
PARTS AND PRICE LIST
S. No.
PARTS DESCRIPTION
QUANTITY
COST
TOTAL COST
1.
WATER VOLUME SENSOR
1
900.00
900.00
2.
LM7805 IC
1
10.00
10.00
3.
1K CARBON FILM RESISTANCE
6
0.25
1.50
4.
10K CARBON FILM RESISTANCE
6
0.25
1.50
5.
220Ω CARBON FILM RESISTANCE
1
0.25
0.25
6.
330Ω CARBON FILM RESISTANCE
1
0.25
0.25
7.
1N 4007 GENERAL PURPOSE Si DIODE
4
1.00
4.00
8.
5mm DIFFUSED LED
3
2.00
6.00
9.
0.1µF/63V NONELECTROLYTIC
CAPACITOR
3
0.50
1.50
10.
22pF /50V NONELECTROLYTIC
CAPACITOR
2
0.50
1.00
11.
MICROSWITCH (TO RESET)
3
5.00
15.00
12.
1000µF / 25V ELECTROLYTIC CAPACITOR
1
7.00
7.00
13.
10µF / 63V ELECTROLYTIC CAPACITOR
1
2.50
2.50
14.
11.0592J3L CRYSTAL
1
10.00
10.00
15.
JHD162A 2X16 LCD
1
150.00
150.00
16.
5 KΩ POTENTIOMETER
1
5.00
5.00
17.
MOLEX CONNECTOR (5 PIN)
1
9.00
9.00
18.
MOLEX CONNECTOR (4 PIN)
2
7.00
14.00
19.
MOLEX CONNECTOR (3 PIN)
3
6.00
18.00
20.
MOLEX CONNECTOR (2 PIN)
2
5.00
10.00
21.
40 PIN IC BASE
1
5.00
5.00
22.
16 PIN IC BASE
1
5.00
5.00
23.
14 PIN IC BASE
1
5.00
5.00
24.
PIC16F877A MICROCONTROLLER
1
150.00
150.00
25.
TRANSFORMER 220V pri 12V sec/1amp
step down shell type
1
200.00
200.00
26.
PCB BOARD
1
50.00
50.00
27.
TOTAL
2081.5
ULTRASONIC RADAR
Minimum range 10 centimeters
1.
2.
3.
4.
5.
6.
7.
Maximum range 400 centimeters (4 Meters)
Accuracy of +-1 cm
Resolution 0.1 cm
5V DC Supply voltage
Compact sized SMD design
Modulated at 40 kHz
Serial data of 9600 bps TTL level output for easy interface with any microcontroller
WORKING AND THEORY OF THE PROJECT
Rational of the project
As stated earlier, we wanted to create something that would be of practical use to people of various walks of
life. For instance, our rangefinder could be used in the case of a blackout where a person needs to find his way
through a dark building and cannot see where the walls are. Alternatively, it could be used by surveyors that are
preparing a site for construction.
One group from a previous year had done a project using an ultrasonic rangefinder, but their project involved a
pre-manufactured system. The bulk of their project was using AM transmission to communicate with the
rangefinder module.
Our project is different in the fact that we built the rangefinder from the ground up.
Logical structure
The basic principal of the rangefinder is based on the simple concepts of SONAR (Sound Navigation and
Ranging). First, an ultrasonic pulse is transmitted by a transducer (a device that converts a voltage signal into a
sound wave and vice versa). This pulse then reflects off an object and is received by another transducer. Using
the known speed of sound (340.29 m/s) and the time between the transmitted pulse (the ping) and the received
pulse (the pong), one can simply calculate the distance traveled by the pulse.
In addition to determining the distance between the device and an object using the known speed of sound, our
initial design included two additional modes of operation. One mode was a calibration mode while the other
was a speed detection mode.
Since the speed of sound varies with altitude and atmospheric conditions, a calibration mode is quite useful. Our
program is designed such that if in calibrate mode, the user can place the device a known distance 5 centimeters
and send a pulse. The device then uses the time necessary for the pulse to reflect off the object and calculates a
new value for the speed of sound. This new value is then used for all future distance calculations until the
device is powered off.
The original speed detection mode was used to indicate to the user how quickly he or she is moving towards or
away from an object. Since speed is determined by the change in distance divided by the change in time (dx/dt),
speed can be determined quite simply using two pulse transmissions. When initiated, a first pulse is sent and
received by the device. The distance is calculated and stored by the device. A second pulse is then sent and
received by the device 0.25 seconds later and the results stored by the device. The MCU can now take the
difference in distance, divide that by 0.25 seconds, and display the speed to the user on the LCD. We had
initially considered using Doppler shifts in frequency to calculate speed, but we decided against it since
measuring frequency is a completely new task we would have to program (compared to just detecting a pulse)
and would have required much more complicated and CPU intensive math. We also felt that the velocity of a
handheld device would not be fast enough to actually incur significant Doppler shifts that would make
calculations doable.
Due to noise issues while testing our original program, the method of calculating distance was changed. We are
now taking multiple distance samples and calculating the average. This enables more accurate readings.
Obviously, the larger the number of samples, the higher the accuracy. Since we are now taking multiple
samples, it is possible to measure the speed of the device in the same operating mode.
Replacing the original speed detection mode is a mode in which the user can change the number of samples to
use for the distance calculation. The user can increase or decrease the number of samples by multiples of 50,
with 50 being the minimum. Our calibration mode was still implemented.
Background math
The math required for our project is very simple. The speed of sound is 340.29 m/s at sea level. This equates to
340,290 millimeters per second. Taking the inverse of this, the time necessary for a sound wave to travel one
millimeter is 2.9387 microseconds. By counting the time between the ping and pong, the distance traveled by
the pulse can be determined. One has to keep in mind however, that the distance traveled is actually twice the
distance between the device and the object (since the pulse travels in one direction, is reflected, and travels back
in the other direction).
The speed calculation is simply the first derivative with respect to time of the position (dx/dt). Two pulses are
sent and received at a known time interval and the difference in distances is used to determine the speed of the
device relative to the object. More specifically, (x2 – x1)/(t2 – t1) = speed.
Hardware/software tradeoffs
Since the basic principle of a rangefinder is rather straight forward (send pulse, receive pulse, calculate distance
based on time difference) we decided to stick with the Atmel Mega32 MCU that we have been using the entire
semester. This MCU can easily handle the software needed for the rangefinder and there was no reason not to
use it. Using a different MCU would require learning all the intricacies associated with it and was not practical.
Initially, we wanted to try an infrared or laser rangefinder. However, since we were going to use a Mega32 with
a 16 MHz crystal, this wouldn’t be possible. The speed of light is 3e8 m/s, meaning the time to travel one meter
is 3.33 nanoseconds. With a 16 MHz crystal, the time resolution of the Mega32 is 62.5 nanoseconds. This
means that a rangefinder based on a light wave would be accurate to only 18.75 meters. Definitely not practical!
The alternative we chose was to use a 40 kHz ultrasound transducer pair. Ultrasonic transducers are relatively
cheap (the ones we bought cost less than $7 for the pair) and easy to acquire. At 40 kHz, the human user would
not be able to hear the pulse (human hearing is limited to approximately 20 Hz to 20 kHz) so irritancy is not an
issue.
As stated earlier, the time resolution of sound is only about 3 microseconds, so speed and fast execution time of
code was not of great importance (compared with some other application like NTSC video signals). This made
ultrasound an easy decision.
Another hardware tradeoff that was taken into account when designing and construction the rangefinder was a
matter of gain. When testing the transducers with a signal generator, we noticed that the effective range was
proportional to the input voltage of the transmitting transducer.
Simply put, the higher the input voltage, the farther its signal could travel. Since the transmitter was being fed a
signal from an MCU port pin, it would only receive 5 volts. This resulted in a poor range using the signal
generator while testing (a range of only a few centimeters). By increasing the voltage to around 20 volts using
the signal generator, we were able to get a range in excess of 1.5 meters. The tradeoff here comes down to the
power supply used when making the device completely portable. We could have used a single 9 volt battery, but
the gain on the transducer would be less than 2 to 1. Using two 9 volt batteries in series (giving a total of 18
volts) would give us a gain of slightly more than 3 to 1 (18 to 5, more specifically). Unfortunately, this makes
the device heavier and cost more to operate. Initially, we decided that increased range was more of a pro than
the con of one more battery. Unfortunately, using 18 volts for the gain ended up not working as desired. More
on this later.
The main software tradeoff was using dx/dt to calculate distance rather than using Doppler shifts. Doppler shifts
would have required measuring the frequencies of the square waves as well as determining frequency shifts.
This would have required code almost as difficult to write as our whole project so we just decided to measure
speed using dx/dt from elementary physics.
Another tradeoff was the decision to use CodeVision C instead of assembly to program the MCU. Assembly
most likely would have resulted in more accurate timing and thus distance measurements, however using C had
the benefit of being more user friendly in regards to programming. Using C enabled us to save time while
programming and allowed us to find bugs in the code more easily.
Design standards
Since our project is a ground-up portable device that does not interact with any other devices, there are no such
design standards (IEEE, ISO, ANSI, etc.) that were considered when designing our rangefinder. Since the
program for the MCU was written in the CodeVision C programming language, that could be considered the
only standard used in the development of the rangefinder.
LCD (Liquid crystal display)
The JHD162A dot-matrix liquid crystal display controller and driver LSI displays alphanumerics,
Japanese kana characters, and symbols. It can be configured to drive a dot-matrix liquid crystal display under
the control of a 4- or 8-bit microprocessor. Since all the functions such as display RAM, character generator,
and liquid crystal driver, required for driving a dot-matrix liquid crystal display are internally provided on one
chip, a minimal system can be interfaced with this controller/driver. A single JHD162A can display up to one
16-character line or two 16-character lines. The JHD162A has pin function compatibility with the HD44780S
which allows the user to easily replace an LCD-II with a JHD162A. The JHD162A character generator ROM is
extended to generate 208 5 ´ 8 dot character fonts and 32 5 ´ 10 dot character fonts for a total of 240 different
character fonts. The low power supply (2.7V to 5.5V) of the JHD162A is suitable for any portable batterydriven product requiring low power dissipation.
LCD screen consists of two lines with 16 characters each. Every character consists of 5x8 or 5x11 dot matrix.
This book covers 5x8 character display, which is indeed the most commonly used one.
Pin assignment. The pin assignment shown in, Table 2.1. is the industry standard for character LCD-modules
with a maximum of 80 characters. The pin assignment shown in Table 2 is the industry standard for character
LCD-modules with more than 80 characters.
To locate pin 1 on a module check the manufacturers datasheet!
Table 2.1., Pin assignment for <= 80 character displays
Pin number
1
2
3
4
Symbol
Vss
Vcc
Vee
RS
Level
0/1
I/O
I
5
R/W
0/1
I
6
7
8
9
10
11
12
13
14
E
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7
1, 1->0
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
I
I/O
I/O
I/O
I/O
I/O
I/O
I/O
I/O
POWER SUPPLY CIRCUIT DIAGRAM
Remarks:
- DDRAM = Display Data RAM.
- CGRAM = Character Generator RAM.
- DDRAM address corresponds to cursor position.
Function
Power supply (GND)
Power supply (+5V)
Contrast adjust
0 = Instruction input
1 = Data input
0 = Write to LCD module
1 = Read from LCD module
Enable signal
Data bus line 0 (LSB)
Data bus line 1
Data bus line 2
Data bus line 3
Data bus line 4
Data bus line 5
Data bus line 6
Data bus line 7 (MSB)
- * = Don't care.
- ** = Based on Fosc = 250kHz.
Table 2.4. Bit names
Bit name
Setting / Status
I/D
0 = Decrement cursor position 1 = Increment cursor position
S
0 = No display shift
1 = Display shift
D
0 = Display off
1 = Display on
C
0 = Cursor off
1 = Cursor on
B
0 = Cursor blink off
1 = Cursor blink on
S/C
0 = Move cursor
1 = Shift display
R/L
0 = Shift left
1 = Shift right
DL
0 = 4-bit interface
1 = 8-bit interface
N
0 = 1/8 or 1/11 Duty (1 line) 1 = 1/16 Duty (2 lines)
F
0 = 5x7 dots
1 = 5x10 dots
BF
0 = Can accept instruction
1 = Internal operation in progress
Table 2.3. HD44780 instruction set
Instruction
Code
RS
R/
W
Description
DB
7
DB
6
DB
5
DB
4
DB
3
DB
2
DB
1
DB
0
Execution
time**
Clear display
0
0
0
0
0
0
0
0
0
1
Clears display and returns cursor to the
home position (address 0).
1.64mS
Cursor home
0
0
0
0
0
0
0
0
1
*
Returns cursor to home position
(address 0). Also returns display being
shifted to the original position.
DDRAM contents remains unchanged.
1.64mS
Entry mode
set
0
0
0
0
0
0
0
1
I/D
S
Sets cursor move direction (I/D),
specifies to shift the display (S). These
operations are performed during data
read/write.
40uS
Display
On/Off
control
0
0
0
0
0
0
1
D
C
B
Sets On/Off of all display (D), cursor
On/Off (C) and blink of cursor position
character (B).
40uS
Cursor/displa
y shift
0
0
0
0
0
1
S/C
R/L
*
*
Sets cursor-move or display-shift
(S/C), shift direction (R/L). DDRAM
contents remains unchanged.
40uS
Function set
0
0
0
0
1
DL
N
F
*
*
Sets interface data length (DL),
number of display line (N) and
character font(F).
40uS
Set CGRAM
address
0
0
0
1
CGRAM address
Sets the CGRAM address. CGRAM
data is sent and received after this
setting.
40uS
Set DDRAM
address
0
0
1
DDRAM address
Sets the DDRAM address. DDRAM
data is sent and received after this
setting.
40uS
Read busyflag and
address
counter
0
1
BF
CGRAM / DDRAM address
Reads Busy-flag (BF) indicating
internal operation is being performed
and reads CGRAM or DDRAM
address counter contents (depending
on previous instruction).
0uS
Write to
CGRAM or
DDRAM
1
0
write data
Writes data to CGRAM or DDRAM.
40uS
Read from
CGRAM or
DDRAM
1
1
read data
Reads data from CGRAM or DDRAM.
40uS
ASCII CHARACTER CODE
Specifications:
Characters: 16 x 2
Character Resolution: 5 x 8
Connection: Standard 16 PIN interface
Backlight: White
Voltage Supply: 5V
Dimensions: 3.6 cm x 8 cm x 1 cm (H x W x D)
HD44780 Compatible
Interface Connection:
PIN 1: Vss - GND
PIN 2: VDD - +5V VCC
PIN 3: Vo - Contrast Adjustment
PIN 4: RS - Reset
PIN 5: R/W - Read/Write
PIN 6: E - Enable Signal
PIN 7: DB0 Data 0
PIN 8: DB1 Data 1
PIN 9: DB2 Data 2
PIN 10: DB3 Data 3
PIN 11: DB4 Data 4
PIN 12: DB5 Data 5
PIN 13: DB6 Data 6
PIN 14: DB7 Data 7
PIN 15: Backlight +5V VCC
PIN 16: Backlight GND
Binary
Oct
Dec
Hex
Abbr
[a]
[b]
[c]
000 0000
000
0
00
NUL
␀
^@
\0
000 0001
001
1
01
SOH
␁
^A
Start of Header
000 0010
002
2
02
STX
␂
^B
Start of Text
000 0011
003
3
03
ETX
␃
^C
End of Text
000 0100
004
4
04
EOT
␄
^D
End of Transmission
000 0101
005
5
05
ENQ
␅
^E
Enquiry
000 0110
006
6
06
ACK
␆
^F
Acknowledgment
000 0111
007
7
07
BEL
␇
^G
\a
Bell
000 1000
010
8
08
BS
␈
^H
\b
Backspace[d][e]
000 1001
011
9
09
HT
␉
^I
\t
Horizontal Tab[f]
000 1010
012
10
0A
LF
␊
^J
\n
Line feed
000 1011
013
11
0B
VT
␋
^K
\v
Vertical Tab
000 1100
014
12
0C
FF
␌
^L
\f
Form feed
000 1101
015
13
0D
CR
␍
^M
\r
Carriage return[g]
000 1110
016
14
0E
SO
␎
^N
Shift Out
000 1111
017
15
0F
SI
␏
^O
Shift In
001 0000
020
16
10
DLE
␐
^P
Data Link Escape
Name
Null character
001 0001
021
17
11
DC1
␑
^Q
Device Control 1 (oft. XON)
001 0010
022
18
12
DC2
␒
^R
Device Control 2
001 0011
023
19
13
DC3
␓
^S
Device Control 3 (oft. XOFF)
001 0100
024
20
14
DC4
␔
^T
Device Control 4
001 0101
025
21
15
NAK
␕
^U
Negative Acknowledgement
001 0110
026
22
16
SYN
␖
^V
Synchronous idle
001 0111
027
23
17
ETB
␗
^W
End of Transmission Block
001 1000
030
24
18
CAN
␘
^X
Cancel
001 1001
031
25
19
EM
␙
^Y
End of Medium
001 1010
032
26
1A
SUB
␚
^Z
Substitute
001 1011
033
27
1B
ESC
␛
^[
001 1100
034
28
1C
FS
␜
^\
File Separator
001 1101
035
29
1D
GS
␝
^]
Group Separator
001 1110
036
30
1E
RS
␞
^^[j]
Record Separator
001 1111
037
31
1F
US
␟
^_
Unit Separator
111 1111
177
127
7F
DEL
␡
^?
Delete[k][e]
\e[h]
Escape[i]
MICRO CONTROLLER PIC16F877A
Circumstances that we find ourselves in today in the field of microcontrollers had their beginnings in the
development of technology of integrated circuits. This development has made it possible to store
hundreds of thousands of transistors into one chip. That was a prerequisite for production of
microprocessors, and the first computers were made by adding external peripherals such as memory,
input-output lines, timers and other. Further increasing of the volume of the package resulted in creation
of integrated circuits. These integrated circuits contained both processor and peripherals. That is how the
first chip containing a microcomputer, or what would later be known as a microcontroller came about.
`
PIN OUTS OF MICROCONTROLLER PIC16F877A
FEATURES OF PIC16F877A MICROCONTROLLER
PIC 16F877A BLOCK DIAGRAM
SOFTWARE USED
MPLAB IDE 8.92
HI-TECH C COMPILER 9,7
MICROCONTROLLER PROGRAMMER
PICKit 2
MICROCONTROLLER PROGRAM
sbit
sbit
sbit
sbit
sbit
sbit
LCD_RS
LCD_EN
LCD_D4
LCD_D5
LCD_D6
LCD_D7
at
at
at
at
at
at
RE0_bit;
RE2_bit;
RB0_bit;
RB1_bit;
RB2_bit;
RB3_bit;
sbit LCD_RS_Direction at TRISE0_bit;
sbit LCD_EN_Direction at TRISE2_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char txt3[] = "Ultrasonic Radar";
char txt4[] = "Water Level Cont.";
int water_level, set_water_level = 1500, error_level, discharge, radar_reading, valve_open,
level_difference ;
int j,u,t,h,th,tth,k,l,m,initial = 0,final,movement,temp;
char i;
// Loop variable
unsigned int hAngle=0;
unsigned short vAngle=0, speed=0;
void Move_Delay()
{
// Function used for text moving
Delay_ms(500);
// You can change the moving speed here
}
char getChar(void)
{
while(!UART1_Data_Ready());
return UART1_Read();
}
void main()
{
char temp; char *ptr;
char textRes[9];
ADCON1 = 15;
TRISE = 0;
PORTE = 0;
TRISA = 0;
PORTA = 0;
TRISB = 0;
PORTB = 0;
TRISC = 0x0F;
PORTC = 0;
TRISD = 0x00;
PORTD = 0;
Lcd_Init();
// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt3);
// Clear display
// Cursor off
// Write text in first row
Lcd_Out(2,1,txt4);
Delay_ms(2000);
l=m=0;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"PULSE");
Lcd_Out(2,1,"PULSE");
while(Button(&PORTC, 1, 1, 1))
{
PORTB = 0b00001100;
Delay_ms(5);
PORTB = 0b00000110;
Delay_ms(5);
PORTB = 0b00000011;
Delay_ms(5);
PORTB = 0b00001001;
Delay_ms(5);
// Write text in second row
h = (l / 100) % 10;
t = (l / 10) % 10;
u = l % 10;
Lcd_Chr(1, 9,h+48);
Lcd_Chr(1, 10,t+48);
Lcd_Chr(1, 11,u+48);
l++;
}
Delay_ms(2000);
while(Button(&PORTC, 0, 1, 1))
{
PORTB = 0b00001001;
Delay_ms(5);
PORTB = 0b00000011;
Delay_ms(5);
PORTB = 0b00000110;
Delay_ms(5);
PORTB = 0b00001100;
Delay_ms(5);
h = (m / 100) % 10;
t = (m / 10) % 10;
u = m % 10;
Lcd_Chr(2, 9,h+48);
Lcd_Chr(2, 10,t+48);
Lcd_Chr(2, 11,u+48);
m++;
}
for(l=0 ; l <= 125 ; l++)
{
PORTB = 0b00001100;
Delay_ms(4);
PORTB = 0b00000110;
Delay_ms(4);
PORTB = 0b00000011;
Delay_ms(4);
PORTB = 0b00001001;
Delay_ms(4);
}
//while(1);
PORTB = 0b00000000;
UART1_Init(9600);
while(1)
{
PORTB = 0b00000000;
temp = getChar();
if(temp == 13)
{
j=0;
while(j <= 8)
{
textRes[j] = getChar();
j++;
}
u = textRes[5] - 48;
t = textRes[4] - 48;
h = textRes[2] - 48;
th = textRes[1] - 48;
tth = textRes[0] - 48;
radar_reading = ((tth*10000)+(th*1000)+(h*100)+(t*10)+(u));
if(radar_reading > 2900)
{
Lcd_Out(1,1,"lvl out of range");
}
else
{
water_level = 2900 - radar_reading;
tth = (water_level / 10000) % 10;
th = (water_level / 1000) % 10;
h = (water_level / 100) % 10;
t = (water_level / 10) % 10;
u = water_level % 10;
//Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Wtr_Lvl=");
Lcd_Chr(1, 9,tth+48);
Lcd_Chr(1, 10,th+48);
Lcd_Chr(1, 11,h+48);
Lcd_Chr(1, 12,46);
Lcd_Chr(1, 13,t+48);
Lcd_Chr(1, 14,u+48);
Lcd_Out(1, 15,"cm");
level_difference = set_water_level - water_level;
if(level_difference > 0 && level_difference < 500)
{
// valve close
final=level_difference;
movement=final - initial;
if(movement > 0)
{
temp=movement/5;
if(temp>0)
{
for(temp=temp;temp>0;temp--)
{
PORTB = 0b00001001;
Delay_ms(5);
PORTB = 0b00000011;
Delay_ms(5);
PORTB = 0b00000110;
Delay_ms(5);
PORTB = 0b00001100;
Delay_ms(5);
}
}
}
if(movement < 0)
{
//doub = fabs(-1.3);
//movement = fabs(movement);
temp=movement/5;
if(temp < 0)
{
for(temp=temp;temp<0;temp++)
{
PORTB = 0b00001100;
Delay_ms(5);
PORTB = 0b00000110;
Delay_ms(5);
PORTB = 0b00000011;
Delay_ms(5);
PORTB = 0b00001001;
Delay_ms(5);
}
// valve 50% open
}
}
initial = final;
}
if(level_difference < 0 && -500 < level_difference)
{
// valve close
final = fabs(level_difference);
movement = final - initial;
if(movement > 0)
{
temp = movement / 5;
for(;temp>0;temp--)
{
PORTB = 0b00001001;
Delay_ms(5);
PORTB = 0b00000011;
Delay_ms(5);
PORTB = 0b00000110;
Delay_ms(5);
PORTB = 0b00001100;
Delay_ms(5);
}
}
if(movement < 0)
{
movement = fabs(movement);
temp = movement / 5;
for(;temp>0;temp--)
{
PORTB = 0b00001100;
Delay_ms(5);
PORTB = 0b00000110;
Delay_ms(5);
PORTB = 0b00000011;
Delay_ms(5);
PORTB = 0b00001001;
Delay_ms(5);
}
// valve 50% open
}
initial = final;
}
}
tth = (set_water_level / 10000) % 10;
th = (set_water_level / 1000) % 10;
h = (set_water_level / 100) % 10;
t = (set_water_level / 10) % 10;
u = set_water_level % 10;
Lcd_Out(2,
Lcd_Chr(2,
Lcd_Chr(2,
Lcd_Chr(2,
Lcd_Chr(2,
Lcd_Chr(2,
Lcd_Chr(2,
Lcd_Out(2,
1,"Set_Lvl=");
9,tth+48);
10,th+48);
11,h+48);
12,46);
13,t+48);
14,u+48);
15,"cm");
Delay_ms(1000);
}
if (Button(&PORTD, 0, 1, 1))
{
while(1)
{
if (Button(&PORTD, 1, 1, 1) && set_water_level < 30000)
{
set_water_level = set_water_level + 10;
tth = (set_water_level / 10000) % 10;
th = (set_water_level / 1000) % 10;
h = (set_water_level / 100) % 10;
t = (set_water_level / 10) % 10;
u = set_water_level % 10;
Lcd_Out(2, 1,"Set_Lvl=");
Lcd_Chr(2, 9,tth+48);
Lcd_Chr(2, 10,th+48);
Lcd_Chr(2, 11,h+48);
Lcd_Chr(2, 12,46);
Lcd_Chr(2, 13,t+48);
Lcd_Chr(2, 14,u+48);
Lcd_Out(2, 15,"cm");
Delay_ms(100);
}
if (Button(&PORTD, 2, 1, 1) && set_water_level > 0)
{
set_water_level = set_water_level - 10;
tth = (set_water_level / 10000) % 10;
th = (set_water_level / 1000) % 10;
h = (set_water_level / 100) % 10;
t = (set_water_level / 10) % 10;
u = set_water_level % 10;
Lcd_Out(2, 1,"Set_Lvl=");
Lcd_Chr(2, 9,tth+48);
Lcd_Chr(2, 10,th+48);
Lcd_Chr(2, 11,h+48);
Lcd_Chr(2, 12,46);
Lcd_Chr(2, 13,t+48);
Lcd_Chr(2, 14,u+48);
Lcd_Out(2, 15,"cm");
Delay_ms(100);
}
if (Button(&PORTD, 3, 1, 1))
{
break;
}
}
}
}
}
POWER SUPPLY
Types of Power Supply
There are many types of power supply. Most are designed to convert high voltage AC mains electricity
to a suitable low voltage supply for electronics circuits and other devices. A power supply can by
broken down into a series of blocks, each of which performs a particular function.
For example a 5V regulated supply:
Each of the blocks is described in more detail below:




Transformer - steps down high voltage AC mains to low voltage AC.
Rectifier - converts AC to DC, but the DC output is varying.
Smoothing - smoothes the DC from varying greatly to a small ripple.
Regulator - eliminates ripple by setting DC output to a fixed voltage.
Power supplies made from these blocks are described below with a circuit diagram and a graph of their
output:
Dual Supplies
Some electronic circuits require a power supply with positive and negative outputs as well as zero volts
(0V). This is called a 'dual supply' because it is like two ordinary supplies connected together as shown
in the diagram. Dual supplies have three outputs, for example a ±9V supply has +9V, 0V and -9V
outputs.
Transformer only
The low voltage AC output is suitable for lamps, heaters and special AC motors. It is not suitable for
electronic circuits unless they include a rectifier and a smoothing capacitor.
Transformer + Rectifier
The varying DC output is suitable for lamps, heaters and standard motors. It is not suitable for
electronic circuits unless they include a smoothing capacitor.
Transformer + Rectifier + Smoothing
The smooth DC output has a small ripple. It is suitable for most electronic circuits.
Transformer + Rectifier + Smoothing + Regulator
The regulated DC output is very smooth with no ripple.
It is suitable for all electronic circuits.
Transformer
Transformers convert AC electricity from one voltage to another with little loss of power. Transformers
work only with AC and this is one of the reasons why mains electricity is AC.
Step-up transformers increase voltage, step-down transformers reduce voltage. Most power supplies
use a step-down transformer to reduce the dangerously high mains voltage (230V in UK) to a safer low
voltage.
The input coil is called the primary and the output coil is called the
secondary. There is no electrical connection between the two coils,
instead they are linked by an alternating magnetic field created in the
soft-iron core of the transformer. The two lines in the middle of the
circuit symbol represent the core.
Transformer
Transformers waste very little power so the power out is (almost)
circuit symbol
equal to the power in. Note that as voltage is stepped down current is
stepped up. The ratio of the number of turns on each coil, called the
turns ratio, determines the ratio of the voltages. A step-down
transformer has a large number of turns on its primary (input) coil
which is connected to the high voltage mains supply, and a small
number of turns on its secondary (output) coil to give a low output
voltage.
Vp
turns ratio =
Np
=
Vs
power out = power in
and
Ns
Vp = primary (input) voltage
Np = number of turns on primary coil
Ip = primary (input) current
Vs × Is = Vp × Ip
Vs = secondary (output) voltage
Ns = number of turns on secondary coil
Is = secondary (output) current
Rectifier
There are several ways of connecting diodes to make a rectifier to convert AC to DC. The
bridge rectifier is the most important and it produces full-wave varying DC. A full-wave rectifier can also
be made from just two diodes if a centre-tap transformer is used, but this method is rarely used now
that diodes are cheaper. A single diode can be used as a rectifier but it only uses the positive (+) parts
of the AC wave to produce half-wave varying DC.
Bridge rectifier
A bridge rectifier can be made using four individual diodes, but it is also available in special packages
containing the four diodes required. It is called a full-wave rectifier because it uses all the AC wave
(both positive and negative sections). 1.4V is used up in the bridge rectifier because each diode uses
0.7V when conducting and there are always two diodes conducting, as shown in the diagram below.
Bridge rectifiers are rated by the maximum current they can pass and the maximum reverse voltage
they can withstand (this must be at least three times the supply RMS voltage so the rectifier can
withstand the peak voltages). Please see the Diodes page for more details, including pictures of bridge
rectifiers.
Bridge rectifier
Output: full-wave varying DC
Alternate pairs of diodes conduct, changing over
the connections so the alternating directions of
AC are converted to the one direction of DC.
(using all the AC wave)
Single diode rectifier
A single diode can be used as a rectifier but this produces half-wave varying DC which has gaps when
the AC is negative. It is hard to smooth this sufficiently well to supply electronic circuits unless they
require a very small current so the smoothing capacitor does not significantly discharge during the
gaps. Please see the Diodes page for some examples of rectifier diodes.
Single diode rectifier
Output: half-wave varying DC
(using only half the AC wave)
Smoothing
Smoothing is performed by a large value electrolytic capacitor connected across the DC supply to act
as a reservoir, supplying current to the output when the varying DC voltage from the rectifier is falling.
The diagram shows the unsmoothed varying DC (dotted line) and the smoothed DC (solid line). The
capacitor charges quickly near the peak of the varying DC, and then discharges as it supplies current to
the output.
Note that smoothing significantly increases the average DC voltage to almost the peak value (1.4 ×
RMS value). For example 6V RMS AC is rectified to full wave DC of about 4.6V RMS (1.4V is lost in the
bridge rectifier), with smoothing this increases to almost the peak value giving 1.4 × 4.6 = 6.4V smooth
DC.
Smoothing is not perfect due to the capacitor voltage falling a little as it discharges, giving a small
ripple voltage. For many circuits a ripple which is 10% of the supply voltage is satisfactory and
the equation below gives the required value for the smoothing capacitor. A larger capacitor will
give less ripple. The capacitor value must be doubled when smoothing half-wave DC. Smoothing
capacitor for 10% ripple, C =
5×
Io
Vs ×
f
C = smoothing capacitance in farads (F)
Io = output current from the supply in amps (A)
Vs = supply voltage in volts (V), this is the peak value of the unsmoothed DC
f = frequency of the AC supply in hertz (Hz), 50Hz in the UK
Regulator
Voltage regulator ICs are available with fixed (typically 5, 12 and 15V) or variable output voltages. They
are also rated by the maximum current they can pass. Negative voltage regulators are available, mainly
for use in dual supplies. Most regulators include some automatic protection from excessive current
('overload protection') and overheating ('thermal protection'). Many of the fixed voltage regulator ICs
has 3 leads and look like power transistors, such as the 7805 +5V 1A regulator shown on the right.
They include a hole for attaching a heat sink if necessary. Please see the website for more information
about voltage regulator ICs.
Making of a Printed Circuit Board (PCB)
PRINTED CIRCUIT BOARDS - Background
Printed circuit boards (PCB's) are laminates. This means that they are made from
two or more sheets of material stuck together; often copper and fibreglass.
Unwanted areas of the copper are etched away to form conductive lands or tracks
which replace the wires carrying the electric currents in other forms of
construction. Some parts of the side with copper tracks is coated with solder resist
(usually green in colour) to prevent solder sticking to those areas where it is not
required. This avoids unwanted solder bridges between tracks. Sometimes the
boards are double-sided with copper tracks on both sides. Tracks on one side can
be joined to tracks on the other by means of wire links. Plated through holes are
available which do the same thing but these make the PCB more expensive.
Components are stuffed into the board by hand or by pick and place machines.
Soldering is done by hand or by flow wave soldering where the PCB passes over a
wave of molten solder. Most recent PCB's use surface mount techniques where
components are on the same side of the board as the tracks. Components are
stuck to the board with adhesive and the solder caused to flow by heating the
board in a hot gas or by some other technique. When fitting components ensure
that they are orientated correctly and lay flat on the board unless otherwise
stated. When the board is assembled avoid flexing it which may crack tracks.
Avoid touching the board which may cause contamination due to dirty fingers or
damage due to static electricity carried on your body. It is best to handle PCB's by
holding them by two edges only, between thumb and forefinger
MAKING A PCB
The design layout of the PCB is done on the computer using CAD; the program is Easy PC
or EPCPROX. The layout is printed out on a transparent A4 size sheet called acetate, which
is especially used for the purpose. This is done in the same way as printing out a word
document. Care must be taken to ensure the circuit layout will be to scale and won't be
too big to fit on the sheet. The layer to be printed out must be defined and pad holes
must be set to 'Avoid' so as black dots and not rings are printed to indicate holes to be
drilled. The base material is FR4 epoxy all woven glass laminate, thickness 1.6mm with
copper foil cladding 1 oz per sq. ft. The surface resistance is 100,000 Mega ohms.
Photo-resist is positive working sensitive to ultra violet light with a developed image of
blue/green tint.
The copper-clad laminate board consists of a layer of copper, covered over by a
layer of green resin called photo-resist. The protective black plastic tape, that
protects the copper laminate from scratches, is removed to reveal green positive
photo-resist covering the copper.
The printout mask of the image (on acetate) is put over the photo-resist face
down, so a mirror image of the circuit layout can be seen over the photo-resist
side of the laminated board. On single sided boards this is important because the
PCB is designed from looking down from the component side, but the tracks are on
the opposite side of the laminated board on the copper side, therefore a mirror
image of the PCB layout must be seen. With double sided PCB manufacture, the
board is put between two sheets of acetate.
One sheet having the design for the top layer and the other sheet having the
design for the bottom layer.
Both sheets are taped together or stapled at the edges ensuring alignment of the
printouts. Double-sided copper coated laminate is put between the sheets of
acetate so that the two images are lined up on the laminate. With single a sided
board, the acetate is placed over the photo-resist side. In each case the laminate
and acetate are enclosed under ultraviolet light and agitated for 2 to 8 minutes.
DEVELOPING
A solution of Liquid photo-resist Developer concentrate is mixed in a beaker with 1
part developer to 9 parts water, total 500mls and poured into a basin.
A beeper will sound when the 2 minutes are up, the board is taken out of the UV
enclosure, and (the acetate is not required any more). The green photo-resist that was
exposed will appear a lighter colour and the darker imprint of the PCB can be seen when
examined closely. The board is put into the solution and the liquid is flowed over and back
on the board. The lighter photo-resist will flow away showing copper and the PCB layout
will be revealed. It will be necessary to wash the board under tap water and clean with
tissue paper to ensure no traces of photo-resist remain on the copper, otherwise etching
would be difficult.
A PCB marker pen can be used to correct any errors such as breaks in the track at this
stage.
ETCHING
Following inspection to satisfaction the board is etched in etchant as follows:
The etching tank is about half the size of the household water tank in the attic. The tank
consists of two compartments. One compartment is 2/3 the size of the other
compartment. The larger compartment consisted of a thermostatically controlled heater
element. Covering the heater is a protective grill mesh. This grill mesh filters the waste
copper away from the heater element .The tank would be filled to the level of the filter
with Ferric Chloride Hexahydrate solution, about 5 litres. The solution is made up of
etchent granules dissolved in water. An electric motor spins a shaft enclosed in a tubular
barrel 18 inches long containing holes perforated on the circumference from top to
bottom. The motor and tube are vertical from the top of the tank so as when switched on,
the motor spins the tube and stirs the solution. The effect is to suction of the solution into
the barrel and to spray the board being etched.
The solution is heated to 50 degree Celsius before use; a light on the control panel will
extinguish when the temperature is reached.
The board held in a clamp the copper side facing the centre of the tank so as to gets the
full force of the spray. On the control panel the timer is set to between 3 to 10 minutes,
depending on the quality of solution and size of the board. A 'start etch button' is
depressed and the display will count down, a beeper will sound when the timer has
reached zero, all exposed copper should be disolved away from the board.
The smaller compartment is plumbed into the water main and is continually
flushed, and is used for washing the board after etching is complete. For doublesided boards, the process is repeated on the opposite side of the board.
STRIPPING (OPTIONAL)
After etching the positive resist 9 (photo-resist) maybe left on the copper to act as
protection. Solder is readily achieved through the resist. This green photo-resist
can be removed using a tube of photo resist stripper, (like shoe polish) and the
PCB washed clean under tap water and dried using tissue paper.
TIN PLATING (OPTIONAL)
This is done to provide a nice finish and to protect the copper from oxidization; also
soldering will appear neater and will flow better.
A solution made up of fine tin powder mixed with water is poured into a basin. The
copper is cleaned to a shiny finish be rubbing using a rubber supplied with the kit.
The board is placed in the solution for 10 minutes. The board should then have a
silver finish.
DRILLING
After cutting the PCB to size around the perimeter using the guillotine, drilling using a 0.9mm drill can now be performed in the workshop.
The board is now ready to stuff with components.
APPLICATIONS
a. Collision avoider
b. Robotic applications
c.
Blind people path finder
d. Object detection.
e. In shipping to find the distant ice bergs.
f.
In RADAR and Army applications.
g. Robotics & Automation
h. Car backing System
i. Exploration
FUTURE PROSPECTS
1. Assistance for the blind
2. Velocity meter
3. Long range and short range mode selection.
4. PC interface.
5. Periphery mapper.
LIMITATIONS
The comparison of the ECHO and REF signals effectively inhibits the large-amplitude signal
received directly from the transmitter at time t=0.2 msec. This method allows the minimum
measurable distance to be as low as approximately 4 in. This method also eliminates the need for an
ADC, as well as the problems associated with defining a threshold value based on some moving
average of echo samples. You can improve the module by adding multiple-echo-detection capability,
which allows a single transmitted ultrasonic pulse to recognize two or more objects at different
distances (Figure 2). You can also incorporate this capability by having the program store the capture
value for the first echo, at time t=5 msec, for example, and then re-enable the input- capture
countdown and wait for the second capture at time t=9.4 msec, and so on. Another possible
improvement is to add servo control to the circuit using one of the internal timers in mode. You can
control hobby servos that you commonly find in radio-controlled toys with a 1- to 2-msec-wide positive
pulse every 20 msec. In PWM mode, the mC’s timers require the loading of only a high width value and
a low width value to generate this type of output signal. If you add servo control, you can use the sonar
module to measure distance in a given direction. (DI #2371)
CONCLUSIONS
This research is significant because it is the first step towards a safe, low-cost system for highly
accurate control of a ground distance measurement. The experimental results presented in this paper
are promising for several reasons. First, a from microcontroller control system was demonstrated using
ULTRASONIC as the only sensor for position and heading. Only one additional sensor—the steering
potentiometer—was used by the controller. Second, a constant gain controller based on a very simple
distance measurement model successfully stabilized and guided the microcontroller along a straight,
predetermined path. Finally, it was found that a ULTRASONIC controller could guide a microcontroller
along straight rows very accurately. The lateral position standard deviation was less than 2.5 cm. in
each of the 8 line tracking tests performed Transitioning from automatic control of a lone farm
microcontroller to automatically controlling the same microcontroller towing an implement is a large
step since the combined system will have more complex dynamics and larger physical disturbances
acting on it. Guiding a distance measurement along curved paths will also present a challenge that has
not been addressed. This work describes a control methodology that was successfully employed to
control a real farm microcontroller to high accuracy. This same methodology, combined with a more
sophisticated dynamic model may be sufficient to control the more complicated microcontrollerimplement system. Further research is currently underway to explore this possibility.
REFERENCES & BIBLIOGRAPHY
1. Bio medical instruments and measurementsi. Leslie Cromwell
2. Linear integrated circuits- Roy Chowdhry
3. Ultrasonic theory and applications – Gooberman
4. John B.Peatman – Design with PIC
Web address- www.windbond.com
Microcontrollers.