Faculty Home - Universiti Teknologi Malaysia

Transcription

Faculty Home - Universiti Teknologi Malaysia
PSZ 19:16 (Pind. 1/07)
UNIVERSITI TEKNOLOGI MALAYSIA
DECLARATION OF THESIS / UNDERGRADUATE PROJECT PAPER AND COPYRIGHT
Author’s full name :
MOHAMAD SHUKRI BIN MOHAMED ZAINUDI
10 FEBRUARY 1988
Date of birth
:
Title
:
Academic Session:
RAT TRAP USING ULTRASONIC SENSOR
2011/1012-2
I declare that this thesis is classified as :
√
CONFIDENTIAL
(Contains confidential information under the Official Secret
Act 1972)*
RESTRICTED
(Contains restricted information as specified by the
organisation where research was done)*
OPEN ACCESS
I agree that my thesis to be published as online open access
(full text)
I acknowledged that Universiti Teknologi Malaysia reserves the right as follows :
1. The thesis is the property of Universiti Teknologi Malaysia.
2. The Library of Universiti Teknologi Malaysia has the right to make copies for the purpose
of research only.
3. The Library has the right to make copies of the thesis for academic exchange.
Certified by :
_______
SIGNATURE
880210-03-5147
______________
(NEW IC NO. /PASSPORT NO.)
Date : 25 JUN 2012
NOTES :
*
SIGNATURE OF SUPERVISOR
DR. KHAIRUL HAMIMAH BT ABAS
NAME OF SUPERVISOR
Date : 25 JUN 2012
If the thesis is CONFIDENTIAL or RESTRICTED, please attach with the letter from
the organisation with period and reasons for confidentiality or restriction.
“I hereby declare that I have read this thesis and
in my opinion this thesis is sufficient in terms of scope and quality for
the purpose of awarding a Bachelor„s Degree of Bachelor of
Engineering (Electrical – Control & Instrumentation)”
Signature
: …………………………………...
Supervisor
: DR HAMIMAH BT ABAS
Date
: 22 JUN 2012
RAT TRAP USING ULTRASONIC SENSOR
MOHAMAD SHUKRI BIN MOHAMED ZAINUDI
Submitted To the Faculty of Electrical Engineering in Partial Fulfilment of
the Requirements for the Award of the Degree of Bachelor of Engineering
(Electrical – Control & Instrumentation)
Faculty of Electrical Engineering
Universiti Teknologi Malaysia
JUNE 2012
iii
i
I declare that this thesis entitled “Rat Trap Using Ultrasonic Sensor” is the result of my
own research except as cited in the references. The thesis has not been accepted for any
degree and is not concurrently submitted in candidature of any other degree.
Signature
: ……………………………………...
Name of Author
: MOHAMAD SHUKRI BIN
MOHAMED ZAINUDI
Date
: 22 JUN 2012
iii
iii
iii
Special dedication to….
To my loving parents
You are all the reasons I am what I am…
To Haji Zainudi, Hajjah Aminah, my brother and all my lovely friends
And those who have helped and supported me….… You are always in my minds…!!
To Dr. Khairul Hamimah Bt Abas
Your valuable help and suggestions imparted…. It makes my special word for you…
" Thank you...."
iv
iv
ACKNOWLEDGEMENT
First of all, I would like to say Alhamdulillah over the willingness of His
Almighty for giving me health, strength and ability to enable me to finish this thesis
until it done successfully.
Besides, I would like to thank and sincere appreciation to my beloved family for
their support, counsel, understandings and encouragement till I stand here. Their
kindness I will never forget till the end.
Then, I also thankful to my supervisor, Dr. Khairul Hamimah Abas, who had
given me his advice, guidance and support constantly from commence till the end of my
final year project.
Sometimes, I had some difficulties in doing this project
especially in technical aspects, but he taught me very tolerant and patiently until I knew
and understand the next step to do my project.
Not forgotten to my friends, Azri, Siti Syahirah and anybody had involve in this
project which are not listed here, thanks for helping me and sharing ideas that related
with my project till this project successfully completed.
v
ABSTRACT
Today, increasing the number of rats worrying many party. Number of rats
increased adverse an effect not only on the public but also to the country economy.
Common rats trap is only able to catch one rat in a certain time. There are two types of
commonly used to rat trap, cage type and pliers type. Cage type have many problems
such as broken doors and can‟t be closed quickly. Pliers type is usually caused rat to die
and the house will smell if not removed quickly. Therefore, this project was built to
overcome the existing weaknesses in the usual traps. For this project, the ultrasonic
sensor used to detect the distance into the rat cage. The distance between the walls and
rats will be calculated then PIC will be chose which door will be closed to get the rat. For
realizing the goal of this project, a cage will be constructed with
three parts.
Microcontroller PIC16F877A will be used to control the whole of this cage. In
conclusion, the purpose of this project is to assist the public in catching rats and thus
overcome the problems mentioned above.
.
vi
vi
ABSTRAK
Pada hari ini, peningkatan angka tikus merisaukan banyak pihak. Peningkatan ini
bukan sahaja merisaukan orang awan malah turut memberi kesan pada ekonomi negara.
Perangkap tikus yang biasa hanya mampu menangkap satu tikus pada satu-satu masa.
Terdapat dua jenis perangkap tikus yang biasa digunakan iaitu jenis sangkar dan jenis
penyepit. Jenis sangkar mempunyai banyak masalah seperti pintunya rosak dan pintu
tidak dapat tutup dengan cepat untuk menangkap tikus. Jenis penyepit pula selalunya
meyebabkan tikus itu mati dan rumah akan menjadi busuk jika ianya tidak dialih dengan
segera. Oleh kerana itu, projek ini dibangunkan untuk mengatasi masalah yang ada pada
perangkap biasa. Untuk projek ini, pengesan ultrasonik digunakan untuk mengesan jarak
tikus yang masuk kedalam sangkar. Selepas pengesan mengira jarak antara tikus dan
dinding maka PIC akan memilih pintu yang mana harus ditutup untuk memerangkap tikus
yang masuk itu. Untuk merialisasikan matlamat projek ini, mikropemproses jenis
PIC16F877A telah dipilih untuk mengawal keseluruhan sangkar. Sebagai kesimpulan,
tujuan utama projek ini dibangunkan adalah untuk membantu orang ramai menangkap
tikus seterusnya meyelesaikan masalah yang telah disebutkan diatas.
vii
vii
vii
TABLE OF CONTENTS
CHAPTER
1
TITLE
PAGE
DECLARATION OF THESIS
ii
DEDICATION
iii
ACKNOWLEDGEMENT
iv
ABSTRACT
v
ABSTRAK
vi
TABLE OF CONTENTS
vii
LIST OF TABLES
xii
LIST OF FIGURES
xiii
LIST OF SYMBOLS AND ABBREVIATIONS
xvi
LIST OF APPENDICES
xix
INTRODUCTION
1.1
Project Background
1
1.2
Problem Statement
3
2
1.3
Project Objectives
viii
viii
viii
3 viii
1.4
Scope of Project
4
1.5
Thesis Layout
4
1.6
Gantt Chart
6
THEORY AND LITERATURE REVIEW
2.1
Chapter Overview
7
2.2
Microcontroller
9
2.2
Range Sensor
14
2.3
LCD Display
17
2.4
MPLAB IDE
21
ix
ix
3
METHODOLOGY
3.1
Introduction
22
3.2
Hardware Assembly
22
3.2.1
Microcontroller PIC 16F877A
23
3.2.2
Ultrasonic Range Sensor
24
(MaxSonar-EZ1)
3.2.3
3.3
3.4
4
LCD Display (JHD 162A)
24
Software Implementation
25
3.3.1
MPLAB IDE Software
25
3.3.3
PICkit 2
26
Hardware and Software Integration Design
27
ELECTRONIC DESIGN
4.1
Introduction
28
4.2
System Block Diagram
29
4.3
Circuit Design
30
4.3.1
The PIC 16F877A
32
4.3.2
Voltage Regulator Circuitry
33
4.3.3
Ultrasonic Sensor EZ1 Circuitry
34
x
4.3.4
5
LCD Display
35
PROGRAMMING
5.1
Introduction
37
5.2
Software Implementation
38
5.2.1
38
MPLAB IDE for Microchip PIC
Microcontroller
5.2.2
5.3
6
PICkit 2 Programmer
39
Algorithm for Speed Control
40
5.3.1
40
Pulse Width Modulation (PWM)
ANALYSIS AND RESULTS
6.1
Introduction
45
6.2
Hardware
45
6.3
Circuitry
46
6.4
Sensor Application on rat cage Systems
47
xi
xi
7
CONCLUSION
7.1
Conclusion
49
7.2
Limitations
50
7.3
Suggestion for Future Development
50
REFERENCES
52
Appendices A-D
54 - 73
xii
xii
xii
LIST OF TABLES
TABLE NO.
TITLE
PAGE
1.1
Activity for semester 1
6
1.2
Activity for semester 2
6
2.1
Result of detection pattern
17
2.2
Pin configuration of LCD
19
6.1
The distance of rat cage system
48
xiii
xiii
xiii
xiii
LIST OF FIGURES
FIGURE NO.
TITLE
PAGE
2.1
Microcontroller overview
11
2.2
Pin diagram PIC 16F877A microcontroller
13
2.3
PIC 16F877A structures
13
2.4
Ultrasonic transmitter and receiver
15
2.5
Maxsonar-EZ1 with structures
16
2.6
Beam characteristics on a 12-inch grid
16
2.6
JHD 162A (16X2) structures
18
2.7
Basic circuit of JHD 162A (16X2)
19
2.8
Overview of MPLAB IDE software
21
3.1
Hardware architecture
23
3.2
Software architecture
25
3.3
MPLAB IDE editor
25
3.4
ICSP programmer and PICkit 2 software
26
4.1
System‟s block diagram
29
4.2
Main flowchart
30
4.3
Schematic diagram for sensor application on
31
Rat cage systems
4.4
System implementation on PCB board
32
4.5
Power supply with voltage regulator circuit diagram
33
4.6
Power supply with voltage regulator on PCB board
34
4.7
Ultrasonic sensor (Maxsonar-EZ1) connection
35
4.8
Actual connection for ultrasonic sensor
35
4.10
Circuit of LCD (2X16 character) connection
36
xiv
xiv
xiv
xv
xv
4.11
Actual connection for LCD display
36
5.1
MPLAB IDE software implementation
39
5.2
PICkit 2 successes to load programming
40
5.3
PIC flowchart
41
5.4
PWM waveform
42
5.5
LCD flowchart
43
5.6
Motor flowchart
44
6.1
Hardware of sensor application on rat
45
cage systems at side view
6.2
Hardware of sensor application on rat
46
cage systems at front view
6.3
The circuit with connection
47
6.4
Cage with initial condition
48
6.5
When rat enter middle door
48
xvi
xvi
xvi
LIST OF SYMBOLS AND ABBREVIATIONS
ADC
-
Automatic Distance Control
GPS
-
Global Positioning System
PWM
-
Pulse Width Modulation
PIC
-
Programmable Integrated Circuit
DC
-
Direct Current
AC
-
Alternated Current LED
RAM
-
Random Access Memory
ROM
-
Read Only Memory
EPROM
-
Erasable Programmable Read Only Memory
EEPROM
-
Electronic Erasable Programmable Read Only Memory
MPLAB
-
Software for Programming
PROTEUS
-
Software for Microcontroller
FYP
-
Final Year Project
CPU
-
Central Processing Unit
RISC
-
Reduced Instruction Simplified Computer
PCB
-
Printed Circuit Board
LCD
-
Liquid Crystal Display
A/D
-
Analog-to-digital converter
DAC
-
Digital-to-analog converter
I/O
-
Input & Output
Hz
-
Hertz
V
-
Voltage
-
Voltage peak to peak
A
-
Ampere
cm
-
Centimeter
RC
-
Resistor & Capacitor
POR
-
Power-On Reset
PWRT
-
Power-Up Timer
CCP1
-
Capture/Compare PWM Module
OST
-
Oscillator Start-Up Timer
WDT
-
Watch Dog Timer
TN / STN
-
Twisted Nematic / Super Twisted Nematic
L
-
Length
R
-
Resistance
xvii
xvii
xvii
xvii
xviii
GND
-
Ground
R/W
-
Read / Write
TTL
-
Transistor-Transistor Logic
PCB
-
Printed Circuit Board
Ω
-
Ohm
USB
-
Universal Serial Bus
T
-
Time Period
CCP1
-
Capture/Compare PWM Module
rpm
-
Rotation Per Minute
Sec
-
Seconds
xix
xix
xix
LIST OF APPENDICES
APPENDIX
TITLE
PAGE
A
Source Code of rat cage Model
54
B
Schematic Diagram of Sensor Application on
66
Rat cage Systems
C
Datasheet of Ultrasonic Sensor – EZ1
68
D
Datasheet of LCD Display (16X2)
71
CHAPTER 1
INTRODUCTION
1.1
Project Background
Today, increasing the number of rats worrying many party. Number of rats
increased adverse an effect not only on the public but also to the country economy. As a
simple example, an increasing number of rats causes loss and damage to the store,
Especially the food sector. When the stored food stocks damaged, the country will be
suffer great losses. The use of ultrasonic sensors is one way that does not give
some
negative affect to humans and animals themselves. Ultrasonic sensors will calculate the
distance between the rats and sensors to send information to the PIC to trap the rat.
2
Problem Eliminate the rat has become a phenomenon among food suppliers.
One of the most serious is when these rat could threaten human health, especially those
who are not particular about cleanliness. In developed industrial Countries, the number
of damage involving the food supply around 30% of the total supply. Nearly 70% of the
damage was caused from rat. Many blame the warehouse keeper because they could not
keep safe the food supply. Although various methods have been carried out but the
problem is still there.
Therefore, many warehouse using rat poison to reduce numbers of this mouse.
But the fact that it is very dangerous way because it can threaten other animals such as
cats and hawks who will die when they eat the rat that had been poisoned. . The project
described in this paper will be helpful to reduce the number of rats if Implemented in real
life applications. This project is designed to detect rat within a specific range and close
door when will be come in this rat cage. This project is implemented on small cage rat
models with some modification and additional hardware resources.
3
1.2
Problem Statement
Many people are complaining about the increasing rat problem. When number of
rats increase, the problems also increase because of that. Health can also be threatened
because cleanness is not properly maintained. In the food industry the amount of rat that
causes a lot of food stored in food stores are damaged. Factor plant foods such as rice,
cereals, chocolate is often stored food problems damaged. This can cause a loss in the
company. Home, especially housing and village area, number of rats that could threaten the
health of many. This is because, these rats carry diseases infected to humans. Various types
of traps have been used to reduce the number of rats, but a lot of problems. Rat poison is not
a perfect solution for rat that died from the poison will cause stink. Cats are also at risk of
death if consuming rat poison.
1.3
Project Objectives
This project is carried out purposely to reduce the number of rat by
designing a rat cage with improved level of efficiency. The objectives of this project
are:
i.
To construct rat trap using ultrasonic sensor.
ii. To build and develop a rat trap using PIC which is able to control door of the rat
trap.
4
1.4
Scope of Project
This project is mainly concerned on development of a rat cage. It is designed to
catch a rat when the rats enter the cage. Scopes for this project is:
i.
To study the principle and application of ultrasonic wave
ii.
To construct the hardware of the circuit until it performs the desired function
iii.
To develop software codes that can control the whole system operation
1.5
Thesis Layout
This thesis consists of seven main chapters explained in detail. In first chapter, it
commence with an introduction of project, problem statement, project objectives and
scope of this project, summary of works followed by Gantt chart.
Chapter 2 discusses on literature review from previous project, especially some
researches‟ project regarding to sensor application on rat cage systems.
Chapter 3 presents the methods undertaken from the beginning until the end of
this project. The discussion on the methodology hardware assembly, software
implementation and system integration between hardware and software is explained in
detail.
5
In Chapter 4 deals with basic design details and common circuit schematic and
various hardware implementation of the system for the most common electrical and
electronic components.
Chapter 5 focuses on the exact programming in this project. This chapter will
discuss the control system development in order to achieve the objectives of the project.
Also will explain about are various programming languages such as BASIC, C, Pascal,
Assembly and etc that allow users to write program in different high-level languages
Analysis and result is discussed in Chapter 6, and finally followed by conclusion,
limitation, and suggestion for future development of this project in Chapter 7.
6
1.6
Gantt Chart
Table 1.1 and Table 1.2 show Gantt chart and the details for the project
that had been implemented for the first and second semester.
Table 1.1 : Activity for semester 1
Weeks
Activities
FYP Briefing
Supervisor's name of FYP list out
Discuss title and scope of project with SV
Search the related information
Literature review
Preparation & complete project proposal
FYP1 proposal presentation
Preparation for seminar FYP1
Preparation for seminar and report FYP1
FYP1 report submission
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
7
Table 1.2 : Activity for semester 2
Weeks
Activities
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Literature Review
Design simulation circuit
Analyze simulation circuit
Analyze results obtain
Hardware development
Preparation for Seminar FYP 2
Presentation of Seminar FYP 2
Thesis Writing
Final Thesis Draft Submission
Thesis Submission (Hardcover
and CD softcopy)
In conclusion, the main part of this chapter was successfully covered. There are
include background of the whole system, the problem statements, project objective, project
scope, project overview and outline thesis
CHAPTER 2
THEORY AND LITERATURE REVIEW
2.1
Chapter Overview
This section will discuss about general information regarding materials,
hardware, software and previous project as a guide to this project. Furthermore, this
research was conducted in order to find the technologies that may be useful for
development of rat cage system.
By analysing previous projects developed by other researchers, there is a lot of
information and guidance attained that is related to improve the limitation and can be
implemented in this project.
These information including terminology, definition, technology, and how the
devices operate will be discussed and explained.
99
2.2
Microcontroller
Microcontrollers are very common component in modern electronics control
systems. It is defined as an integrated electronic computing device that includes three
major components on a single chip microcontroller.
Another term to describe a microcontroller is embedded controller, because the
microcontroller and its support circuits are often built into, or embedded in, the devices
they control. Below is the detail about microcontroller [9]:
i.
Central processing unit (CPU) CPU is the brain of a microcontroller that responsible for fetching the
instruction, decodes it, and then finally executed.
ii.
Memory Random Access Memory (RAM) and read Only Memory (ROM) are
parts of memory. Generally, it used to store data and program.
10
10
iii.
Parallel input/output ports for communications These ports are mainly used to drive/interface various devices among
LCD‟S to a microcontroller.
iv.
Serial interfacing ports Serial ports provide various serial interfaces between microcontroller and
other peripherals like parallel ports.
v.
Timers and counters Microcontrollers have more than one timer and counters. It usually used
for counting external pulses, perform clock functions, pulse generations
making oscillations, modulations and frequency measuring.
vi.
Analog to Digital Converter (ADC) ADC converters use in measurement devices that converting the analog
signal to digital form.
vii.
Digital to Analog Converter (DAC) DAC perform reversal operation of ADC conversion. DAC are mainly
used for controlling analog devices like DC motors, various drives and
much more.
viii.
Interrupt controls The interrupt control are used for providing interrupt (delay) for a
working program either external interrupt or internal interrupt.
ix.
Special functioning block These controllers used only for some special applications namely robotics
and space systems.
11
11
Figure 2.1 is the system overview of PIC16F877A. This overview are as the
outline of this project and various I/O modules connected to the microcontroller.
Figure 2.1 Microcontroller overview
Peripheral Interface Controllers also known as PIC is the advanced
microcontrollers made by microchip technologies. Developer's industrial prefer use PIC
because low cost, large user base, wide range of applications, serial programming
capability, high quality and ease of availability. There are some benefits of PICs, that
are:
i.
CORE – PICs come with 1 of 4 CPU „cores‟.
ii.
PACKAGING –PICs come in a variety of packages.
iii.
SPEED – PICs required a clock to work.
iv.
MEMORY – PIC programming space is different for each chip.
v.
CONTROL REGISTER – PICs use a series of “special function registers” for
controlling peripherals and PIC behaviors.
vi.
PROGRAM MEMORY – PICs have two different types of program storage
(EPROM, FLASH) and PICs use general purpose “file registers” for RAM
(each register is 8 bits for all PICs).
vii.
DIGITAL I/O PORT – All PICs have digital I/O pins, called „Ports‟. Ports have
2 control registers.
12
12
Based on intelligent system of PIC16F877A, this project choose PIC16F877A
microcontroller to develop vehicle model. This controller is widely used in modern
electronic due to the features PIC16F877A datasheet as shown below [10]:
i.
It has high performance Reduced Instruction Simplified Computer (RISC) CPU.
ii.
It has only 35 single word instructions, capability of interrupt (up to 14 sources)
and eight level hardware stack.
iii.
Maximum operating speed is 20 MHz clock input.
iv.
It is able to generate FLASH Program memory up to “8K X 14” words, Data
Memory (RAM) up to “368 X 8” bytes, and EEPROM Data Memory up to “256
X 8” bytes.
v.
It can be obtain low power and high speed CMOS FLASH/EEPROM
technology.
vi.
It can operate in wide voltage range: 2.0V to 5.5V.
vii.
Commercial, Industrial and Extended temperature ranges.
viii.
It has low power consumptions :
a. < 0.6 mA typical @ 3V, 4 MHz.
b. 20 μA typical @ 3V, 32 kHz.
c. < 1μA typical standby current.
ix.
For protection, it has programmable code.
x.
It is reliable to RC oscillator operation.
xi.
It can be obtain power-on reset (POR), power-up timer (PWRT), oscillator startup timer (OST), and watchdog timer (WDT) with its own on chip and power
saving SLEEP mode.
xii.
It has selectable oscillator options.
Figure 2.2 shows the pin diagram for PIC16F877A. The basic structure of a
modern PIC16F877A chip is shown in Figure 2.3.
13
13
Figure 2.2 Pin diagram PIC16F877A microcontroller
Figure 2.3 PIC16F877A structure
These features make it special for more advanced level A/D applications in
automotive, industrial, appliances and consumer applications.
14
14
2 .3
Range Sensor
Nowadays, sensors are essential components of automotive electronic control
systems and commonly used in automotive industry. Sensors are defined as a device
capable of transforming physical into output signals that serve as inputs for control
systems [11].
Ultrasonic and infrared sensors are mostly used to measure distance between two
physical objects. In automotive industry, there are various types of sensor technology
used to measure distance such as ultrasonic sensor, Infra-Red proximity sensors, laser
rangefinder, sonar, and radar, depending on the specification, complexity and budget of
the project.
Among them, the ultrasonic sensor is a sensor that works on the principle of
reflection of sound waves and is used to detect the presence of a particular object in
front of it. It works on the area above the frequency of sound waves from 40 KHz to 400
KHz [12]. Ultrasonic sensor comprises of transmitter and receiver.
For the measurement principle of ultrasonic sensors, it transmits ultrasonic waves
from its sensor head and again receives the ultrasonic waves reflected from an object. In
addition, the position of the object is detected by measuring the length of time from the
transmission to reception of the sonic wave. Figure 2.4 shows the measurement
principle of ultrasonic sensor.
15
15
Figure 2.4
Ultrasonic transmitter and receiver
The theory of the ultrasonic sensor operation is based on echo-location like
SONAR and similar to bats and dolphin use to navigate. The pitch or frequency of the
sound is below the human hearing, which is useful because it provides accuracy and
remains inconspicuous. As sound hits a solid object, it is reflected back creating an echo.
Since the speed of sound is constant, it is possible to determine the distance of the object
you hear an echo from by multiplying the speed of sound by half the time it takes to hear
the echo [13].
In this project, MaxSonar-EZ1 is used in vehicles model. MaxSonar-EZ1 is also
known as ultrasonic Range Finder detects objects in range 0 to 6.45m (254inches) and
even small objects through touching the front of sensor. These sensors are designed to
operate at 42 kHz. Besides, the sensor possesses high output 10Vpp square wave.
However, there are various kinds of interface outputs format where all interfaces are
active simultaneously such as analog voltage output (10mV/inch), serial digital output (0
to 5V) and pulse width output (147uS/inch). Thus, benefits of these sensors is low cost
sonar ranger, quality beam characteristics, lowest power ranger, fast measurement cycle
and more [14].
Figure 2.5 is the diagram of structures MaxSonar-EZ1 from manufacturer‟s data
sheet. Besides, Figure 2.6 shows the beam characteristics of MaxSonar-EZ1 in grid 12
inch. By measuring the beam pattern on the transducers used, the result of detection
pattern is shown in Table 2.1.
16
16
Figure 2.5 MaxSonar-EZ1 with structures
Figure 2.6 Beam characteristics on a 12-inch grid
17
17
Table 2.1 : Result of detection pattern
Beam Pattern
2.4
Diameter
Beam characteristics
A
0.25 inch
Very narrow beam
B
1 inch
Long narrow beam
C
3.25 inch
Long controlled beam
D
11 inch
Long range capability beam
LCD display
LCD stands for "Liquid Crystal Display". It is the technology used for displays
in notebook and other smaller computers. The image on an LCD screen is created by
sandwiching an electrically reactive substance between two electrodes. This colour of
this substance can be changed by increasing or reducing the electrical current. Since
LCD screens are based on the principle of blocking light (rather than emitting it), which
used up much lighter and consume less power than other display technologies.
In this project, JHD 162A (16x2) is selected to display distance when ultrasonic
sensor transmit and receive signal by detected object in front. The LCD is simple and
easily controlled by a microcontroller. 16X2 represent the LCD display have two lines
by 16 characters. This LCD has typical features such as [15]:
18
18
i.
Display content: 2 lines X 16 characters.
ii.
5 X 8 dots with cursor
iii.
Driving mode: 1/16 duty cycle
iv.
Operate in voltage: +5V
v.
Built-in controller
vi.
Available types:
a. TN / STN (yellow green, grey, B/W)
b. Reflective with:
I.
II.
EL / 100VAC, 400HZ
LED / 4.2VDC
Refers to JHD 162A (16x2) datasheet, Figure 2.7 and Figure 2.8 shows the
structures and basic circuit of LCD displays. Finally, the Pin configuration of LCD
shows on Table 2.2.
Figure 2.7 JHD 162A (16x2) structures
19
19
Figure 2.8 Basic circuit of JHD 162A (16x2)
Table 2.2 : Pin configuration of LCD
Pin no.
Symbol
Function
1
Vss
GND
2
Vdd
+3V or +5V
3
Vo
Contrast Adjustment
4
RS
H/L Register Select Signal
5
R/W
6
E
7
DB0
H/L Data Bus Line
8
DB1
H/L Data Bus Line
9
DB2
H/L Data Bus Line
10
DB3
H/L Data Bus Line
11
DB4
H/L Data Bus Line
H/L Read/Write Signal
H → L Enable Signal
20
20
12
DB5
H/L Data Bus Line
13
DB6
H/L Data Bus Line
14
DB7
H/L Data Bus Line
15
A
4.2V for LED/Negative Voltage Output
16
K
Power Supply for B/L (OV)
21
21
2.5
MPLAB IDE
This project used C language to program the microcontroller and MPLAB v8.10
as a compiler to create the source code and hex file for the microcontroller. Figure 2.9
shows overview of MPLAB software.
Figure 2.9 Overview of MPLAB Software.
In conclusion, MPLAB IDE is a software program that runs on a PC to develop
applications for Microchip microcontrollers based on Integrated Development
Environment (IDE). It provides a single integrated “environment” to develop code
for
microcontrollers. MPLAB IDE also serves as a single, unified graphical user
interface for additional Microchip and third party software and hardware development
tools [17].
CHAPTER 3
METHODOLOGY
3.1
Introduction
This chapter story about the methods used to develop the application sensor on
the rat trap cage system. The discussion on the methodology hardware assembly,
software implementation and system integration between hardware and software is
explained in detail.
3.2
Hardware Assembly
The Figure 3.1 shows the hardware architecture. Further explanation will be
discussed in the following subsections.
23
23
Figure 3.1 Hardware architecture
3.2.1
Microcontroller PIC 16F877A
This project utilizes PIC16F877A as consideration of PIC features to implement
into the vehicle following system. The microcontroller will act as a brain of the car
developed. It will control the car speed based on Pulse Width Modulation (PWM) in
programming part.
There are three inputs connected to microcontroller PIC16F877A which are
ultrasonic sensor EZ1, and LCD display (JHD 162A). This type of oscillator for the
PIC can only accommodate max Cristal 40Mhz clock and required 5V power supply.
24
24
3.2.2
Ultrasonic Range Sensor (MaxSonar-EZ1)
In the distance measurement part, ultrasonic range sensor MaxSonar-EZ1 has
been chosen as a device to detect the obstacle object in front and measure the distance
simultaneously. MaxSonar-EZ1 has several advantage compared to other ultrasonic
range sensor that fulfill the requirement of this project system. Some of the advantages
are High Power Output, Noise Rejection, Auto Calibration and Long-Range Medium
Detection Zone.
3.2.3
LCD Display (JHD 162A)
There are two types of LCD display, which are serial port and parallel port. This
project utilizes parallel port LCD as a device to display the measured distance. Parallel
port LCD is more simple and cheap compared to serial port LCD and similar in quality.
LCD used as a user interface in this project. The LCD function is to display numerical
value of distance measurement detected by the sensors. The LCD display the distance in
centimeter and display content 16 characters X 2 rows.
25
25
3.3
Software Implementation
Figure 3.2 below shows the software that architecture is utilized in this project.
Figure 3.2 Software architecture
3.3.1
MPLAB IDE Software
Figure 3.3 MPLAB IDE editor
26
26
MPLAB IDE is a program that uses various kind of language to build the desired
program. The MPLAB IDE is chosen in this project because it is easy to be use and the
programmed created can be apply to the PIC microcontroller. Figure 3.3 shows the
MPLAB IDE Editor.
3.3.2
PICKit 2
Software PICkit 2 requires ICSP PIC programmer and socket to copy and write
the programming into the PIC. Then the PIC can be connected to the circuit board.
Figure 3.4 shows the ICSP Programmer and PICkit 2 software.
Figure 3.4 ICSP Programmer and Pickit 2 software
27
27
3.4
Hardware and Software Integration Design
This project is developed by divided it into several parts. There consist of voltage
regulator circuit, LCD circuit, sensor circuit.
Voltage regulator circuit serve as 12V main power supply which is then
converted to 5V to supply the power to the circuit board as required. LCD circuit serve
as a device to display the desired information designed in this project. The LCD consists
of 16 characters and 2 rows as per LCD JHD 162A data sheet. All the connection is in
accordance with the data sheet.
Next stage is designing sensor circuit. The ultrasonic range sensor MaxSonarEZ1 is used to detect the preceding vehicle and measure the distance. These three circuit
structures are integrated and simulated by using Proteus software.
In conclusion, after completion on the hardware assembly, this project continued
on to the programming process. The programming is accomplished by using MPLAB
and copy and write into the PIC by using PICkit 2 device. The design‟s programming
including distance to be display, sensor application and motor speed. These
programming interpret the following system design.
CHAPTER 4
ELECTRONIC DESIGN
4.1
Introduction
Most electronic products and devices contain similar components, such as
resistors, capacitors, diodes, connectors and wire. Understanding common faults of these
components and how to test the circuit is a pre-requisite for the troubleshooter. The rat
cage avoidance system was described in chapter 1 in terms of design requirements,
technical specifications, and general overview.
This chapter deals with basic design details and common circuit schematic and
various hardware implementation of the system for the most common electrical and
electronic components. One the most important advantage of ultrasonic sensor EZ1 will
become evidence in its easiness of interface to electronic circuitry (digital and analog).
29
29
4.2
System Block Diagram
The block diagram of the rat cage system is presented in Figure 4.1. As shown in
the block diagram, the microcontroller is the brain of the system that controls and
coordinates between the other parts. The switch is controlled by the driver‟s turning
signal when following system is decided.
This cage is divided into three parts. Overall length for this cage is 60cm and
20cm along each part. When the rat entered the cage, the ultrasonic sensor will calculate
distance to ensure which part that rat enter end then will be give signal to the PIC. PIC
will control doors which one need to be closed to catch the rat. LCD will display how
many rat already enter this cage. This cage have three LED it is green for first part,
yellow for second part and red for last part.
Figure 4.1 System‟s Block Diagram
30
30
Figure 4.2
4.3
Main flowchart
Circuit Design
Figure 4.3 shows the complete system schematic diagram for Sensor Application
on Rat Cage. Systems drawn using PROTEUS ISIS Professional. This schematic is
divided into several parts of component. There are:
i.
Microcontroller
ii.
Ultrasonic sensor EZ1
iii.
Voltage regulator
iv.
LCD Display
31
31
Ultrasonic
Sensor EZ1
LCD Display
PIC Microcontroller
Motor Driver
with 5V DC
Motor
Voltage Regulator
Figure 4.3 Schematic Diagrams for Sensor Application on Rat Cage Systems
Each circuit will be explained in detail below. Figure 4.4 shows a photo of the
system realized on donut board.
32
32
LM324
Microcontroller
Voltage
Regulator
Connection
For LCD Display
Figure 4.4 System implementation on donut board
4.3.1
The PIC 16F877A
PIC 16F877A is very popular, cheap and easy to assembled in industry
component. This IC can be reprogrammed and erased up to 10, 000 times. Besides, it has
wide operating voltage range between 2.0V to 5.5V. However, the available voltage is
7.4V from car‟s battery. Voltage level step-down is performed by the voltage regulator
IC LM7805. The 7805 takes a 7.4V input and convert it to 5V output suitable for PIC.
These 5V are fed to one of PIC‟s
pins (11 or 32) which are internally
connected. Ground (0V) connection is required to any of PIC‟s
pins (12 or 32). The
two capacitors, C1 and C2 suppress high- and low-frequency noise on the 5V supply
line. PIC 16F877A connection uses 9 pins of the 33 available Ports pins. There are:
RD2 : Transmitter ultrasonic sensor
RB0 : Receiver ultrasonic sensor
33
33
RD0 : LCD Register Select
RD2 : LCD Enable line.
RD4 - RD7 : LCD Data lines.
Therefore, additional components necessary to operate this PIC such as 5V
power supply adapter, a 20 MHz crystal oscillator and 2 units of 22pF capacitors.
4.3.2
Voltage Regulator Circuitry
Voltage regulator usually having three legs as known as input leg, output leg and
ground leg which converts varying input voltage and produces a constant regulated
output voltage. Therefore, input leg can hold up to 36VDC Common leg (GND) and an
output leg with the regulator‟s voltage. It is also available in a variety of outputs.
The LM78XX series typically has the ability to drive current up to 1A. Usually,
for maximum voltage regulation, a capacitor will add in parallel between the common
leg and the output. Typically a 0.1MF capacitor is used. This eliminates any high
frequency AC voltage that could otherwise combine with the output voltage. Figure 4.5
shows the power supply circuit diagram using voltage regulator and Figure 4.6 shows
power supply with voltage regulator on donut board.
Figure 4.5 Power supply with voltage regulator circuit diagram.
34
34
Figure 4.6
4.3.3
Power supply with voltage regulator on donut board
Ultrasonic Sensor EZ1 circuitry
Ultrasonic sensor EZ1 was designed from the ground up and be able to detect
objects. The circuit for this sensor consists of a set of ultrasonic receiver and transmitter
which operate at the same frequency. One of the most important advantages of this
sensor is that it can detect even small objects up to and touching the front sensor face.
This sensor provides very accurate readings of 0 to 255 inches (0 to 6.45m) in 1
inch increments with little or no dead zone. Besides, this sensor has wide operating
voltage range between 2.5V to 5.5V at frequency 42 kHz. The connection for this sensor
is connected to the analog to digital converter in the microcontroller PIC 16F877A. The
ultrasonic sensor needs three terminal connections such as input voltage, ground and
analog. The RA0 is shown as the input to connect the analog pin of microcontroller. The
resistor connected series with input voltage 5V in order to reduce the noise. Figure
4.7and Figure 4.8 shows the connection design by using Proteus software and the actual
connection to the hardware.
35
35
Figure 4.7
Ultrasonic sensor (MaxSonar-EZ1) connection
Ultrasonic
Sensor EZ1
Figure 4.8 Actual connection for ultrasonic sensor
4.3.4
LCD Display
16X2 Liquid Crystal Display (LCD) is used to display the distance between two
cars. Only 4 inputs from LCD display are connected to the microcontroller. Figure 4.10
and Figure 4.11 show the design and actual connection for LCD display. In order to
control the brightness the LCD display,
connection need to connect with variable
36
36
resistor 5KΩ. The positive
and
connected to the power supply from voltage regulator 5V
to the ground. Below the figure connection for the LCD display for the design
and actual connection the hardware.
Figure 4.10
Circuit of LCD (2x16 Character) connection
PIC 16F877A
LCD display
connection
Figure 4.11
Actual connection for LCD display
CHAPTER 5
PROGRAMMING
5.1
Introduction
Programming is the great language that can be used to create programs to express
algorithms precisely. It is usually split into two components of syntax (form) and
semantics (meaning).
Besides, programming PIC microcontrollers is a simple three-step process such
as writing, compiling, and uploading the code into a microcontroller. There are various
programming languages such as BASIC, C, Pascal, Assembly and etc that allow users to
write program in different high-level languages. This chapter will discuss the control
system development in order to achieve the objectives of the project.
38
38
5.2
Software Implementation
The program for the PIC16F877A microcontroller is written in C and compiled
with the development system from MPLAB software. An implementation of a
programming language provides a way to execute that program on one or more
configurations of hardware and software.
5.2.1
MPLAB IDE for Microchip PIC Microcontroller
MPLAB is software that uses to write the program in C language and assembly
language. Meanwhile, PICkit ICSP Programmer is a hardware programmer that acts as
burner to burn hex file into PIC16F877A program memory. In this project, C language is
used as the main programming language because the language is easier to write and to
understand. So, MPLAB IDE suits the needs as the compiler for this project as it will
change the C file to hex file for the microcontroller. Figure 5.1 shows the software
implementation used in this project. This software has a header file and source file.
39
39
Figure 5.1 MPLAB software implementation
5.2.2
PICkit 2 Programmer
PICkit 2 programmer is the software used to load the program to the
microcontroller. The circuit is interfaced with USB programmer first before the hex file
is loaded into the PIC16F877A. This software can read, write and erase the
programming from the hex file. It is also user friendly. Figure 5.2 indicates the
programming successfully overwrite to the microcontroller.
40
40
Figure 5.2
The PICKit 2 success to load programming
5.3
Algorithm for Speed Control
5.3.1
Pulse Width Modulation (PWM)
The objective of following system is to develop a cage that is able to catch rat
when rat enter this cage. Figure 5.3 is the PIC Control System flowchart showing how
the PIC system operates with the input configures the analog signal.
41
41
Figure 5.3
PIC flowchart
The speed of the DC motor is varied using Pulse Width Modulation (PWM)
signal. Figure 5.4 shows a PWM output which has a period and a duty cycle. From the
data sheet of PIC 16F877A, the PWM signal has some properties of digital signal. There
are;

