Upload
lythuan
View
219
Download
7
Embed Size (px)
Citation preview
1
CHAPTER 1
1.INTRODUCTION
Smart factory enables real-time collection of data, real-time analysis of
the data and consequently real-time decision-making. For the effective
production the workers should find their work environment safe and
comfortable . Smart Factory, which is comprised of hundreds and thousands of
sensors and devices that are managed by one central command or operator. This
serves as the link that connects everything together.
In the paper Remote-controllable power outlet system for home power
management. IEEE Trans. Consume. Electron. 2007 it describes the Wireless
Power Controlled Outlet Module (WPCOM) with a scalable mechanism for
home power management. WPCOM consists of six scalable modules, that is,
the Essential Control Module, the Bluetooth Module, the GSM Module, the
Ethernet Module, the SD Card Module and the Power Measuring Module,
which together provide an indoor wireless, and an outdoor remote control and
monitor of electric home appliances over a Bluetooth network in a home
environment. The WPCOM also allows a GSM cellular mobile phone using
SMS and PC or Notebook using the Internet to monitor and control electric
home appliances at remote locations.
In the paper Design and implementation of a real-time remote
measurement and monitoring of weather parameters system, Ai Rashed ; Oumar
;Singh ; IEEE Publication ; November 2013, it describes the development of an
automated real-time remote measurement and monitoring of weather parameters
system .This work brings forward new programmable device for remote real-
time measurement and monitoring of weather parameters. Three levels are
2
included in the system: the microcontroller for information processing using
atmega32 with a control program for real time measurement, processing and
monitoring, GSM unit to provide wireless communication via cell phones for
monitoring of weather parameters, with the aid of sensors (temperature,
humidity, wind speed, pressure sensors) The System operates in two modes: the
first one is for real time transmitting of data through fixed time intervals, these
intervals are set via key pad according to the user requirement and GSM sharing
between the different sensors. A real time clock (RTC) is used for time
adjustment. The second mode is used in case of regular collection of data as
well as rain fall expectation, where the data is to be collected in regular
intervals,. In this mode the user can exclaim the system about the weather
parameters (temperature, humidity, dew point and rainfall) ,then the user will
receive a real time feedback SMS explaining the weather conditions.
In the paper Wireless access monitoring and control system based on
digital door lock. IEEE Trans. Consume. Electron. 2007, it describes a novel
wireless access monitoring and control system based on the digital door lock,
which is explosively used as a digital consumer device. ZigBee module and
digital door lock with human detection module were implemented. ZigBee
module was designed to support wireless sensor network and also used for the
ZigBee tag to identify the access objects. Digital door lock module was
implemented as a digital consumer device to control the access system as well
as locking system. It is very convenient system for the consumer and has
extensible and flexible characteristics. That is, it can be used as a home security
system by the ZigBee network with additional sensor devices. Developed
system will be improved to an application of the connection with the mobile
phone, in which it sends messages to the mobile phone of the user regarding the
current status and receives control commands from the mobile phone. This
3
system can be one of the practical applications of the home network for the real
life.
The factory can be a smart factory only when there is a comfortable and
safety environment for workers and there should be an efficient production ,this
can be achieved by monitoring the factory environment by using various
sensors and the information from the sensors are collected by the controller.
The data‘s are send to the mobile through the GSM module as stated in the
above papers.
An intelligent monitoring system is suitable for factory area
management and control. Numerous industrial and work place hazards
necessitate a system that monitors the conditions of a factory area. A factory
health monitoring system was created to improve industrial processes by
reducing the handling of dangerous things time required. By using their smart
phones, managers can instantly monitor a factory area. This is a very innovative
approach to safeguard a workplace.
This project implements a real-time method to carry out the monitoring
of factory zone temperatures, humidity and air quality using smart phones. At
the same time, the system detects flames, and analyzes and monitors electrical
load. The monitoring also includes detecting the vibrations of operating
machinery in the factory area. These information's are sent to the user by Short
Message Service (SMS) which alerts the customer simultaneously using a GSM
module. Smart phone APP is developed to control the system. The APP
approach can be applied to the proposed system by using a smart phone.
4
CHAPTER 2
2. HARDWARE DESCRIPTION
2.1. Block diagram
Fig: 2.1 Block Diagram
The brain of this system is a PIC microcontroller which collects
information from various sensors(temperature sensor, PIR sensor, gas sensor,
vibration sensor and flame sensor) and the data are being sent to the mobile via
GSM module.LCD is used to display the information and the loads are turned
on and off using relay circuit.
5
Block diagram contains:
PIC 16F877A
Gas sensor
Vibration sensor
PIR sensor
Flame sensor
Temperature sensor
LCD display.
GSM module.
2.2. Circuit diagram:
Fig: 2.2.1 Overall Circuit Diagram
6
Circuit diagram includes:
40 pin PIC Controller IC
16 pin (16 X 2) LCD
20MHz crystal
GSM module
Gas sensor
Vibration sensor
PIR sensor
Flame sensor
Temperature sensor
2.3. PIC CONTROLLER
2.3.1.PIN diagram:
Fig: 2.3.1 Pin Diagram
7
Table 1. Pin classification
PORT-A RA-0 to RA-5 6 bit wide
PORT-B RB-0 to RB-7 8 bit wide
PORT-C RC-0 to RC-7 8 bit wide
PORT-D RD-0 to RD-7 8 bit wide
PORT-E RE-0 to RE-2 3 bit wide
Master clear for this PIC controller is enabled by pin number 1. This
resets the controller at that instant. Pin number 2 to 10(except 6) are used for
analog to digital interfacing, port C and D are used for parallel and serial
communication. There are two power supply (11, 31) and ground (12,32) pins.
Read/write/chip select are being decided by the output of pin number 8,9,10.
2.3.2. Architecture of PIC:
Fig: 2.3.2 Architecture
8
2.3.3Features of PIC:
Harvard architecture and High performance RISC CPU.
Operating speed: 20 MHz, 200 ns instruction cycle.
It has 8K words X 14 bits of flash program memory.
Data RAM 368 bytes and EEPROM 256 bytes.
Operating voltage is 2V to 5.5V.
8 level deep hardware stack.
35 single word instruction.
Power saving sleep mode.
Selectable oscillator option.
5 I/O ports with 33 I/O pins.
It has 3 timers
Timer 0: 8 bit with 8 bit prescaler.
Timer 1: 8 bit with 8 bit prescaler.
Timer 2: 8 bit with 8 bit prescaler and postscaler.
Watchdog Timer with on chip RC oscillator.
All single cycle instruction except for program branches which
are 2 cycles.
USART with 9 bit address detection.
Synchronous serial port with two modes:
SPI Master
12C Master and slave.
Brown-out detection circuitry for brown out reset.
10 bit, 8 channel A/D converter.
Parallel slave port (PSP) 8 bit wide with external RD,WR and
CS controls.
9
Two capture, compare, PWM modules.
It is 40 pin IC. In that 33 pins is used for general purpose I/O
and remaining 7 is used for clock, Vpp, ground and master
clear.
It is CMOS technology.
The 33 pins are again classified into 5 ports
2.3.4.Advantage and Application of PIC:
Fig:2.3.3 Tree diagram
10
It has inbuilt ADC, Timer, PSP, CCP, MSSP, USART.
Reliable and malfunctioning percent is very less.
High performance due to RISC architecture.
Less power consumption.
Wide availability and large user base.
Re-programmable flash memory capability
Extensive collection of application notes.
Availability of low cost/free development tools.
In-circuit programming capability.
2.4. Gas sensor:
2.4.1.Working:
Whenever the LPG leakage is detected, the MQ_5 sensor gets activated
which in turn indicates it is ON position by a LED glow. This digital output is
given to the input port of controller at the pin no 33.MQ_5 Gas sensor has an
inbuilt Op-amp IC which amplifies the signal being detected and thereby adds
to its sensitivity.
Fig:2.4.1 Gas sensor module.
11
2.4.2.Advantage of gas sensor:
Provides digital output, interfacing with microcontroller is easy.
Low power consumption.
High sensitivity to LPG.
Economical
Stable and long life
Fast response
2.5.Temperature sensor:
Fig: 2.5.1 Temperature sensor
2.5.1.Working principle
The LM35 series are precision integrated-circuit temperature sensors,
with an output voltage linearly proportional to the Centigrade temperature. Thus
the LM35 has an advantage over linear temperature sensors calibrated in °
Kelvin, as the user is not required to subtract a large constant voltage from the
output to obtain convenient Centigrade scaling. The LM35 does not require any
external calibration or trimming to provide typical accuracies of ±¼°C at room
temperature and ±¾°C over a full −55°C to +150°C temperature range. Low
cost is assured by trimming and calibration at the wafer level. The low output
impedance, linear output, and precise inherent calibration of the LM35 make
interfacing to readout or control circuitry especially easy. The device is used
12
with single power supplies, or with plus and minus supplies. As the LM35
draws only 60 μA from the supply, it has very low self-heating of less than
0.1°C in still air.
2.5.2. Features
•Calibrated Directly in ° Celsius (Centigrade)
•Linear + 10 mV/°C Scale Factor
•0.5°C Ensured Accuracy (at +25°C)
•Rated for Full −55°C to +150°C Range
•Suitable for Remote Applications
•Low Cost Due to Wafer-Level Trimming
•Operates from 4 to 30 V
•Less than 60-μA Current Drain
•Low Self-Heating, 0.08°C in Still Air
•Nonlinearity Only ±¼°C Typical
•Low Impedance Output, 0.1 Ω for 1 mA Load.
2.6.Fire Sensor(IR sensor)
Fig 2.6.1 .Fire Sensor
13
2.6.1.Working Principle
Infrared (IR) array flame detectors, also known as visual flame
detectors, employ flame recognition technology to confirm fire by analyzing
near IR radiation using a charge-coupled device (CCD).
2.6.2Features
Allows your robot to detect flames from 2m away.
Fire indicator led.
Calibration preset for range adjustment.
Operating voltage is 5V
Active High Output
2.6.3.Applications
Industrial heating and drying systems
Domestic heating systems
Industrial gas turbines
2.7.VIBRATION SENSOR:
Fig,2.7.1.Vibration Sensor
14
2.7.1.Working principle:
This circuit is using for detecting the vibration using piezo-electric plate.
Piezoelectricity is the ability of crystals and certain ceramic materials to
generate a voltage in response to applied mechanical stress. Piezo electric plate
converts the mechanical vibration to electrical signal. The converted electrical
signal is in the range of milli voltage signal. Then the electrical signal voltage is
given to amplifier unit. The amplifier circuit is constructed with hex inverter IC
4069. The amplified output is in the form of AC signal the diode is used to
rectify the negative signal. Then the electrical signal voltage is given to
comparator unit through 0.1uf capacitor in order to filter the noise signal.
2.7.2.Features:
Power requirements: DC 12v
Device type: fang-wide solid-state control device
Operating Temperature: -30℃ ~ 65℃
Dimensions:45mm*38mm*20mm
2.7.3.Advantages:
Miswiring protection
One second current settling time
Low sensitivity to thermal gradients and base strain
Electronic resonance damping
Measures acceleration
15
2.8.PIR Sensor:
Fig.2.8.1.PIR sensor
2.8.1.Working Principle:
The PIR sensor itself has two slots in it, each slot is made of a special
material that is sensitive to IR. The lens used here is not really doing much and
so we see that the two slots can 'see' out past some distance (basically the
sensitivity of the sensor). When the sensor is idle, both slots detect the same
amount of IR, the ambient amount radiated from the room or walls or outdoors.
When a warm body like a human or animal passes by, it first intercepts one half
of the PIR sensor, which causes a positive differential change between the two
halves. When the warm body leaves the sensing area, the reverse happens,
whereby the sensor generates a negative differential change. These change
pulses are what is detected.
2.8.2.Features:
Output: Digital pulse high (3V) when triggered (motion detected) digital
low when idle (no motion detected). Pulse lengths are determined by
resistors and capacitors on the PCB and differ from sensor to sensor.
16
Sensitivity range: up to 20 feet (6 meters) 110 degrees x 70 degrees
detection range
Power supply: 3.3V - 5V input voltage,
2.8.3Applications:
All outdoor Lights
Lift Lobby
Multi Apartment Complexes
Common staircases
For Basement or Covered Parking Area
Shopping Malls
For garden lights
2.9.Power Supply :
2.9.1.Working Principle
The ac voltage, typically 220V rms, is connected to a transformer,
which steps that ac voltage down to the level of the desired dc output. A diode
rectifier then provides a full-wave rectified voltage that is initially filtered by a
simple capacitor filter to produce a dc voltage. This resulting dc voltage usually
has some ripple or ac voltage variation.
A regulator circuit removes the ripples and also remains the same dc
value even if the input dc voltage varies, or the load connected to the output dc
voltage changes. This voltage regulation is usually obtained using one of the
popular voltage regulator IC units.
17
Fig:2.9.1.Block diagram (Power supply)
2.9.2.Transformer :
The potential transformer will step down the power supply voltage (0-
230V) to (0-6V) level. Then the secondary of the potential transformer will be
connected to the precision rectifier, which is constructed with the help of op–
amp. The advantages of using precision rectifier are it will give peak voltage
output as DC, rest of the circuits will give only RMS output.
2.9.3.Bridge rectifier
When four diodes are connected as shown in figure, the circuit is called as
bridge rectifier. The input to the circuit is applied to the diagonally opposite
corners of the network, and the output is taken from the remaining two corners.
Let us assume that the transformer is working properly and there is a
positive potential, at point A and a negative potential at point B. the positive
potential at point A will forward bias D3 and reverse bias D4.
The negative potential at point B will forward bias D1 and reverse D2. At
this time D3 and D1 are forward biased and will allow current flow to pass
through them; D4 and D2 are reverse biased and will block current flow.
The path for current flow is from point B through D1, up through RL,
through D3, through the secondary of the transformer back to point B. this path
is indicated by the solid arrows. Waveforms (1) and (2) can be observed across
D1 and D3.
TRANSFORMER
RECTIFIER FILTER
IC REGULATOR LOAD
18
One-half cycle later the polarity across the secondary of the transformer
reverse, forward biasing D2 and D4 and reverse biasing D1 and D3. Current
flow will now be from point A through D4, up through RL, through D2, through
the secondary of T1, and back to point A. This path is indicated by the broken
arrows. Waveforms (3) and (4) can be observed across D2 and D4. The current
flow through RL is always in the same direction. In flowing through RL this
current develops a voltage corresponding to that shown waveform (5). Since
current flows through the load (RL) during both half cycles of the applied
voltage, this bridge rectifier is a full-wave rectifier.
One advantage of a bridge rectifier over a conventional full-wave rectifier
is that with a given transformer the bridge rectifier produces a voltage output
that is nearly twice that of the conventional full-wave circuit. This may be
shown by assigning values to some of the components shown in views A and B.
assume that the same transformer is used in both circuits. The peak voltage
developed between points X and y is 1000 volts in both circuits. In the
conventional full-wave circuit shown—in view A, the peak voltage from the
centre tap to either X or Y is 500 volts. Since only one diode can conduct at any
instant, the maximum voltage that can be rectified at any instant is 500 volts.
The maximum voltage that appears across the load resistor is nearly-but
never exceeds-500 v0lts, as result of the small voltage drop across the diode. In
the bridge rectifier shown in view B, the maximum voltage that can be rectified
is the full secondary voltage, which is 1000 volts. Therefore, the peak output
voltage across the load resistor is nearly 1000 volts. With both circuits using the
same transformer, the bridge rectifier circuit produces a higher output voltage
than the conventional full-wave rectifier circuit.
19
2.9.4.IC voltage regulators:
Voltage regulators comprise a class of widely used ICs. Regulator IC
units contain the circuitry for reference source, comparator amplifier, control
device, and overload protection all in a single IC. IC units provide regulation of
either a fixed positive voltage, a fixed negative voltage, or an adjustably set
voltage. The regulators can be selected for operation with load currents from
hundreds of milli amperes to tens of amperes, corresponding to power ratings
from milli watts to tens of watts.
A fixed three-terminal voltage regulator has an unregulated dc input
voltage, Vi, applied to one input terminal, a regulated dc output voltage, Vo,
from a second terminal, with the third terminal connected to ground.
For sensors, microcontroller, LCD --------- 5 volts
For relay circuits ---------- 12 volts
Fig:2.9.2: Circuit diagram (Power supply)
20
2.10.LM7805 (Voltage Regulator)
Fig :2.10.1.:Voltage Regulator
2.10.1.Description:
The KA78XX/KA78XXA series of three-terminal positive
regulator are available in the TO-220/D-PAK package and with several fixed
output voltages, making them useful in a wide range of applications. Each type
employs internal current limiting, thermal shut down and safe operating area
protection, making it essentially indestructible. If adequate heat sinking is
provided, they can deliver over 1A output current. Although designed primarily
as fixed voltage regulators, these devices can be used with external components
to obtain adjustable voltages and currents.
2.10.2Features:
Output Current up to 1A
Output Voltages of 5, 6, 8, 9, 10, 12, 15, 18, 24V
Thermal Overload Protection
Short Circuit Protection
Output Transistor Safe Operating Area Protection
21
2.11.Relay Circuit:
Fig:2.11.1.Relay
2.11.1.Description:
A single pole dabble throw (SPDT) relay is connected to port RB1
of the microcontroller through a driver transistor. The relay requires 12 volts at
a current of around 100ma, which cannot provide by the microcontroller. So the
driver transistor is added. The relay is used to operate the external solenoid
forming part of a locking device or for operating any other electrical devices.
Normally the relay remains off. As soon as pin of the microcontroller goes high,
the relay operates. When the relay operates and releases. Diode D2 is the
standard diode on a mechanical relay to prevent back EMF from damaging Q3
when the relay releases. LED L2 indicates relay on.
2.12. Liquid Crystal Display:
2.12.1. Working:
As the system performs controlling and monitoring operations, it is
primary requirement to put a display in the system which shows various
message such as gas leakage detection, booking number of cylinder in case of
refill of cylinder and also will display actions taken by microcontroller. LCD is
connected to the PORT D in the controller.
We have also provided Liquid Crystal Display (LCD display) to this
system. We have used 16X2 alphanumeric display. LCD display shows actual
22
weight of the gas and at the same time it shows various status messages like
―Sending SMS‖, ―SMS sent‖ and ―Gas has reached to 20% value‖ or ―Gas has
reached to 5% value‖. All these kinds of messages are show n on the LCD so
that person operating this project can read this message. LCD display is useful
in testing purposes as well.
A 16x2 LCD means it can display 16 characters per line and there are 2
such lines. In this LCD each character is displayed in 5x7 pixel matrix. This
LCD has two registers, namely, Command and Data.
Table: 2 LCD pin configuration
Pin
No Function Name
1 Ground (0V) Ground
2 Supply voltage; 5V (4.7V – 5.3V) Vcc
3 Contrast adjustment; through a variable resistor VEE
4 Selects command register when low; and data register
when high
Register
Select
5 Low to write to the register; High to read from the
register Read/write
6 Sends data to data pins when a high to low pulse is
given Enable
7
8-bit data pins
DB0
8 DB1
9 DB2
10 DB3
11 DB4
12 DB5
23
13 DB6
14 DB7
15 Backlight VCC (5V) Led+
16 Backlight Ground (0V) Led-
2.12.2. Interfacing ofLCD with PIC:
Fig: 2.12.1 Interfacing of LCD with PIC
2.13 GSM:
GSM is a second generation cellular system introduced in Europe. Here
GSM is used to send SMS to the user regarding the leakage of the gas and also
it notifies the user when the gas content is below a pre-set value. By this, the
user will be alerted in case of emergency. A SIM card is inserted in the GSM
module. And the mobile number to which the message has to be sent is given
through programming. It sends message to the user as explained.
The message is first sent to the Base Transceiver Station. Several Base
Transceiver Station will be connected to the Base Station Controller. Each Base
24
Station Controller is connected to the Mobile Switching Center. The message
will be sent to the user through Home Location Register if the user is in the
same coverage area as that of Mobile Switching Center, and through Visitor
Location Register if the user is visiting the coverage area of the particular
Mobile Switching Center. Once the message is received, the user can decide
whether or not to book the gas cylinder. In case of the leakage message, the user
will be alerted.
2.13.1 Working:
Whenever there is a leakage detected, the sensor will send a stimulus to
the GSM module. The GSM then sends a message to the numbers which are
already programmed depending on the user's requirements. And also when the
gas content goes below 20% , the system will send an impulse to the GSM
module which in turn sends an alert message to the user. It is done using AT
commands.
2.13.2 Circuit Diagram:
Fig 2.13.1 GSM module
25
2.13.3 Application of GSM module:
In various digital application such as GPS, cell phones and
laptops.
SMS feature which allows the user to send alphanumeric
characters of limited length.
It is used for voice transmission.
It also offers emergency calling and facsimile service.
It is used for high speed data transfer
2.14.WORKING MODULE IMAGE
Fig 2.14.1 Hardware Prototype
26
CHAPTER 3
3. ALGORITHM:
To analyze the problem effectively, the algorithm is essential. This
chapter explains the step by step procedure of the entire process involved in this
project.
3.1Introduction:
PIC microcontrollers are a very useful and versatile tool. They are
inexpensive and easy to find. PICs are very easy to program. Assembler
converts the program into PIC understandable format. Here MPLAB is used to
program PIC controller using embedded C language. Writing algorithm for
program separately simplifies the overall task by dividing it into two simpler
tasks.
While writing the algorithm, solving problems can be focused
instead of concentrating on a particular language. Its reduces the complexity of
programming. Algorithms help in debugging and identifying the logical errors
easily.
3.2Algorithm steps:
1. Initialize header files for PIC.
2. Assign relay circuit for PORT B(RB5,6,7)
3.Assign keypad keys for PORT E and PORT C.
SetRE1
MovRC1
IncRC2
DecRC3
EntRE0
4.Assign digital outputs of fire and PIR sensors to the PORT B(RB3,RB4) of a
27
microcontroller and assign analog outputs of temperature, gas and vibration
sensors to the PORT A.
5.Declare variables for different sensors of data type unsigned.
6. Initialize function calls for transmitter and receiver.
7. Declare PORT A for analog inputs ,PORT B for relay circuit and digital
input, PORT C and PORT E for keypad, PORT D for display.
8. Intialize LCD and OPTION Register.
9. Delay should be given between LCD displays .
10. Initialize the mobile using Mobile_Init() function
11.Set the mobile number using keypad.
12. Display the present sensor values in the LCD display.
13. Send messages to mobile via GSM module if following conditions occur
The conditions are
1.temperature greater than 40
2.Gas greater than 100
3.vibration greater than 50
4.Fire output is high
5.PIR output is high.
14. Here ,AT commands are used to communicate between GSM and PIC and
the messages are converted as text format in GSM and send to mobile.
15. Check for the interrupts.
16. Control the relay circuit if there is any fault in the sensors according to the
received messages from mobile by sending messages.
17. Turn off the relay circuit according to the message sent to microcontroller.
28
3.3Algorithm description:
Here the microcontroller collects information from the various
sensors and sends to the mobile through GSM module. Therefore, the first step
is the mobile initialization where the mobile number is entered using keypad.
The keypad which is connected to the controller consists of five switches. They
are set, move, enter, increment, decrement. After mobile initialization, the
respective mobile number is displayed in the LCD display.
Based on the atmospheric conditions, sensor outputs are being
displayed initially. If any sensor detects and its output is higher than the
allowable limits then it sends its corresponding values to the microcontroller .
the sensor is detected when temperature is greater than 40,gas greater than
100,vibration greater than 50,fire and PIR output is high. The microcontroller
displays the sensor values on the LCD and also sends messages to the given
mobile number. For this purpose, AT commands are used between
microcontroller and GSM.
From the received information of the sensors in the mobile,
factory environment is monitored and can be controlled if necessary by sending
messages from the mobile via GSM to the microcontroller. The microcontroller
turns off the relay circuit according to the messages received. The relay circuit 1
is turned on when *1N is send from the mobile and the same circuit is turned
off when *1F is send. Similar steps are followed for relay circuits 2 and 3.
3.4Control registers:
A control register is a processor register which changes or controls the
general behavior of a CPU or other digital device. Common tasks performed
by control registers include interrupt control, switching the addressing mode,
paging control, and coprocessor control.
29
3.4.1ADCON1 REGISTER:
The size of this register is one byte (8 bits). Each bit has an important role in the
definition of the component. Here's a breakdown of the bits role:
ADFM: A/D Result Format Select bit
1 = Right justified. 6 Most Significant bits of ADRESH are read as ‗0‘.
0 = Left justified. 6 Least Significant bits of ADRESL are read as ‗0‘.
The A/D converter has a resolution of ten bits, i.e., the result of the conversion
can not be stored in one register of eight bits.
. Therefore, the result is stored in two registers: ADRESL and ADRESH. The
size of each register is 8 bits long, so that we have 16 (2*8) bits all together. We
can store the result of the conversion which is 10 bits long using the two
registers ADRESL and ADRESH in the following 2 ways:
alignment to the left
alignment to the right
Alignment to the left – the eight MSB bits are stored in the ADRESH, and the
two LSB bits are stored in ADRESL. In this case, the remaining six bits appear
as - "0".
30
Left Justified
X X X X X X X X X X 0 0 0 0 0 0
ADRESH ADRESL
Alignment to the right – the eight LSB bits are stored in ADRESL, and two
MSB bits are stored in the ADRESH. In this case six highest bits appear as -
"0".
Right Justified
0 0 0 0 0 0 X X X X X X X X X X
ADRESH ADRESL
PCFG3:PCFG0: A/D Port Configuration Control bits:
With these bits PORTA or PORTE can be controlled. analog (A) or digital (D)
mode is decided. If PORTA and PORTE as analog ports, then select the option
PCFG3: PCFG0 = 0000; If the ports are digital, then select the option PCFG3:
PCFG0 = 011x.
In this project ADCON1 register has the following values.
ADFM - - - PCFG3 PCFG2 PCFG0 PCFG0
0 0 0 0 0 0 1 0
.
31
3.4.2INTCON REGISTERS:
GIE EEIE T0IE INTE RBIE T0IF INTF RBIF
GIE: The Global Interrupt Enable bit is like the master switch for all the
different interrupts.
SETTING this bit will enable all the interrupts to function, CLEARING this bit
will disable ALL interrupts.
1 = Enables all un-masked interrupts
0 = Disables all interrupts
EEIE: EE Write Complete Interrupt Enable bite Write Complete Interrupt
Enable bit allows an interrupt to occur when a write operation to the EEPROM
has completed. This interrupt may be required in your programs because it takes
time for a write operation to EEPROM to complete. This interrupt capability
allows the program to do other things instead of halting while the write
operation is accomplished. SETTING the EEIE bit allows an interrupt when the
write to EEPROM operation is complete; CLEARING the bit disables the
interrupt.
1 = Enables the EE write complete interrupt
0 = Disables the EE write complete interrupt
T0IE: The TMR0 Overflow Interrupt Enable bit allows an interrupt when the
TMR0 counter overflows from 255 (0xff) to 0 (0x00). Setting this bit allows the
TMR0 interrupt, CLEARING this bit will disable the interrupt.
1 = Enables the TMR0 interrupt0 = Disables the TMR0 interrupt
32
INTE: The RB0/INT External interrupt Enable bit allows an interrupt from a
clocking signal applied to pin RB0. Whether the interrupt occurs on the rising or
falling edge of this clocking signal is determined by the state of the INTEDG bit
in the OPTION_REG. SETTING the INTE bit allows an interrupt from the
signal on RB0, CLEARING this bit disables the interrupt
.
1 = Enables the RB0/INT interrupt
0 = Disables the RB0/INT interrupt
RBIE: The Port Change Interrupt Enable bit allows an interrupt when there is a
change of state on pins RB7, RB6, RB5 and RB4 on PORTB. SETTING the
RBIE bit will allow the PORTB change interrupts; CLEARING this bit disables
the interrupts.
1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
T0IF: The TMR0 Overflow Interrupt Flag bit is used by the device to indicate
if the interrupt was the result of a TMR0 overflow. As you may have noticed, an
interrupt code will be triggered by any of the different resources available on the
MCU. It is up to you, the programmer, to determine through your software code
which of the resources generates the interrupt. Flag bits allow you to make that
determination. In this case, when a TMR0 overflow interrupt occurs, the TOIF
flag bit is SET. Early in the interrupt service routine (the subroutine program
that you will write to deal with an interrupt) , a check of the various flags is
accomplished - in this case, the TOIF flag, and if it is SET, a TMR0 interrupt
33
occurred and the program will take the desired action. You reset the TMR0
interrupt by CLEARING the TOIF bit. If you fail to reset the TOIF bit,
additional TMR0 interrupts will occur immediately once the interrupt service
routine has completed.
1 = TMR0 has overflowed (must be cleared in software)
0 = TMR0 did not overflow
INTF: The RB0/INT External Interrupt Flag bit is used by the device to
indicate if the interrupt was the result of a clocking signal on the RB0 pin. You
will need to check the state of INTF in the interrupt service routine to determine
if the interrupt occurred because of a clock signal on RB0. At completion of the
interrupt service routine, the INTF pin must be CLEARED to prevent
unintended interrupts.
1 = The RB0/INT interrupt occurred
0 = The RB0/INT interrupt did not occur
RBIF: The Port Change Interrupt Flag bit is used likewise by the device to
indicate if the interrupt was the result
1 = When at least one of the RB7:RB4 pins changed state (must be cleared in
software)
0 = None of the RB7:RB4 pins have changed state.
34
In this project INTCON Register has the following values:
GIE EEIE T0IE INTE RBIE T0IF INTF RBIF
1 1 0 0 0 1 0 1
3.4.3OPTION REGISTER:
The Option_Reg register is a Readable and Writable register that is
used to control some modules of the PIC. This register is only available from
bank 1 and bank 3. The bits of the Option_Reg register as as follows:
Bit # 7 6 5 4 3 2 1 0
Access R/W R/W R/W R/W R/W R/W R/W R/W
Initial 1 1 1 1 1 1 1 1
Name -RBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
Bit <2:0> - PS<2:0>: Pre scalar rate selection
Those bits are Readable and Writable and after a reset they will get
the value '1'. They are used to set the Pre scalar rate. The values it can get are:
35
Bit 3 - PSA: Pre scalar Assignment
This bit is Readable and Writable and after a reset it will get the
value 1. This bit is used to assign the Pre scalar to the Watchdog timer or
the Timer 0 module. The values it can get are:
0: The Pre scalar r is assigned to the Timer 0 module (TMR0)
1: The Pre scalar is assigned to the Watchdog timer (WDT)
Bit 4 - TOSE: Timer 0 Source Edge Select
This bit is Readable and Writable and after a reset it will get the
value 1. It is used to select the RA4/TOCKI pin clock edge (High to Low or
Low to High) on which the Timer 0 will count. The values it can get are:
0: Increment on Low to High
1: Increment on High to Low
Bit 5 - T0CS: Timer 0 Clock Source Select
This bit is Readable and Writable and after a reset it will get the
value 1. This bit will define theTimer 0 module clock source. It can be
either the RA4/TOCKI pin or the Internal Instruction Cycle Clock
(CLKO). The values it can get are:
0: Timer 0 clock source is the Internal instruction cycle clock (CLKO)
1: Timer 0 clock source is the RA4/TOCKI pin
36
Bit 6 - INTEDG: RB0/INT pin Interrupt Edge Select
This bit is Readable and Writable and after a reset it will get the
value 1. By altering this bit, you can select the RB0/INT pin pulse edge
that the RB0/INT interrupt will occur. The values it can get are:
0: The RB0/INT interrupt will occur on the falling edge of the
RB0/INT pin
1: The RB0/INT interrupt will occur on the rising edge of the
RB0/INT pin
Bit 7 - -RBPU: PORTB Pull-up Enable
This bit is Readable and Writable and after a reset it will get the
value 1. The RB ports have an internal programmable pull-up resistor to
minimize the use of external pull-up resistors when needed. This bit will
enable or disable those resistors. The values it can get are:
0: The RB pull-up resistors are enabled
1: The RB pull-up resistors are disabled
In this project the value of OPTION Register has the following values
Bit # 7 6 5 4 3 2 1 0
37
Access R/W R/W R/W R/W R/W R/W R/W R/W
Initial 0 0 0 0 0 1 1 1
Name -RBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
3.4.4INPUT/OUTPUT PORTSOF PIC 16F877:
PIC 16F877 series normally has five input/output ports. They are used for the
input/output interfacing with other devices/circuits. Most of these port pins are
multiplexed for handling alternate function for peripheral features on the
devices. All ports in a PIC chip are bi-directional. When the peripheral action is
enabled in a pin, it may not be used as its general input/output functions. The
PIC 16F877 chip basically has 5 input/output ports. The five input/output ports
and its functions are given below.
PORT A and the TRIS A Registers
PORT A is a 6-bit wide bi-directional port, the direction of this port is
controlled by TRIS A data direction register. Setting a TRIS A (=1) makes
corresponding PORT A pin as an input, clearing the TRIS A (=0) making the
corresponding PORT A pin as an output
Pin RA4 is multiplexed with the ―Timer0‖ module clock input to become the
RA4/T0CKI pin and functioning either input/output operation or Timer 0 clock
functioning module. The RA4/T0CKI pin is a Schmitt Trigger input and an
open-drain output. All other PORT A pins have TTL input levels and full
CMOS output drivers.
Other PORT A pins in this microcontroller multiplexed with analog inputs and
the analog VREF input for both the A/D converters and the comparators. The
operation of each pin is selected by clearing/setting the appropriate control bits
38
in the ADCON1 and/or CMCON registers. The TRIS A register controls the
direction of the PORT pins even when they are being used as analog inputs. The
user must ensure the bits in the TRISA register are maintained set when using
them as analog inputs.
In this project the TRIS A Register has the following values:
1 1 1 1 1 1 1 1
PORT B and the TRIS B Registers
PORT B is also an 8 bit bi-directional PORT. Its direction controlled and
maintained by TRIS B data direction register. Setting the TRIS B into logic ‗1‘
makes the corresponding ―PORT B‖ pin as an input. Clearing the TRIS B bit
make PORT B as an output. Three pins of PORT B are multiplexed with the In-
Circuit Debugger and Low-Voltage Programming function: RB3/PGM,
RB6/PGC and RB7/PGD for performing its alternate functions.
In this project the TRIS B Register has the following values:
0 0 0 1 1 1 1 1
PORT C and the TRIS C Registers
PORT C is an 8-bit wide, bidirectional PORT which controlled and
maintained by TRIS C data direction register. Setting a TRIS C bit (= 1) will
make the corresponding PORT C pin an input (i.e., put the corresponding output
driver in a High-Impedance mode). Clearing a TRIS C bit (= 0) will make the
corresponding PORT C pin an output PORT C is also multiplexed with several
peripheral functions. PORT C pins have Schmitt Trigger input buffers.
When enabling peripheral functions, more care should be taken in
defining TRIS bits for each PORT C pin as compared to other. Some
39
peripherals override the TRIS bit to make a pin an output, while other
peripherals override the TRIS bit to make a pin an input. Since the TRIS bit
override is in effect while the peripheral is enabled, read-modify write
instructions (BSF, BCF, and XORWF) with TRISC as the destination, should be
avoided. The user should refer to the corresponding peripheral section for the
correct TRIS bit settings.
In this project the TRIS C Register has the following values:
1 0 0 0 1 1 1 1
PORT D and TRIS D Registers
PORT D is an 8-bit PORT with bi-directional nature. This port also with
Schmitt Trigger input buffers, each pin in this PORT D individually
configurable as either input or output. PORT D can be configured as an 8-bit
wide microprocessor PORT (functioning as Parallel Slave PORT) by setting
control bit, PSPMODE ((TRISE<4>). In this mode, the input buffers are TTL.
In this project the TRIS D Register has the following values:
0 0 0 0 0 0 0 0
PORT E and TRIS E Registers
PORT E has only three pins (RE0/RD/AN5, RE1/WR/AN6 and
RE2/CS/AN7) which are individually configurable as inputs or outputs. These
pins controllable by using its corresponding data direction register ―TRIS E‖.
These pins also have Schmitt Trigger input buffers. The PORT E pins become
the I/O control inputs for the microprocessor PORT when bit PSPMODE is set.
In this mode, the user must make certain that the TRIS E bits are set and that the
40
pins are configured as digital inputs. Also, ensure that ADCON1 is configured
for digital I/O. In this mode, the input buffers are TTL.
TRISE register which also controls the Parallel Slave PORT operation.
PORT E pins are multiplexed with analog inputs. When selected for analog
input, these pins will read as ‗0‘s. TRIS E controls the direction of the RE pins,
even when they are being used as analog inputs. The user must make sure to
keep the pins configured as inputs when using them as analog inputs.
In this project the TRIS E Register has the following values:
0 0 0 0 0 1 1 1
3.4.5 SCON (SERIAL CONTROL) REGISTER:
Its used to program the start bit, the stop bit and the data bits of data framing
among other things.
SM0 SM1 SM2 REN TB8 RB8 T1 R1
SM0
SM1
These 2 bits determine the framing of data by specifying number of
bits per character and start and stop bits they take following combo.
SM0 SM1 MODE
0 0 Serial mode 0
0 1 Serial mode 1, 8 bit data, 1 stop bit, 1 start bit.
1 0 Serial mode 2
1 1 Serial mode 3
SM2 This enables multiprocessing capabilities of 8051. Usually set to 0
41
REN
Also referred to as SCON.4 as SCON is a bit addressable register.
This is receive enable. When high or 1 it allows 8051 to receive data
from RxD pin. Used or access as SET SCON.4 and CLR SCON.4.
very useful in blocking external serial reception.
TB8 Transfer bit 8. Used for serial mode 2 and 3 not generally used so set
it always to 0
RB8 Receive bit 8. Again used for serial mode 2 and 3 not used so set it to
0
T1 Transmit interrupt. Important flag bit in SCON register. When 8051
finishes transfer of 8 bit character, it raises the T1 flag to indicate that
it is ready to transfer another byte. Is used at beginning of stop bit.
R1 Receive interrupt. Another important flag bit in SCON register. When
8051 finishes receiving data i.e when data is successfully stored in
SBUF it raises R1 flag to indicate byte is received and to be picked
before it gets lost.
In this project the SCON Register has the following values:
SM0 SM1 SM2 REN TB8 RB8 T1 R1
0 1 0 1 0 0 0 0
42
3.5Conclusion:
With the help of algorithm , problems can be analyzed
effectively, Logical errors can be corrected very easily,it act as a blueprint
during programming . Using the Algorithm the coding can be easily written
using any language like C,C++,BASIC or Assembly language.
CHAPTER 4
4.CONCLUSION
The factory area has a lot of very dangerous places that must be
monitored using various sensors. This intelligent monitoring system is suitable
for the management and control of a factory area. Managers using smart phones
to instantly monitor a factory area are a very innovative approach. With further
development the proposed monitoring system will be able to be widely applied
to any dangerous industrial place. For example, chemical product manufacturing
plants, cement product manufacturing plants or factories that employ highly
polluting substances. Remote monitoring using smart phones to do the work is
thus an excellent security application.
REFERENCE:
1.A real time GSM or GPS tracking system based on GSM mobile phone ,Ai
Rashed ; Oumar ;Singh ; IEEE Publication ; November 2013.
2.Design and Implementation of Remote Monitoring System , Zou Changsheng
; Zhang Zixuan ; He Xin ; IEEE Publication July 2015.
43
3.Lien, C.-H.; Bai, Y.-W.; Lin, M.-B. Remote-controllable power outlet system
for home power management. IEEE Trans. Consume. Electron. 2007, 53, 1634–
1641.
4.Salehi, V.; Mohamed, A.A.; Mazloomzadeh, A.; Mohammed, O.A.
Laboratory-based smart power system, part II: Control, monitoring, and
protection. IEEE Trans. Smart Grid 2012, 3, 1405–1417.
5. Schroeder, K.; Moyne, W.; Tilbury, D.M. A Factory Health Monitor: System
Identification, Process Monitoring, and Control. In Proceedings of IEEE
International Conference on Automation Science and Engineering, Arlington,
VA, USA, 23–26 August 2008; pp. 16–22.
Sensors 2013, 13 17412
6. Branch, M.; Bradley, B. Real-Time Web-Based System Monitoring. In
Proceedings of Conference Record of Annual Pulp and Paper Industry
Technical Conference, Appleton, WI, USA, 18–23 June 2006; pp. 1–4.
7. Huang, Y.P.; Young, M.S.; Tai, C.C. Noninvasive respiratory monitoring
system based on the piezoceramic transducer‘s piezoelectric effect. Rev. Sci.
Instrum. 2008, 79, doi:10.1063/1.2889398.
8. Hwang, I.-K.; Baek, J.-W. Wireless access monitoring and control system
based on digital door lock. IEEE Trans. Consume. Electron. 2007, 53, 1724–
1730.
44
9. Chen, B.-R.; Patel, S.; Buckley, T. A web-based system for home monitoring
of patients with Parkinson‘s disease using wearable sensors. IEEE Trans.
Biomed. Eng. 2011, 58, 831–836.
10. Islam, K.; Shen, W.; Wang, X. Wireless sensor network reliability and
security in factory automation: A survey. IEEE Trans. Syst. Man Cybern. Part C
Appl. Rev. 2012, 42, 1243–1256.
45
APPENDIX
1.CODING
.C FILE:
#include<pic.h>
#include"pic_lcd8.h"
#include"pic_adc.h"
#include"pic_serial.h"
#define relay3 RB7
#define relay2 RB6
#define relay1 RB5
#define set RE1
#define mov RC1
#define inc RC2
#define dec RC3
#define ent RE0
#define Fire RB3
#define Pir RB4
//#define vib RC0
unsigned char temp,Gas,Vib,ct1,ct2,c,val[30],a,m_sec,sec,i,b[14];
46
bit aa,bb,dd;
unsigned int s;
void Mobile_Init();
void receiver();
void enter();
void send( );
void keypad();
void main()
{
ADCON1=0x02;
TRISA=0Xff;
TRISC=0X8f;
TRISD=0X00;
TRISB=0X1F;
TRISE=0x07;
PORTB = 0X1F;
relay1=relay2=1;relay3=1;
Lcd8_Init();
Delay(5000);
Lcd8_Init();
Delay(5000);
OPTION_REG = 0;//nRBPU =0;
GIE=1;
PEIE=1;
TMR0=0x3c;
47
T0IE=0;
OPTION_REG=0x07;
TMR0IE=0;
TMR0IE=0;
Lcd8_Display(0x80,"Mob Moni & Emb ",16);
Lcd8_Display(0xC0,"Sys 4 Fac Enviro",16);
Delay(65000);Delay(65000);
Lcd8_Command(0x01);
Delay(65000);
//Serial_Init(9600);Receive(1);
Serial_Init(9600);
Mobile_Init();//Mobile_Init();
Receive(0);
relay1=1;relay2=1;relay3=1;
Delay(65000);Delay(65000);//Delay(65000);
Lcd8_Command(0x01);//keypad();
Lcd8_Display(0x80,"T: G: V: ",16);
Lcd8_Display(0xc0,"Fir: PIR: ",16);
while(1)
{
if(!set){keypad();}
//if(!mov){send();}
temp = Adc8_Cha(0);
Lcd8_Decimal3(0x82,temp);
if(temp>40){send();}
Gas = Adc8_Cha(1);
48
if(Gas>100){send();}
Lcd8_Decimal3(0x88,Gas);
Vib = Adc8_Cha(2);
Lcd8_Decimal3(0x8d,Vib);
if(Fire){Lcd8_Display(0xc4,"High",4);aa=1;}
else{Lcd8_Display(0xc4,"Low ",4);aa=0;}
if(Pir){Lcd8_Display(0xcc,"High",4);bb=1;}
else{Lcd8_Display(0xcc,"Low ",4);bb=0;}
Delay(6500);
}
}
void interrupt ab()
{
if(T0IF==1)
{
T0IF=0;
m_sec++;
if(m_sec>20)
{
sec++;
49
m_sec=0;
}
TMR0=0x3c;
}
if(RCIF==1)
{
RCIF=0;
val[a]=RCREG;
if(val[0] == '*' ){a++; }
else a=0;
}
}
void Mobile_Init()
{
Lcd8_Command(0x01);
Lcd8_Display(0x80,"MOBILE INITLIZAT",16);
Receive(0);
Serial_Conout("AT",2);
Serial_Out(0x0d);
Delay(65000);Delay(65000); //Delay(65000);
Serial_Conout("AT+CMGF=1",9);
Serial_Out(0x0d);
Delay(65000);Delay(65000); //Delay(65000);
Serial_Conout("AT+CNMI=2,2,0,0,0",17);
Serial_Out(0x0d);
50
Delay(65000);Delay(65000);//Delay(65000);
Receive(0);
}
void enter()
{
Serial_Out(0x0d);
Serial_Out(0x0a);
}
void send()
{
Lcd8_Command(0x01);Delay(65000);
Lcd8_Display(0x80,"SENDING",7);
Receive(0);
enter();
Serial_Conout("AT",2);
enter();
Delay(65000);Delay(10000);
Serial_Conout("AT+CMGF=1",9);
enter();
Delay(65000);Delay(15000);
Serial_Conout("AT+CMGS=",8);
Serial_Out('"');
Lcd8_Display(0x80,"MOBILE NO ",16);
//if(aa==1)
//{Serial_Conout("9942481378",10);Lcd8_Display(0xc0,"9942481378
",16);}
51
//else if(aa==0)
{Serial_Conout("9688077740",10);Lcd8_Display(0xc0,"9688077740 ",16);}
for(i=0;i<10;i++){b[i]=EEPROM_READ(i);Delay(6500);Lcd8_Write(0x
c4+i,b[i]);}
for(i=0;i<10;i++){Serial_Out(b[i]);Lcd8_Write(0xc0+i,b[i]);}
Serial_Out('"');
enter();Delay(65000);
//Serial_Conout("Temp:",5);
Serial_Out(temp%1000/100+0x30);
Serial_Out(temp%100/10+0x30);
Serial_Out(temp%10/1+0x30);
Serial_Out('?');
// enter();
// Serial_Conout("Gas:",4);
Serial_Out(Gas%1000/100+0x30);
Serial_Out(Gas%100/10+0x30);
Serial_Out(Gas%10/1+0x30);
Serial_Out('?');
// enter();
// Serial_Conout("Vibration:",10);
Serial_Out(Vib%1000/100+0x30);
Serial_Out(Vib%100/10+0x30);
Serial_Out(Vib%10/1+0x30);
Serial_Out('?');
// enter();
if(aa == 1){Serial_Conout("Fire Detected",13);Serial_Out('?');}
52
//enter();
if(bb == 1){Serial_Conout("PIR Detected",12);Serial_Out('?');}
//enter();
Serial_Out(0x1a);Delay(65000);Delay(65000);Delay(65000);Delay(6500
0);
Lcd8_Command(0x01);
Delay(65000);Delay(65000);Receive(0);a=0;
Lcd8_Display(0xc0," COMPLETE
",16);Delay(65000);Lcd8_Command(0x01);
Lcd8_Display(0x80,"T: G: V: ",16);
Lcd8_Display(0xc0,"Fir: PIR: ",16);
}
void keypad()
{
unsigned char k=0;c=0;
Lcd8_Display(0x80,"ENTER MOBILE NO:",16);
Lcd8_Display(0xC0," ",16);
Lcd8_Command(0x0e);
Delay(65000);
while(ent)
{
Lcd8_Command(0xc0+k);
53
if(!inc){while(!inc)Delay(650);c++;if(c>9)c=0;b[k]=c+0x30;Lcd8_Write
(0xc0+k,b[k]);}
if(!dec){while(!dec)Delay(650);c--
;if(c>9)c=9;b[k]=c+0x30;Lcd8_Write(0xc0+k,b[k]);}
if(!mov){while(!mov)Delay(650);k++;if(k>9){k=0;}}
}
Lcd8_Command(0x0c);
Lcd8_Display(0x80,"MOBILE NO STORED",16);
Lcd8_Display(0xc0," ",16);
for(i=0;i<10;i++){EEPROM_WRITE(i,b[i]);Delay(5000);}
for(i=0;i<10;i++){b[i]=EEPROM_READ(i);Delay(6500);Lcd8_Write(0x
c4+i,b[i]);}
Delay(65000);Lcd8_Command(0x01);
Delay(65000);
Lcd8_Display(0x80,"T: G: V: ",16);
Lcd8_Display(0xc0,"Fir: PIR: ",16);
}
54
.H FILE:
Adc8_Cha(unsigned char);
Adc10_Cha(unsigned char);
unsigned int adc_hbit,adc_lbit;
unsigned int adc_temp,adc_temp0,adc_val1;
unsigned char adc_val,adc_del,adc_j;
Adc8_Cha(unsigned char val)
{
ADFM=0;
adc_del=25;
if(val==0)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x00;
ADON=1;
while(adc_del--);
ADCON0 =0x05;
while(ADCON0!=0X01);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
55
else if(val==1)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x08;
ADON=1;
while(adc_del--);
ADCON0 =0x0d;
while(ADCON0!=0X09);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==2)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x10;
ADON=1;
while(adc_del--);
ADCON0 =0x15;
while(ADCON0!=0x11);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==3)
56
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x18;
ADON=1;
while(adc_del--);
ADCON0 =0x1d;
while(ADCON0!=0x19);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==4)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x20;
ADON=1;
while(adc_del--);
ADCON0 =0x25;
while(ADCON0!=0x21);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==5)
{
57
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x28;
ADON=1;
while(adc_del--);
ADCON0 =0x2d;
while(ADCON0!=0x29);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==6)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x30;
ADON=1;
while(adc_del--);
ADCON0 =0x35;
while(ADCON0!=0x31);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==7)
{
adc_temp0=0;
58
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x38;
ADON=1;
while(adc_del--);
ADCON0 =0x3d;
while(ADCON0!=0x39);
adc_temp=ADRESH;
adc_temp0=adc_temp0+adc_temp;
}
}
adc_val=adc_temp0/10;
return adc_val;
}
Adc10_Cha(unsigned char val)
{
ADFM=1;
adc_del=25;
if(val==0)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x00;
ADON=1;
59
while(adc_del--);
ADCON0 =0x05;
while(ADCON0!=0X01);
adc_hbit=ADRESH;Delay(100);
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==1)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x08;
ADON=1;
while(adc_del--);
ADCON0 =0x0d;
while(ADCON0!=0X09);
adc_hbit=ADRESH;
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==2)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
60
{
ADCON0=0x10;
ADON=1;
while(adc_del--);
ADCON0 =0x15;
while(ADCON0!=0x11);
adc_hbit=ADRESH;
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==3)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x18;
ADON=1;
while(adc_del--);
ADCON0 =0x1d;
while(ADCON0!=0x19);
adc_hbit=ADRESH;
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==4)
61
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x20;
ADON=1;
while(adc_del--);
ADCON0 =0x25;
while(ADCON0!=0x21);
adc_hbit=ADRESH;
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==5)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x28;
ADON=1;
while(adc_del--);
ADCON0 =0x2d;
while(ADCON0!=0x29);
adc_hbit=ADRESH;
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
62
}
}
else if(val==6)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x30;
ADON=1;
while(adc_del--);
ADCON0 =0x35;
while(ADCON0!=0x31);
adc_hbit=ADRESH;
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
else if(val==7)
{
adc_temp0=0;
for(adc_j=0;adc_j<10;adc_j++)
{
ADCON0=0x38;
ADON=1;
while(adc_del--);
ADCON0 =0x3d;
while(ADCON0!=0x39);
adc_hbit=ADRESH;
63
adc_lbit=ADRESL;
adc_temp = adc_lbit + (256*adc_hbit);
adc_temp0=adc_temp0+adc_temp;
}
}
adc_val1=adc_temp0/10;
return adc_val1;
}
void Serial_Init(unsigned long int);
void Serial_Out(unsigned char);
void Serial_Conout(const unsigned char *,unsigned char);
void Baudrate(unsigned long int);
void Receive(unsigned char);
void Serial_Init(unsigned long int baud)
{
Baudrate(baud);
SYNC = 0; // asynchronous mode
SPEN = 1; // serial port enable
TXEN = 1; // tx enable
GIE=1;
PEIE=1;
RCIE = 0; // interrupt set
CREN = 0; // rx enable
}
64
void Serial_Out(unsigned char val)
{
TXREG =val;
while(!TXIF);
TXIF = 0;Delay(5000);
}
void Serial_Conout(const unsigned char *data,unsigned char n)
{
unsigned char ser_j;
for(ser_j=0;ser_j<n;ser_j++)
{
Serial_Out(data[ser_j]);
}
}
void Baudrate(unsigned long int baud)
{
if(baud==110) //Crystal Freq 1 Mhz
{
SPBRG = 141; // for 110 baud rate
BRGH = 0; // baud rate low
}
else if(baud==1200) //Crystal Freq 4 Mhz
{
SPBRG = 51; // for 1200 baud rate
BRGH = 0; // baud rate high
}
65
else if(baud==2400)
{
SPBRG = 25; // for 2400 baud rate
BRGH = 0; // baud rate high
}
else if(baud==4800)
{
SPBRG = 12; // for 4800 baud rate
BRGH = 0; // baud rate high
}
else if(baud==9600)
{
SPBRG = 25; // for 9600 baud rate
BRGH = 1; // baud rate high
}
else if(baud==9620)
{
SPBRG = 129; // for 9600 baud rate
BRGH = 1; // baud rate high
}
else if(baud==57600)
{
SPBRG = 20; // for 57600 baud rate
BRGH = 1; // baud rate high
}
else if(baud==115200)
{
SPBRG = 10; // for 115200 baud rate
BRGH = 1; // baud rate high
66
}
}
void Receive(unsigned char rece)
{
if(rece==1)
{
RCIE = 1; // interrupt set
CREN = 1; // rx enable
}
else
{
RCIE = 0; // interrupt set
CREN = 0; // rx enable
}
}