The signal remains "ON" for some time and "OFF" for some time.

= Time the output remains high.

= Time the output remains low.
42
42

When output is high the voltage is 5V

When output is low the voltage is 0V

T = Time Period =
+
However, equation 5.1 is used to determine the duty cycle of the PWM signal on
CCP1.
Figure 5.4
PWM Waveform
× 100
(5.1)
After the calibration to convert speed to the decimal is done, the relationship
between the first motor and second motor speed is measured. The speed of the following
vehicle was set to 200rpm, in order to maintain the distance.
So, the set point was set to 20cm and the following motor will follow
consistently. The set point is known as fixed distance that is set earlier to follow the
preceding motor. The microcontroller will read the data from the ultrasonic sensor and
the analog value will be converted to digital value. In the programming, the maximum
time the data can read by ultrasonic sensor was 15ms.
43
43
The delay of 15ms was written in the programming. The analog value in the
centimeters will be displayed on the LCD. By using the following programming, the
follower motor can adjust the speed automatically based on the distance measured by
the ultrasonic sensor in the safe mode distance. Figure 5.5 and Figure 5.6 below show
the flowchart for LCD and motor algorithm.
Figure 5.5
LCD flowchart
44
44
Figure 5.6 Motor flowchart
In conclusion, are various programming languages such as BASIC, C, Pascal, Assembly
and etc that allow users to write program in different high-level languages.
CHAPTER 6
ANALYSIS AND RESULTS
6.1 Introduction
In this section, the analysis and result of the project will be discussed in details.
Some of the figure will be included in this section.
6.2
Hardware
Figure 6.1 Hardware of rat cage using ultrasonic sensor at side view
46
46
One Way Door
Figure 6.2 Hardware of rat cage using ultrasonic sensor at bird view
Figure 6.1 and Figure 6.2 shows the side view and the bird view of the
completed project in accordance to the main objective of this project. The main
significant hardware which acts as a brain of this project is the main circuit board for rat
cage system. On the circuit board, the ultrasonic sensor connection act as distance
measurement, and control which door will be closed when rat enter this cage. LCD will
display how many rat that enter cage.
6.3
Circuitry
Figure 6.3 shows the circuit designed which are connected to the developed car.
There are the main circuits and the regulator circuits with the inter-connection, with the
main circuit consist of sensor, LCD display, and connection of power supply.
47
47
This project utilized a donut board for component installation because this board
is easy to obtain at electronic shop. In addition, this board gives easier way to design the
electronic circuit and easy for troubleshoot if any problem occurs.
The ultrasonic
circuit
The counter circuit
The main circuit
Figure 6.3
6.4
The circuit with connection
Sensor Application on Rat Cage Systems
This cage is divided into three parts. Overall length for this cage is 60cm and
20cm along each part. When the rat entered the cage, the ultrasonic sensor will calculate
distance to ensure which part that rat enter end then will be give signal to the PIC. PIC
will control doors which one need to be closed to confine the rat will be caught in this
cage. LCD will display how many rat already enter this cage. This cage have three LED
it is green for first part, yellow for second part and red for last part.
48
48
Table 6.1 : The distance of rat cage system
Distance (cm)
LED
Action
< 40
Red
Door 1,2, will be close
> 40
Red
> 20
Yellow
Door 2,3,4 will be close
Door 4,5 will be close
Figure 6.4 and 6.5 below show how the actual distance are taken to compare with
the LCD display reading.
Figure 6.4 Cage with initial condition.
Figure 6.5 When rat enter middle door
CHAPTER 7
CONCLUSION
7.1
Conclusion
This project has successfully achieved its objective. However, it has some
limitation needed to be improved for future development. In this project, the developed
the cage was perfectly catch rat in great numbers compare to usual trap and maintaining a
safe for all.
From the result and analysis done, it can be shown that the construction of this
cage can solve many problems arising from the rat. However, there are some additions to
be done and recommendations for development in the near future.
50
50
7.2
Limitations
The first limitation of this developed cage is the detection process is
always interrupted due to the beam width of ultrasonic sensor EZ1 is too large. Before
testing the ultrasonic sensor, the surrounding area needs to be cleared from any obstacle
for get the best result.
The second limitation is servo motor is not enough power to lift a lot of doors.
During the testing of cage, the front door has been changed several times to match with
the motor power.
Finally, the third limitation is the inside surface of the cage should be appropriate
to get the right distance. Reflection of ultrasonic wave sensor so it is difficult to control
that why the installation process needs to be done with very carefully. This cage can only
hold 15 rats maximum at once time. This depends on the size of the cage being built.
7.3
Suggestion for Future Development
To overcome the first limitation of this project, it is suggested to utilize the
ultrasonic sensor EZ4 for narrower beam width and lower sensitivity than the EZ1.
Narrower beam width will provide more concentration on obstacle in front rather than
the surrounding.
57
51
On the other hand, by implementing the high power servo motor onto the
developed cage will overcome the second limitation of this project. The high power
servo motor can control many door so the cage no need one motor for each door.
As conculusion, more research on PIC circuit design and development of
programming would give the accurate and precise reading on distance measurement in
this project. Size cage and wood must be select and calculate very well so that ultrasonic
sensors can measure the right distance.
52
52
REFERENCES
1.
RobotFreak, Pacman - Beginners Robot Chassis Challenge. website :
http://letsmakerobots.com/node/21708, 2010/10/11.
2.
Axelson, J., Microcontroller Basics. 6th ed. The Microcontroller Idea Book, ed.
N.J. Englewood Cliffs. 2003: Prentice-Hall.
3.
Microchip, PIC16F87XA Data Sheet. 2003 Microchip Technology Inc.
4.
Norton, H., Transducer fundamentals. Handbook of Transducers.Englewood
Cliffs, NJ: Prentice Hall, 1989, ch. 2.
5.
MaxBotix, The MaxSonar-EZ1 High Performance Sonar Range Finder. Data
Sheet: MaxSonar-EZ1, 2006. v1.0
6.
Vishay, 16 x 2 Character LCD. Data Sheet: LCD-016M002A, 2002.
7.
Microelectronics, S.-T., Push-Pull Four Channel Driver With Diodes. Data
Sheet: L293D Motor Driver, June 1996.
8.
Inc., M.T., MPLAB® IDE USER’S GUIDE. Data Sheet: MPLAB® IDE, 2005
57
53
APPENDIX A
Source Code of Rat Cage Model
57
55
#include <pic.h>
#include <htc.h>
CONFIG(0x3F32);
#defineLCD_RS
RD0
#define LCD_RW
RD1
#define LCD_EN
RD2
#define LCD_LIGHT RD3
#define LCD_DATA PORTD
//D7-D4
#defineLCD_PULSE() ((LCD_EN=1),(LCD_EN=0))
#define BAUD
9600
#define _XTAL_FREQ
20000000
#define sw1
RE0
#define sw2
RE1
#define motor_ra
RC0
#define motor_rb
RC3
#define motor_la
RC4
#define motor_lb
RC5
#define LED_RED
RB1
#define LED_YELLOW
RB2
#define LED_GREEN
RB3
//#define buzzer
RE2
56
56
#define SPEEDL
CCPR1L
#define SPEEDR
CCPR2L
void pic_init(void);
void uart_init(void);
void ultrasonicmotor(void);
void lcd_init(void);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_goto(unsigned char pos);
void lcd_string(const chars);
int read_a2d(unsigned char channel);
void convert(int no, char base);
void display(char number);
void forward(void);
void stop(void);
void backward(void);
char di[10],sonic[40],z;
int counter=0,pwm;
static void interrupt isr(void)
{char i;
if(RCIF==1){
57
57
if(RCREG=='R') t=0;
sonic[t]=RCREG;
t++;}
if(TMR0IF==1){ TMR0IF=0;
if(RB0==1) counter++;
}
if(INTF==1){ INTF=0;
pwm=(int)(51.2*2.54*counter/147);
counter=0;}
}void main(void)
{unsigned char m=0;
LED_GREEN = 0;
LED_RED
= 0;
LED_YELLOW = 0;
pic_init();
//initialize PIC lcd_init();
//initialize LCD lcd_goto(0x00);
//select first line lcd_string(" ULTRASONIC CAR "); //display
string lcd_goto(0x40);
second line
//select
58
58
lcd_string("Distance(cm):
");
//display string
if(!sw1)
{while(!sw1)
m++;
if (m >2) m=0;
ultrasonicmotor()}
}uart_init();
//initialize UART
lcd_goto(0x0);
//select third line
lcd_string("Serial: cm");
//display string
lcd_goto(0x0)
//select fourth line
lcd_string("Pulse width: cm ");
//display string
buzzer = 1;
convert(serial,10);
//in cm
lcd_goto(0x21);
for(i=2;i>=0;i--) display(di[i]);
convert(pwm,10);
//in cm
lcd_goto(0x61);/for(i=2;i>=0;i--) display(di[i])
}void ultrasonicmotor(void)
{int distance,analog,serial,i;; while(1){
analog=(int)(0.25444*4.88888*read_a2d(0));
59
59
convert(analog,10);
//in cm
lcd_goto(0xD);
for(i=2;i>=0;i-) display(di[i]);
for(i=0;i<100;i++)
delay_ms(1);
distance = read_a2d(0);
if (distance > 50){
forward();
// then forward with full speed
SPEED= 255;
SPEED= 255;
LED_GREEN = 1;
LED_RED
=0
LED_YELLOW = 0; }
else if(distance > 45)
{forward()
// then forward with medium speed
SPEED= 235;
SPEED= 235;
LED_YELLOW = 1;
LED_GREEN = 0;
LED_RED
= 0;}
else if(distance > 35){
forward();
// then forward with low speed
60
60
SPEED = 200;
SPEED = 200;
LED_YELLOW = 1;
LED_GREEN = 0;
LED_RED
= 0;
}else if(distance > 25)
{stop();
LED_RED
= 1;
LED_YELLOW = 0;
LED_GREEN = 0;
// stop movement
}else if(distance > 15)
{stop();
LED_RED
= 1;
LED_YELLOW = 0;
LED_GREEN = 0;
// stop movement
Else
{backward();
SPEED = 235;
SPEED = 235;
}
}
// then backward with 75% speed
61
61
void pic_init(void) // motor PWM configuration
PR2 = 255;
T2CON =
// set period register
0b00000100;
CCP1CON = 0b00001100;
// CCP2CON =
0b00001100;
// config for RC1 to generate PWM ( for more detail refer datasheet section
'capture/compare/pwm')
stop();// config for RC2 to generate PWM
TRISA=0b00000001;
TRISB=0b00000001;
TRISC=0b10000000;
TRISE=0b00000000;
OPTION=0b00001000;
ADCON1=0b00000110;
INTCON=0b11110000;//Enable RX interrupt
PORTA=0b00000000;
PORTB=0b00000000;
PORTC=0b00000000;
PORTD=0b00000000;
PORTE=0b00000000;
}
void uartinit(void)
{
62
62
TXSTA=0b10100000; RCSTA=0b10010000;
SPBRG=(int)(_XTAL_FREQ/(64.0*BAUD)-1);
}
/* initialise the LCD - put into 4 bit mode */
void lcd_init(void)
{
delay_ms(15);
//delay for LCD Power Up
lcd_write(0x28);
//function set
lcd_write(0x0C);
//display on/off control
lcd_clear();
//clear screen
lcd_write(0x06);
//entry mode set
LCD_LIGHT=1;
/* write a byte to the LCD in 4 bit mode */
void lcd_write(unsigned char c)
{ LCD_DATA=(LCD_DATA&0x0F)|(c&0xF0);
LCD_PULSE();
LCD_DATA=(LCD_DATA&0x0F)|((c<<4)&0xF0);
LCD_PULSE();
delay_us(40);
63
63
}
/* clear LCD and goto home */
void lcd_clear(void)
{ LCD_RS=0;
lcd_write(0x1);
delay_ms(2);
}
/* write a string of chars to the LCD */
void lcd_string(const chars)
{ LCD_RS=1;
while(s)
lcd_write(s++);
}
/* go to the specified position */
void lcd_goto(unsigned char pos)
{ LCD_RS=0;
lcd_write(0x80+pos);
}
64
64
int read_a2d(unsigned char channel)
{ADCON0=0b00000001;
//Turn on A/D module
ADCON1=0b10000000;
//configures analog and voltage reference
pins
ADCON0=(ADCON0&0xC7)|(channel<<3);
//select analog input channel
delay_ms(2);
ADGO=1;
while(ADGO==1) continue;
return(256*ADRESH+ADRESL);}
void convert(int no, char base)
{char i;
for(i=0;i<=9;i++) di[i]=0;
i=0; do{
di[i]=no%base;
no=no/base;
while(no!=0);
}
void display(char number)
{switch(number){
}}
void forward ()
//initiate conversion on the selected channel
//wait until conversion done
65
65
{motor_ra
=
0;
motor_rb
=
1;
motor_la
=
0;
motor_lb
=
1;
}
void backward ()
{
motor_ra
=
1;
motor_rb
=
0;
motor_la
=
1;
motor_lb
=
0;
{motor_ra
=
1;
motor_rb
=
1;
motor_la
=
1;
motor_lb
=
1;
}
void stop()
}
66
66
APPENDIX B
Schematic Diagram of Sensor Application on Rat Cage Systems
67
67
68
68
APPENDIX C
Datasheet of Ultrasonic Sensor –EZ1
69
69
70
70
71
71
APPENDIX D
Datasheet of LCD Display (16X2)
72
72
73
73