101
RESTRICTED DESIGN AND FABRICATION OF MICROCONTROLLER BASED FOUR CHANNEL DATA LOGGER By PLT OFF FARHAN HASEEB (16062) ADVISOR WG CDR MUHAMMAD AJMAL CO ADVISOR ASST PROF SHAFQAT-UL-MULK REPORT SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR THE DEGREE OF BE COLLEGE OF AERONAUTICAL ENGINEERING PAF ACADEMY RISALPUR i RESTRICTED

data logger

Embed Size (px)

DESCRIPTION

hh

Citation preview

Page 1: data  logger

RESTRICTED

DESIGN AND FABRICATION OF MICROCONTROLLER BASED FOUR CHANNEL DATA LOGGER

By

PLT OFF FARHAN HASEEB (16062)

ADVISOR

WG CDR MUHAMMAD AJMAL

CO ADVISOR

ASST PROF SHAFQAT-UL-MULK

REPORT SUBMITTED IN PARTIAL FULFILLMENT OFTHE REQUIREMENTS FOR THE DEGREE OF BE

COLLEGE OF AERONAUTICAL ENGINEERING

PAF ACADEMY RISALPUR

August, 2012

i

RESTRICTED

Page 2: data  logger

RESTRICTED

ACKNOWLEDGEMENT

First of all I would like to thanks ALLAH, the Almighty for the successful

completion of my final semester project and report. Also I am extremely grateful to my

advisor Wing Commander Muhammad Ajmal Khan and co-advisor Assistant Professor

Shafqat-ul-Mulk for their continuous help and guidance. In addition to that, I am also

grateful to Flying Officer Ali Raza Ghumman from No. 606 R&D Wing for the support

and help they provided to me. Without their affectionate guidance at every step this

would not have been possible.

ii

RESTRICTED

Page 3: data  logger

RESTRICTED

DEDICATION

I would like to dedicate my report to my parents as they have always been a

source of guidance and motivation to me. Without their prayers this would not have

been possible.

iii

RESTRICTED

Page 4: data  logger

RESTRICTED

ABSTRACT

This document provides final semester project report of a microcontroller based

four channel data logger. It provides detailed definition and explanation of the

designed data logger, which was fabricated successfully. This document presents a

brief summary of different softwares and components that were used during the

project and finally the hardware part. It will also comprise of a detailed summary of

the specifications and applications of the data logger.

iv

RESTRICTED

Page 5: data  logger

RESTRICTED

TITLE…………………................................................................................................. i

ACKNOWLEDGEMENT........................................................................................ ii

DEDICATION...………………………………………………………………………………………………….iii

ABSTRACT………................................................................................................. iv

CHAPTER 01: INTRODUCTION.........................................................................10

Project Title....................................................................................................................................10

Proposal Statement………………..…………………………………………….……………………………………………………….10

Background And Overview..............................................................................................................10

Selection Of PIC 16F877A................................................................................................................13

Features Of PIC 16F877A.................................................................................................................13

CHAPTER 02: PHASES AND SOFTWARES.........................................................15

Phases............................................................................................................................................15

Phase 1 and 2..................................................................................................................................15

Ccs C Compiler................................................................................................................................16

Ic Writer..........................................................................................................................................16

Proteus...........................................................................................................................................17

CHAPTER 03: COMPONENTS.............................................................................19

External Eeprom.............................................................................................................................19

Real Time Clock...............................................................................................................................20

I2C Connection................................................................................................................................20

LCD.................................................................................................................................................21

4X3 Keypad.....................................................................................................................................21

MAX 232.........................................................................................................................................22

IC-7805...........................................................................................................................................22

5

RESTRICTED

Page 6: data  logger

RESTRICTED

CHAPTER 04: ADVANCE FEATURES AND MEMORY ALLOCATION......................24

Advance Features............................................................................................................................24

Variable Time Interval

Present And Finish Date And Time5

Variable No Of Channels

Graphical Trend Analysis

No Of Sample25

Resolution26

Battery26

Frequency

Memory Allocation28

First 14 Bytes Dedicated28

CHAPTER 05: SERIAL INTERFACING AND GRAPHICAL TREND ANALYSIS

Serial Communication

USART

Max 232 Level Conversion

RS 232 To Usb Convertor

Baud Rate

Serial Terminal

Hercules

Advantages

Method32

Graphical Trend Analysis33

CHAPTER 06: HARDWARE IMPLEMENTATION

6

RESTRICTED

Page 7: data  logger

RESTRICTED

PCB35

PCB Design Software35

Final product36

Sensors38

LM 35 Temperature Sensor38

Light Dependent Resistor39

MQ 2 Smoke Sensor39

Variable Resistor40

Sinewave From Function Generator40

CHAPTER 07: CONCLUSION

Project Deliverables For 8th Semester41

Project Monstration Of 8th Semester41

Additional Tasks42

REFERENCES……..APPENDICES……..5

Appendix A46

Appendix B63

Appendix C70

Appendix D73

7

RESTRICTED

Page 8: data  logger

RESTRICTED

LIST OF FIGURES

Figure 1.1: Data Logger....................................................................................................................11

Figure 1.2: Modern Data Logger……………………………………………………………………………………………………..11

Figure 1.3: Data Logger Block Diagram............................................................................................12

Figure 1.4: Elements Of A Digital Controller.....................................................................................13

Figure 1.5: Pin Configuration Diagram Of Pic 16f877a......................................................................14

Figure 2.1: Patch Board Picture Of Circuit........................................................................................15

Figure 2.1: Serial Circuit Diagram.....................................................................................................15

Figure 2.2: CCS C Compiler...............................................................................................................16

Figure 2.3: IC Writer........................................................................................................................17

Figure 2.4: Proteus..........................................................................................................................17

Figure 2.5: Complete Simulation Diagram........................................................................................18

Figure 3.1: Atmel 24C256................................................................................................................19

Figure 3.2: Pin Configuration Diagram Of AT24C256........................................................................19

Figure 3.3: Pin Configuration Diagram Of DS-1307...........................................................................20

Figure 3.4: Basic Form Of The I2c Connection..................................................................................20

Figure 3.5: I2C Signals......................................................................................................................21

Figure 3.6: 16x2 LCD........................................................................................................................21

Figure 3.7: IC-7805..........................................................................................................................22

Figure 3.8: Picture Of Max 232........................................................................................................22

Figure 3.9: Max 232 Along With Capacitors Attached.......................................................................23

Figure 4.1: LCD Showing The Entered Time Interval In Sec...............................................................28

8

RESTRICTED

Page 9: data  logger

RESTRICTED

Figure 4.2: LCD Showing The Entered Finish Date............................................................................25

Figure 4.3: Picture Of Sample No Along With Date And Time...........................................................25

Figure 4.4: Rechargeable Battery.....................................................................................................26

Figure 4.5: Memory Allocation........................................................................................................27

Figure 4.6: 247 (8 Bit No) Saved At First Memory Location...............................................................28

Figure 4.7: 283 (16 Bit No) Saved At First Two Memory Locations....................................................28

Figure 5.1: Female Part Of Standard Serial Db9 Connector...............................................................29

Figure 5.2: RS232 To Usb Converter.................................................................................................31

Figure 5.3: Hercules Terminal..........................................................................................................31

Figure 5.4: Com 9 Is Usb To Serial Communication Port...................................................................32

Figure 5.5: Matlab Command Window............................................................................................33

Figure 5.6: Input simulated wave....................................................................................................33

Figure 5.7: Reconstructed wave in MATLAB.....................................................................................34

Figure 6.1: PCB File Designed In Proteus..........................................................................................35

Figure 6.3: Final Pcb Design In Orcade Software..............................................................................36

Figure 6.4: Finaly Encased Data Logger............................................................................................37

Figure 6.4: Side and Back View of Data Logger.................................................................................37

Figure 6.4: Lm-35 Temperature Sensor............................................................................................38

Figure 6.5: Different Types Of Light Dependent Resistors................................................................39

Figure 6.6: MQ-2 Sensor..................................................................................................................39

Figure 6.7: Variable Resistor............................................................................................................40

9

RESTRICTED

Page 10: data  logger

RESTRICTED

CHAPTER 01

INTRODUCTION

Project Title

1. Design and fabrication of microcontroller based four channel data logger.

Proposal Statement

2. This project is aimed at designing and fabrication of microcontroller based data

logger which is able to simultaneously store values from four different sensors that are

attached to its four input analog channels. The data is to be stored and can

subsequently be transferred to a PC using serial port.

Background and Overview

3. Data logger, also called data recorder is an electronic device that is able to

record data over time with help of external sensors. It is portable, battery powered, and

standalone device with a built-in memory and an interface for data transfer with a

personal computer.

4. Data logging has become the most common, useful and easy way of storing data

values taken from different sensors, without the requirement of user on the apparatus.

Data logger can record values from the sensor automatically. User can attach any

sensor whose output voltage is within the allowed voltage range for data logger input.

Data logger samples the inputs at user defined intervals and saves the sample digital

values in external memory. Figures 1.1 and 1.2 show two of the commercial data

loggers available in market.

10

RESTRICTED

Page 11: data  logger

RESTRICTED

Figure 1.1: Data logger [1]

Figure 1.2: Modern data logger [2]

5. There are many different possible approaches to the design of data logger. For

example by using data acquisition card and recording data values from inputs of DAQ

card, which is further connected to PC in which data can be was acquired by some

software like LAB VIEW. However, the main drawback of this technique is the

mandatory requirement of a PC as the DAQ card can’t be used independently. Hence it

is not portable and economical. The main idea of this project was to design a

microcontroller based data logger that is independent of PC and is portable. Data

values should be recorded in some memory from where they can be transferred to PC

11

RESTRICTED

Page 12: data  logger

RESTRICTED

whenever required for subsequent analysis. Once in PC, appropriate software can be

used to see graphical trend of the data values.

6. The concept block diagram of data logger is shown in figure 1.3 below:-

Figure 1.3: Data logger block diagram

7. The microcontroller unit (MCU) is big revolution in electronics. It is one of the

most significant developments in electronics which has highly contributed in

miniaturization of electronic hardware. Microcontroller has three main elements i.e. I/O

pins, processor and a memory. I/O stands for input or output devices, which

communicate with the outside world. The function of processor is to make calculations

and handle data operations to provide results. The purpose of memory is to store

program and data. The microcontroller contains all these elements shown in figure 1.4

below, in one chip and is not like PC which has separate chips integrated to perform

these functions.

12

RESTRICTED

Page 13: data  logger

RESTRICTED

Figure 1.4: Elements of a Digital Controller [3]

8. Selection of PIC 16F877A. The main manufacturers of microcontrollers

are Intel, Atmel and Microchip. The Intel’s microcontroller taught and commonly used in

CAE is Intel 8051, and that of Microchip is PIC 16F877A. There are many differences in

both like inbuilt ADC channels in PIC 16F877A, total number of ports in both are

different and many other. After studying in detail about microcontrollers, the selected

microcontroller used in this project was PIC 16F877A due to its many additional

features over other compared microcontrollers. Some of the reasons for selection were:-

a) PIC 16F877A has inbuilt capability of analog to digital conversion (ADC) at

its eight pins ie from AD0 to AD7. For a four channel data logger four input ADC

pins were required.

b) Two options are available for selecting the resolution of ADC ie 8 or 10 bit.

Due to less requirement of storage capacity, 8 bit ADC would be used.

9. Features of PIC 16F877A. Features of PIC 16F877A are:-

a) The A suffix in PIC 16F877A indicates that the chip can have maximum

clock speed of 20 MHz which is the main upgrade from the original 16F877

device.

13

RESTRICTED

Page 14: data  logger

RESTRICTED

c) The chip has 8K (8096 x14 bits) of flash ROM program memory. The

microcontroller stores its program in ROM (read only memory). It has 256 bytes

internal EEPROM.

d) The operating voltage range is 2-6 Volts. The maximum current that can

flow through any pin is 25 mA at 5V. It has total five ports ie A, B, C, D and E and

has total of 33 I/O pins

10. Pin configuration diagram of PIC 16F877A is shown in figure 1.5 below:-

Figure 1.5: Pin configuration diagram of PIC 16F877A [4]

14

RESTRICTED

Page 15: data  logger

RESTRICTED

CHAPTER 02

PHASES AND SOFTWARES

PHASES

1. The project has two main phases which are required to be completed:-

a) Phase one is interfacing external components with microcontroller.

b) In second phase PC is to be interfaced with microcontroller.

2. Phase 1. In phase 1 was completed by interfacing the components that are

explained later in the report. Figure 2.1 shows the patch board picture of the hardware

components patched. All these components and methods would be explained later in

the report.

Figure 2.1 Patch board picture of circuit

3. Phase 2. In phase two PC was interfaced with the circuit and the figure 2.2

below shows the circuit.

Figure 2.2: Serial circuit

15

RESTRICTED

Page 16: data  logger

RESTRICTED

Software

4. CCS C Compiler. The microcontroller would be programmed to perform certain

task and this can be done in either machine language or C language. Machine language

is not user friendly relatively to C language. Then C code would be compiled in some

suitable software to convert it into machine code. Many compilers like CCS compiler,

Mickro C compiler High Tec C are used. CCS C Compiler was selected and the source

code written is provided in appendix A at the end. CCS is shown in figure 2.2 below:-

Figure 2.2: CCS C compiler

5. IC Writer. It is the software that is supplied by the vendor with IC burner

which is used to burn the HEX file into the microcontroller. Microchip is to be selected

for PIC microcontrollers and it is shown in figure 2.3 below:-

Figure 2.3: IC writer

16

RESTRICTED

Page 17: data  logger

RESTRICTED

6. MATLAB. MATLAB would be used for analyzing the data in the form of

plots which would be explained later in the report. Figure 2.4 shows the matlab window

picture below:-

Figure 2.4: MATLAB window

7. Proteus User can simulate the program involving different components and

ICs in Proteus. The circuit is to be patched as it is in physical hardware and the HEX

file, the output file of CCS compiler, is given to the microcontroller along with the

oscillating frequency. The Proteus window is shown in figure 2.5 below:-

Figure 2.5: Proteus

8. The complete simulation diagram of Proteus is shown in figure 2.6 below.

17

RESTRICTED

Page 18: data  logger

RESTRICTED

Figure 2.6: Complete simulation diagram

18

RESTRICTED

Page 19: data  logger

RESTRICTED

CHAPTER 03

COMPONENTS

1. The data logger would sample input values after the defined time interval and

along with time stamp they would be saved in external memory till either the memory is

full or the predefined time. Initially the internal EEPROM of the microcontroller was used

for the saving of data but it was only 256 bytes and large no of values can’t be saved. It

could only save 240 samples which is too less for an effective data logger. For saving

large number of observations an external EEPROM was used.

Components

2. External EEPROM. The selected external EEPROM chip Atmel 24C256 is a

serial memory chip and has storage capacity of 256K bits or 32K bytes with three

external address pins that are A0, A1, and A2. These three chips can be connected to

increase the storage capacity to 96K bytes. The figure 3.1 shows the picture of Atmel

24C256. The pin configuration diagram of at24c256 is shown in figure 3.2 below:-

Figure 3.1: Atmel 24C256 [5]

Figure 3.2: Pin configuration diagram of AT24c256 [6]

19

RESTRICTED

Page 20: data  logger

RESTRICTED

3. Real Time Clock. External real time clock DS 1307 was used instead of

internal clock of microcontroller, to prevent it from intense load. It is an 8 pin IC including

SDA and SCL pin. The driver code for this RTC written in C language is provided at the

end of report in appendix C. A crystal of 32.768 KHz is connected between pin 1 and 2.

It can also be powered up from a backup battery. Figure 3.3 shows its pin configuration

diagram below:-

Figure 3.3: configuration diagram of DS-1307 [7]

I2C Connection

4. At24c256 and DS-1307 are connected through I2C connection with PIC. The I2C

stands for interintegrated circuit bus which is used for short-range communication

between different chips and microcontroller. Chip used in this type of connection is

either master or slave. Slave chip is attached to a two-wire bus and is pulled up to logic

1 when idle. The basic form of the I2C connection is shown in figure 3.4 below:-

Figure 3.4: Basic form of the I2C connection [8]

5. The master chip first sends a control code to set up the transfer, then the 8-bit or

10-bit address code, and finally the data. Each byte has a start and acknowledge bit. To

20

RESTRICTED

Page 21: data  logger

RESTRICTED

improve reliability each byte must be acknowledged before sending the next byte. The

sequence to read a single byte requires a total of 5 bytes to complete the process, 3 to

set the address, and 2 to return the data. I2C signals are shown in figure 3.5 below:-

Figure 3.5: I2C signals [9]

6. LCD. 20x4 LCD has total 16 pins including enable, RS, RW, VCC, ground

and seven data pins. Enable pin of LCD would be connected to PIN_D0 of PIC,

similarly RS pin with PIN_D1 and RW pin with PIN_D2. Rest pins of D port except

PIN_D3 would be connected to data pins of the LCD. Initially 16x2 LCD was used for

display which was upgraded to 20x4. Driver code for this LCD written in C language is

provided in appendix B at the end of report. Its pin configuration is same as 20x4 LCD.

Figure 3.6 below shows 20x4 LCD.

Figure 3.6: 16x2 LCD [10]

7. Keypad. 4x3 Keypad was used so that the user can interact with the

device. It’s * button would be used as OK button for selection of any feature.

21

RESTRICTED

Page 22: data  logger

RESTRICTED

8. IC 7805. Above 5V the voltage should be down converted to 5V to prevent

PIC from any damage. 6.5 volts rechargeable battery was used as DC source in circuit.

IC-7805 was used for converting 6.5 volts to exact 5 volts. It has three pins for input,

ground and output respectively. The output can vary from 4.8-5.2 volts and the

maximum current allowed through it is 1 Amp. IC 7805 is shown in figure 3.7 below:-

Figure 3.7: IC-7805 [11]

8. MAX 232. MAX 232 would be used for serial interface between the data

logger and PC. Transmit pin of MAX 232 will be connected to receive pin and vice

versa. Selected baud rate was 9600. The main function of MAX-232 is to convert the

level of PIC signals from 5 volts to 12 volts for PC. Figure 3.8 below shows picture of

MAX 232. Figure 3.9 below shows pin configuration diagram along with attached

capacitors of MAX 232.

Figure 3.8: Picture of MAX 232 [12]

22

RESTRICTED

Page 23: data  logger

RESTRICTED

Figure 3.9: MAX 232 along with capacitors attached [13]

23

RESTRICTED

Page 24: data  logger

RESTRICTED

CHAPTER 04

ADVANCE FEATURES AND MEMORY ALLOCATION

1. Conventional data loggers have some features which were required to be

amended and incorporate those in the required data logger. In this chapter all those

advance features would be explained and they would be compared with conventional

data loggers.

Advance Features

2. Conventional data loggers take input values after some fix time interval. The date

and time is already fed in its clock and it can only be changed from the code. Cheaply

available data loggers in the market are either single or dual channel. In the designed

data logger, memory is divided into number of parts equal to total number of channels

that have been used.

3. Variable Time interval. Designed data logger has user defined variable

time interval and is entered in the start of logging process which can be different for

different logging processes. The complete range for the time interval is from a minimum

of one second to a maximum of 255 minutes. Seconds are used for fast varying

processes and minutes are used for slow varying processes. Figure 4.1 below shows

the picture of LCD showing the entered time interval in seconds.

Figure 4.1: LCD showing the entered time interval in sec

24

RESTRICTED

Page 25: data  logger

RESTRICTED

4. Present and finish date and time. In conventional data loggers the date

and time can only be changed from the source code but the designed data logger has

the capability of user defined present and finish date and time. Logging process will

automatically stop at predefined time. Figure 4.2 below shows the picture LCD showing

the entered finish date. Similarly present date and time would be entered.

Figure 4.2: LCD showing the entered finish date

5. Variable no of Input Channels. Designed data logger has variable no of

channels. They could be different for different logging processes. Minimum no of

channels to be selected is one and a maximum of four can be selected.

6. Graphical trend analysis. The logged data would be analyzed in MATLAB

in form of graphs that would be plotted automatically after reception of data by Matlab

over serial interface. This would be explained later in the report.

7. No of Samples. The total no of samples taken would be displayed on LCD.

Figure 4.3 below shows the picture of the sample no, date and time taken during the

execution of the program.

Figure 4.3: Picture of sample no along with date and time

25

RESTRICTED

Page 26: data  logger

RESTRICTED

8. Resolution. Resolution of the ADC channel that would be used is 8 bit.

Sensors would have output voltage from 0-5V. This means that a 9.6 mV change in

voltage would change the ADC level. 8 bit means that ADC level assigned to each

voltage level would be within 0 to 255. The formula applied to specify ADC level to the

voltage is given below:-

ADC level value= (ADC value at channel / 255.0) * 5.0

9. Battery. A rechargeable battery was connected with the circuit to eliminate

its dependence on AC input power supply. It is a 4.5 Amp Hour battery, which means it

can supply 4.5 Amperes of current for one hour. The designed circuit draws current of

70mA, so it can survive over 2.5 days with this current requirement. Figure 4.4 shows

the picture of the rechargeable battery which was used.

Figure 4.4: Rechargeable battery

10. Frequency. Basically the data logger was designed for sensing slow varying

factors like temperature, humidity, light intensity etc. The minimum frequency of the

input sensor should be less than 0.25 Hz. It was tested after providing it with a sine

wave of frequency of 0.2 Hz. It recorded 5 values in every cycle and was able to draw

graph of the wave. This part would be explained later in the report.

26

RESTRICTED

Page 27: data  logger

RESTRICTED

Memory Allocation

11. Each sampled value is 8 bit so it requires one byte memory space for saving.

The basic idea of memory allocation is that if one channel is used the complete memory

would be dedicated for that sensor. If two channels are used the complete memory

would be divided into two parts. Similarly for three and four channels, memory would be

divided into three and four parts.

12. First 14 bytes dedicated. Data entered by the user in the start is saved on

first 14 bytes of the memory. The first byte is a number (123) that is for checking the

correct transfer of data over serial interface and its reception by the serial terminal. The

next five bytes are for the current date and time. The next five are for the finish date and

time. 12th byte saves no of used channels. 14th byte is the interval between two

consecutive readings. If previous value is 1 and 14th byte shows 5 as shown in figure, it

means that the interval is of 5 seconds. Rest all locations are for the data.

Figure 4.5: Memory allocation [14]

27

RESTRICTED

Page 28: data  logger

RESTRICTED

13. AT24C256 cannot be reset at start of every program execution as it is a

permanent memory. First two bytes of internal EEPROM of PIC would be used to store

the 16 bit number. Due to this, the ability to only transfer the values of the recent logging

process would be achieved. We need to reset this variable so that the new data logging

process can be started otherwise there is much chance garbage data. Figure 4.7 shows

16 bit no saved at first two memory locations. A 16 bit no is required to cover all

memory locations. As only 8 bit no can be saved internal EEPROM‘s one memory

location, so we require two memory locations to save one 16 bit no.

Figure 4.6 : 247 (8 bit no) saved at first memory location

Figure 4.7 : 283 (16 bit no) saved at first two memory locations

28

RESTRICTED

Page 29: data  logger

RESTRICTED

CHAPTER 05

SERIAL INTERFACING AND GRAPHICAL TREND ANALYSIS

1. The recorded data is to be transferred to PC after the completion of logging

process for observing the trend. There could have been different approaches for

interfacing data logger with PC like wireless connection through antennas, wired serial

connection etc. The method selected for connection between PC and data logger was

serial connection using wires. There was a requirement that this data logger could be

connected to any PC easily without the need of any extra components which was the

most important reason for selection of this method. It was achieved by using the

standard DB-9 connector which is shown in figure 5.1. Nowadays serial DB-9 port is not

available in new PCs so serial to USB convertor was used for connection. The data

logger would utilize only one USB port.

Figure 5.1 Female part of standard serial DB9 connector [15]

Serial communication

2. Serial communication is a way enables different equipment to communicate with

outside world. It is called serial because the data bits will be sent in a serial way over a

single line and transmission or reception of data one bit at a time would occur. A

personal computer has a serial port known as communication port or COM Port used to

connect a modem or any other device. Serial ports are controlled by a special chip

29

RESTRICTED

Page 30: data  logger

RESTRICTED

called UART (Universal Asynchronous Receiver Transmitter). Different applications use

different pins on the serial port and this basically depend of the functions required.

3. USART. The universal synchronous/asynchronous receive transmit

(USART) device is typically used in asynchronous mode to implement off-board, one-to-

one connections. The term asynchronous means that no separate clock signal is

needed to time the data reception, so only a data send, data receive, and ground wires

are needed. It is quick and simple to implement if a limited data bandwidth is

acceptable. The USART link can send data up to 100 meters by converting the signal to

higher-voltage levels (typically 12 V). The digital signal is inverted and shifted to

become bipolar (symmetrical about 0 V, line negative when inactive) for transmission.

PIC has a dedicated hardware RS232 port, but CCS C allows any pin to be set up as an

RS232 port, providing functions to generate the signals in software. The basic form of

the signal has 8 data bits, a stop and start bit. The bit period is set by the baud rate

whose typical value is 9600 which is about 10 k bits per second.

4. MAX 232 level conversion. In order to connect a microcontroller to a serial

port on a PC computer, we need to adjust the level of the signals so communicating can

take place. The signal level on a PC is -10V for logic zero, and +10V for logic one. Since

the signal level on the microcontroller is +5V for logic one and 0V for logic zero, we

need an intermediary stage that will convert the levels. One chip specially designed for

this task is MAX232. This chip receives signals from -10 to +10V and converts them into

0 and 5V.

5. RS232 to USB converter. Modern PCs have USB ports instead of serial

ports. For utilization of USB port instead of serial port we will use RS 232 to USB

converter. It is a port-powered, bi-directional USB to RS232 adapter / USB to RS232

converter that can be used to convert any standard RS232 port into a USB port and vice

versa. Figure 5.2 below shows a RS232 to USB converter.

30

RESTRICTED

Page 31: data  logger

RESTRICTED

Figure 5.2: RS232 to USB converter [16]

6. Baud Rate. In telecommunication and electronics, baud is synonymous to

symbols per second or pulses per second. It is the unit of symbol rate, also known as

baud rate or modulation rate. The number of distinct symbol changes (signaling events)

made to the transmission medium per second in a digitally modulated signal or a line

code.

Serial Terminal

7. Serial terminals are used for reception of data transferred from microcontroller

and transferring data that can be received by microcontroller. Serial terminal used in

serial interfacing was Hercules.

8. Hercules. Hercules SETUP utility is useful serial port terminal which was

created for HW group internal use only, but today it's includes many functions in one

utility. Figure 5.3 shows a Hercules terminal below:-

Figure 5.3: Hercules terminal

31

RESTRICTED

Page 32: data  logger

RESTRICTED

11. There are many properties that should be known for accurate serial

communication of data. The first and most important is baud rate. The second

characteristic is parity. The parity characteristic can be even, odd, mark, space, or none.

If even parity, then the last data bit transmitted will be a logical 1 if the data transmitted

had an even amount of 0 bits. If odd parity, then the last data bit transmitted will be a

logical 1 if the data transmitted had an odd amount of 0 bits.

13. Advantages. Serial communication has some advantages over the

parallel communication. One of the advantages is transmission distance. Serial link can

send data to a remote device, more far then parallel link. The cable connection of serial

link is simpler then parallel link and uses less number of wires.

Method

14. Initially the RS 232 to USB conversion cable would be connected to any USB

port and the driver for this cable is installed. This driver is freely available on internet. A

COM port would be generated in the device manager. This port is noted and fed in the

serial terminal for serial connection. Figure 5.4 below shows COM port generated and

shown in device manger on connecting the cable in USB port.

Figure 5.4: COM 9 is USB to serial communication port

32

RESTRICTED

Page 33: data  logger

RESTRICTED

Graphical Trend Analysis in MATLAB

15. Analysis of the recorded data would be done in MATLAB. The code written in

MATLAB is provided in appendix D at the end. MATLAB would receive the data that is

sent at serial port of PC at specified baud rate. All the data entered in the start would

be displayed in the command window. Figure 5.5 below shows this data displayed in the

command window.

16. MATLAB automatically plots the graphs equal to the number sensors used. They

would be differentiated by plotting them by different colors and shapes. Figure 5.6 below

shows the input wave simulated in Proteus. This input wave was generated by charging

and discharging of capacitor through a resistor. Figure 5.7 below shows the

reconstructed wave in MATLAB.

Figure 5.5: Matlab command window

Figure 5.6: Input wave simulated in Proteus

33

RESTRICTED

Page 34: data  logger

RESTRICTED

Figure 5.7: Reconstructed wave in MATLAB

34

RESTRICTED

Page 35: data  logger

RESTRICTED

CHAPTER 06

HARDWARE IMPLEMENTATION AND FINAL PRODUCT

Printed Circuit Board

1. The project was aimed at fabricating the data logger in a final product form so it

could be properly packed in some casing. PCB is very necessary for a product to be in

its presentable form. First of all the PCB design was to be made in some software and

then fabricate it on the PCB machine.

PCB Design Software

2. The machine available in CAE is compatible with Advance design system (ADS)

and Proteus. Initially PCB was designed in Proteus but due to some fault, the PC was

not fabricating the Proteus design. Figure 6.1 shows the PCB file designed in Proteus

below:-

Figure 6.1: PCB file designed in Proteus

35

RESTRICTED

Page 36: data  logger

RESTRICTED

3. Orcad. Due to non-compatibility of PCB machine with Proteus, the PCB

was redesigned in Orcad software and finally fabricated from No 606 R&D Wing with

the help and guidance of Flying Officer Ali Raza Ghumman. Figure 6.3 shows the final

PCB design of Orcad software. All the green connections are showing ground signal. All

connections in red correspond to bottom layer and blue color corresponds to top layer.

Figure 6.3: Final PCB design in Orcad software

Final product

3. Data logger was given a final product form by encasing it, which was fabricated

by the help of IE workshop. Plastic sheet was used which was painted with silver color

to improve its aesthetics. Terminals for connection of sensors and the power button

were provided on the front side. LCD and keypad were provided on the inclined surface

for the ease of the user’s view. Buttons for transfer of data serially were provided at one

side along with the serial port. Battery charger input was given at the back side. Figure

6.4 below shows the finally encased data logger. Figure 6.5 provides the side and back

view of the data logger.

36

RESTRICTED

Page 37: data  logger

RESTRICTED

Figure 6.4: Finally encased data logger

Figure 6.5: Side and back view of data logger.

Sensors

4. The aim was not to attach complex sensors and design their difficult complex

driver circuits. Simple sensors were used during the testing of project which are:-

a) LM-35 temperature sensor

b) Light dependent resistor

c) MQ-2 smoke sensor

37

RESTRICTED

Page 38: data  logger

RESTRICTED

d) Variable resistor

e) Sine wave from function generator

5. LM-35 temperature sensor. LM35 is an integrated circuit sensor that can

be used to measure temperature with an electrical output proportional to the

temperature (in oC). This temperature sensor is an easy to use, cost-effective and has

decent accuracy of 0.5 oC. It has three pins i.e. Vcc, Vout, and ground respectively

Output is taken across a resistor of 10 k ohm that is connected between pin no 2 and

ground. The output voltage is between 0 and 1 volt. Figure 6.6 below shows the pin

configuration diagram of LM-35. Figure 6.7 shows the circuit diagram of LM-35.

Following conversion factor is used to convert voltage into temperature.

Temperature in degrees = Output voltage * 100

Figure 6.6: LM-35 temperature sensor [17]

Figure 6.7: Circuit diagram of LM-35 [18]

38

RESTRICTED

Page 39: data  logger

RESTRICTED

6. Light dependent resistor. A light dependent resistor (LDR) is a resistor

whose resistance decreases with increasing incident light intensity. A photo resistor is

made of a high resistance semiconductor. If light falling on it is of high enough

frequency, photons absorbed by the semiconductor give bound electrons enough

energy to jump into the conduction band. The resulting free electron (and its hole

partner) conduct electricity, thereby lowering resistance. Figure 6.8 shows different

types of light dependent resistors below:-

Figure 6.8: Different types of light dependent resistors [19]

7. MQ-2 smoke sensor. MQ-2 is a simple smoke sensor which is used in gas

leakage detecting equipment in family and industry. They are suitable for detection of

LPG, i-butane, propane, methane, alcohol, Hydrogen and smoke. It has many

advantages over other smoke sensors which include wide detecting scope, fast

response and high sensitivity. Figure 6.9 below shows the picture of MQ-2 sensor.

Figure 6.9: MQ-2 sensor [20]

39

RESTRICTED

Page 40: data  logger

RESTRICTED

8. Variable resistor. It is a resistor whose value can be varied from 0 to

maximum. A knob is provided at the top of it to change the value of resistance. Variable

resistor of 50 K Ohm was used in the circuit. It has three pins similar to any other three

pin IC. Pin 1, 2 and 3 are for input voltage, ground and output voltage respectively. 5V

input voltage would be applied at input pin and the resistance was varied from 0-50 K

Ohm to get output voltage from 5-0 volts. The response of the variable resistor is

known, so the circuit can be checked for the exact response. The diagram of variable

resistor is shown in figure 6.10 below:-

Figure 6.10: Variable resistor [21]

9. Charging and Discharging of Capacitor. Capacitor was charged and

discharged through a resistor to check the response of MATLAB code. Graph plotted in

MATLAB was approximately exact as it was expected. The amplitude of this wave was

4.5 volts and had DC offset of approximately 2 volts. This offset was required to shift the

complete sine wave above zero value so that the microcontroller takes the accurate

values from ADC ports. Frequency of this wave is 0.2 Hz, so the time period is 5

seconds.

40

RESTRICTED

Page 41: data  logger

RESTRICTED

CHAPTER NO 7

CONCLUSION

1. This chapter summarizes various tasks completed in this project. These were as

following:-

a) Understanding overall project and carrying out literature survey about

available data loggers

b) Studying different microcontrollers and understanding PIC16F877A

c) Understanding PIC C programming, IC writer, Proteus and Orcade

d) Compiling C program for keypad, DS1307 and external memory and

interfacing them with microcontroller

e) Transfer of logged data through serial port to PC

f) Graphical trend analysis of data in MATLAB

g) Design and fabrication of PCB

Project Deliverables for 8th Semester

2. The project deliverables for 8th semester are as following:-

a) Design and fabrication of data logger circuit with user interface.

b) Design and fabrication of data logger interface with PC.

c) Demonstration of maximum capacity of data logging capability with

suitable sensor.

d) Project documentation in form of project report.

41

RESTRICTED

Page 42: data  logger

RESTRICTED

Final Demonstration for 8th semester

3. Data logger can receive analog inputs from multiple sensors and then sample /

store these values as per user defined time interval. After the completion of logging

process, the logged data can be transferred to PC over a serial interface for subsequent

data analysis.

Additional Tasks

4. Additional tasks for the project were:-

a) Provide USB capability with the designed data logger.

b) Attach rechargeable battery along with the ac input source.

5. Rechargeable battery was attached with circuit successfully. It is 6.5 volts, 3.5

amp hour battery. So IC 7805 was used to provide exactly 5 volt to microcontroller. USB

capability cannot be added with PIC 16F877A. MMC/SD card is addressed in blocks of

512 bytes. PIC 16F877A has very less RAM i.e. 368 bytes. USB requires even more

RAM

6. The project was successfully completed and all the milestones were achieved

within the specified time. Data Logger can form independent measurement and storage

system by hooking it up with suitable sensor. Measurement parameters could be:-

a) Humidity

b) Wind speed

c) Temperature

d) Voltage

42

RESTRICTED

Page 43: data  logger

RESTRICTED

REFERENCES

Ref [1], [2] www.dataloggerinc.com

Ref [3], [4], [8], [9] Programming-8BIT-PIC-MicroController

Ref [5], [6] www.microchip.com

Ref [7], [12] www.maxim.com

Ref [10], [19] www.extreme-electronics.com

Ref [10], [11] www.extreme-electronics.com

Ref [14] www.sodoityourself.com

Ref [15] Proteus software picture

Ref [16] www.romux.com

Ref [17] www.usbconvertors.com

Ref [18] www.engineersgarage.com

Ref [20] www.electronics-table.com

Ref [21] www.hwsensor.com

43

RESTRICTED

Page 44: data  logger

RESTRICTED

Appendices

Appendix A: C source code for data logger

Appendix B: Driver code for 4 line LCD

Appendix C: Driver code for DS-1307 real time clock

Appendix D: Matlab code for graphical trend analysis

44

RESTRICTED

Page 45: data  logger

RESTRICTED

Appendix A

C source code for data logger

#include <16F877A.h> //PIC 16F877A selected

#device adc=8 //8 or 10 bit can be selected. 8 bit resolution of ADC is selected

#FUSES NOWDT //No Watch Dog Timer

#FUSES HS //High speed Osc (> 4mhz)

#FUSES NOPUT //No Power Up Timer

#use delay(clock=20000000) //clock speed

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) //Baud rate, transmit

//and recieve pin selected

#include <lcd44.c> //driver for 20x4 LCD

#include <ds1307.c> //driver for ds-1307 RTC

#define EEPROM_SDA PIN_C1 //SDA(serial data) pin selected

#define EEPROM_SCL PIN_C2 //SCL(serial clock) pin selected

#include <24256.c> //Driver for AT24c256 EEprom

int sec,mins,hrs,day,month,year,dow,x,numofdigs,channel,Fhrs,Fmin,hunsdig=0,tensdig=0,onesdig=0,

thoudig=0, Fday,Fmonth,m_Adc_Value,Fyear,yy,number;

void value(); //If the function is defined after main program

//then its prototype should be defined in start.

//prototype function named value defined.It would be explained later in the program.

int16 pata,xx,zz=0,interval; //16 bit values defined

float m_inp;

// getkey is a function for keypad. Whichever key would be pressed its value would be returned. * key

//would act as OK key.

int getkey()

45

RESTRICTED

Page 46: data  logger

RESTRICTED

{

int i;

while(1)

{

//Checking Row 1

output_low(PIN_B4); //output logic 0 at pin B4

output_high(PIN_B5); //output logic 1 at pin B5

output_high(PIN_B6);

output_high(PIN_B7);

i=0;

while(i<10)

{

if(!input(PIN_B0))

{

while(!input(PIN_B0));

return x=1; //Return value of x as 1

break; //Leave the loop

}

else if(!input(PIN_B1))

{

while(!input(PIN_B1));

return x=2;

break;

}

else if(!input(PIN_B2))

{

while(!input(PIN_B2));

return x=3;

break;

}

i=i+1;

delay_ms(5);

46

RESTRICTED

Page 47: data  logger

RESTRICTED

}

//Checking Row 2

output_high(PIN_B4);

output_low(PIN_B5);

output_high(PIN_B6);

output_high(PIN_B7);

i=0;

while(i<10)

{

if(!input(PIN_B0))

{

while(!input(PIN_B0));

return x=4;

break;

}

else if(!input(PIN_B1))

{

while(!input(PIN_B1));

return x=5;

break;

}

else if(!input(PIN_B2))

{

while(!input(PIN_B2));

return x=6;

break;

}

i=i+1;

delay_ms(5);

}

//Checking Row 3

output_high(PIN_B4);

output_high(PIN_B5);

47

RESTRICTED

Page 48: data  logger

RESTRICTED

output_low(PIN_B6);

output_high(PIN_B7);

i=0;

while(i<10)

{

if(!input(PIN_B0))

{

while(!input(PIN_B0));

return x=7;

break;

}

else if(!input(PIN_B1))

{

while(!input(PIN_B1));

return x=8;

break;

}

else if(!input(PIN_B2))

{

while(!input(PIN_B2));

return x=9;

break;

}

i=i+1;

delay_ms(5);

}

//Checking Row 4

output_high(PIN_B4);

output_high(PIN_B5);

output_high(PIN_B6);

output_low(PIN_B7);

i=0;

while(i<10)

{

48

RESTRICTED

Page 49: data  logger

RESTRICTED

if(!input(PIN_B0))

{

while(!input(PIN_B0));//printf(lcd_putc,"\f"); //star key breaks the function and

// acts as ok key

return x=100;

break;

}

else if(!input(PIN_B1))

{

while(!input(PIN_B1));

return x=0;

break;

}

else if(!input(PIN_B2))

{

while(!input(PIN_B2));

return x=200;

break;

}

i=i+1;

delay_ms(5);

}

}

}

// Pause function is for giving delay or pause between acquirement of two consecutive input values.

The //pause should be equal to the time interval entered in start of data logging process that is either in

//second or minutes. During the pause the time and date would be shown on LCD.

void pause(void)

{

int s,t;

zz++;

ds1307_get_date(day,month,year,dow); //get date from DS-1307

49

RESTRICTED

Page 50: data  logger

RESTRICTED

ds1307_get_time(hrs,mins,sec); //get time from DS-1307

printf(lcd_putc,"\fTime : %02u:%02u:%02u",hrs,mins,sec); //Display time on LCD

lcd_gotoxy(1,2);

printf(lcd_putc,"Date : %02u:%02u:%02u",day,month,year); //Display date on LCD

lcd_gotoxy(1,3);

printf(lcd_putc,"Waiting for input.");

lcd_gotoxy(1,4);

printf(lcd_putc,"Sample # %lu",zz); //No of acquired samples would be shown

if(number==1){ //Pause loop for 1 second

for(t=0;t<(interval);t++)

{

delay_ms(900);

}

}

if(number==2){ //Pause loop for 1 minute

for(t=0;t<(interval);t++)

{

for(s=0;s<60;s++)

{

delay_ms(950);

}

}

}

}

//checkchannel is a function for selecting the no of channels by data logger and switching the ADC

pins //equal to the channels entered.

void check_channel(void)

{

float m_inp;

if(channel==1)

{

set_adc_channel(0); //set to read ADC channel 0

50

RESTRICTED

Page 51: data  logger

RESTRICTED

delay_ms(10);

m_Adc_Value=read_adc(); //get 8 bit readig for channel 0

delay_ms(10);

m_inp=(m_Adc_Value/255.0)* 5; //Conversion factor

printf(lcd_putc,"\fvoltage1=%3.3f",m_inp); //Print voltage value at LCD

write_ext_eeprom(pata,m_Adc_Value); //Write to external EEPROM at location variable that is pata

delay_ms(10);

pata++; //increment in pata for next value to be saved at next location

delay_ms(1500);

}

else if(channel==2)

{

set_adc_channel(0);

delay_ms(20);

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

printf(lcd_putc,"\fvoltage1=%3.3f",m_inp);

set_adc_channel(1); //ADC Channel 1

delay_ms(20); //Donot remove delay

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

lcd_gotoxy(1,2); //Goto 2nd line of LCD

printf(lcd_putc,"voltage2=%3.3f",m_inp);

delay_ms(1500);

}

else if(channel==3)

{

51

RESTRICTED

Page 52: data  logger

RESTRICTED

set_adc_channel(0); //ADC Channel 0

delay_ms(10); //Donot remove delay

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

printf(lcd_putc,"\fvoltage1=%3.3f",m_inp);

set_adc_channel(1); //ADC Channel 1

delay_ms(10);

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

lcd_gotoxy(1,2);

printf(lcd_putc,"voltage2=%3.3f",m_inp);

set_adc_channel(2); //ADC Channel 1

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

lcd_gotoxy(1,3); //Goto 3rd line of LCD

printf(lcd_putc,"voltage3=%3.3f",m_inp);

delay_ms(2000);

}

else if(channel==4)

{

set_adc_channel(0); //ADC Channel 0

delay_ms(20);

m_Adc_Value=read_adc();

delay_ms(10);

52

RESTRICTED

Page 53: data  logger

RESTRICTED

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

printf(lcd_putc,"\fvoltage1=%3.3f",m_inp);

set_adc_channel(1); //ADC Channel 1

delay_ms(20); //Donot remove delay

m_Adc_Value=read_adc();

delay_ms(10); //ADC needs some time for reading input so Donot remove this delay

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

lcd_gotoxy(1,2);

printf(lcd_putc,"voltage2=%3.3f",m_inp);

set_adc_channel(2); //ADC Channel 1

delay_ms(10);

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

lcd_gotoxy(1,3);

printf(lcd_putc,"voltage3=%3.3f",m_inp);

set_adc_channel(3); //ADC Channel 1

delay_ms(10);

m_Adc_Value=read_adc();

delay_ms(10);

write_ext_eeprom(pata,m_Adc_Value);

delay_ms(10);

pata++;

m_inp=(m_Adc_Value/255.0)*5.0;

delay_ms(10);

lcd_gotoxy(1,4);

printf(lcd_putc,"voltage4=%3.3f",m_inp);

delay_ms(2000);

53

RESTRICTED

Page 54: data  logger

RESTRICTED

}

}

// makenum is a function due to which we don’t have to define an array of number entered by

keypad.this //function automatically makes number even if is 2 or 3 digit

void makenum()

{

numofdigs++;

if(numofdigs==4)

{

thoudig=hunsdig;hunsdig = tensdig; tensdig = onesdig; onesdig = x;

}

if(numofdigs==3)

{

hunsdig = tensdig; tensdig = onesdig; onesdig = x;

}

if(numofdigs==2)

{

tensdig=onesdig; onesdig = x;

}

if(numofdigs==1)

onesdig = x;

}

//Loop for printing no pressed on keypad on the LCD.

void again(void)

{

do

{

x=getkey();

if(x==100)

break;

else

54

RESTRICTED

Page 55: data  logger

RESTRICTED

{

printf(lcd_putc,"%u",x);delay_ms(250);

if((x>=0)&&(x<=9))

makenum();

}

}while((x>=0)&&(x<=9)) //execute this loop until entered no is between 1 to 9

}

void start(void)

{

write_ext_eeprom(0,11); //Write 11 on location 0 of EEPROM

delay_ms(15);

write_ext_eeprom(1,22);

delay_ms(15);

printf(lcd_putc,"\fEnter complete date");

lcd_gotoxy(1,2); //Goto 2nd line of LCD

printf(lcd_putc,"and time.Enter day");

lcd_gotoxy(1,3);

printf(lcd_putc,"Day : ");

again();

delay_ms(15);

day=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000); //Forming the no of

//value entered by keypad

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,day);

pata++;

delay_ms(20);

lcd_gotoxy(1,4);

printf(lcd_putc,"Enter Month : ");

again();

month=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata, month);

pata++;

delay_ms(20);

55

RESTRICTED

Page 56: data  logger

RESTRICTED

printf(lcd_putc,"\fEnter year : ");

again();

year=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

write_ext_eeprom(pata,year);

pata++;

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

delay_ms(20);

lcd_gotoxy(1,2);

printf(lcd_putc,"Enter current time");

lcd_gotoxy(1,3);

printf(lcd_putc,"Enter Hours : ");

again();

hrs=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

write_ext_eeprom(pata,hrs);

pata++;

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

delay_ms(20);

lcd_gotoxy(1,4);

printf(lcd_putc,"Enter Mins : ");

again();

mins=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,mins);

pata++;

delay_ms(20);

printf(lcd_putc,"\fEnter finish date ");

lcd_gotoxy(1,2);

printf(lcd_putc,"and time.Enter time");

lcd_gotoxy(1,3);

printf(lcd_putc,"Enter hours : ");

again();

Fhrs=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

56

RESTRICTED

Page 57: data  logger

RESTRICTED

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,Fhrs);

pata++;

delay_ms(20);

lcd_gotoxy(1,4);

printf(lcd_putc,"Enter min : ");

again();

Fmin=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

delay_ms(10);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,Fmin);

pata++;

delay_ms(20);

printf(lcd_putc,"\fEnter Finish Date");

lcd_gotoxy(1,2);

printf(lcd_putc,"Enter Day : ");

again();

Fday=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,Fday);

pata++;

delay_ms(20);

lcd_gotoxy(1,3);

printf(lcd_putc,"Enter Month : ");

again();

Fmonth=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,Fmonth);

pata++;

delay_ms(20);

lcd_gotoxy(1,4);

printf(lcd_putc,"Enter Year : ");

again();

57

RESTRICTED

Page 58: data  logger

RESTRICTED

Fyear=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,Fyear);

pata++;

delay_ms(20);

printf(lcd_putc,"\fEnter no of channels");

lcd_gotoxy(1,2);

printf(lcd_putc,"to be used.");

lcd_gotoxy(1,3);

printf(lcd_putc,"Channels: ");

again();

channel=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,channel);

pata++;

delay_ms(20);

printf(lcd_putc,"\fEnter gap b/w input");

lcd_gotoxy(1,2);

printf(lcd_putc,"values.Press 1 for");

lcd_gotoxy(1,3);

printf(lcd_putc,"seconds.Press 2 for");

lcd_gotoxy(1,4);

printf(lcd_putc,"minutes : ");

again();

number=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

write_ext_eeprom(pata,number);

delay_ms(20);

pata++;

printf(lcd_putc,"\fEnter Interval : ");

again();

interval=onesdig+(tensdig*10)+(hunsdig*100)+(thoudig*1000);

numofdigs=thoudig=hunsdig=tensdig=onesdig=0;

58

RESTRICTED

Page 59: data  logger

RESTRICTED

write_ext_eeprom(pata,interval);

delay_ms(20);

pata++;

}

//Checktime is a function which compares the start time and date with finish time and date in every

loop. //when they are same or when start time and date

//match with finish time and date.

void checktime(void)

{

if((Fhrs==hrs)&&(Fmin==mins)&&(Fday==day)&&(Fmonth=month)&&(Fyear==year))

{

output_high(PIN_A5);

while(1)

{

lcd_putc("\fFinish time reached");

delay_ms(1000);

}

}

}

void main() //Main function

{

pata=2;

lcd_init(); //Initialize LCD

delay_ms(10);

init_ext_eeprom(); //Initialize external EEPROM

delay_ms(10);

ds1307_init(); //Initialize DS-1307

delay_ms(10);

if(input(PIN_C0)) //push button at PIN_C0

{

printf(lcd_putc,"\fSending saved data");

59

RESTRICTED

Page 60: data  logger

RESTRICTED

xx=read_int16_eeprom(0); //read location 0 of internal

EEPROM of PIC

yy = read_ext_eeprom(0);

delay_ms(10);

printf("%u\n\r",yy); //send value of variable

printf("%lu\n\r",xx);

delay_ms(10);

value();

}

if(input(PIN_B3))

{

write_int16_eeprom(0,0); //write on internal EEPROM of PIC at

location 0

write_int16_eeprom(1,0);

}

start();

ds1307_set_date_time(day,month,year,3,hrs,mins,50); //Set date and time of DS-1307

delay_ms(10);

setup_adc(ADC_CLOCK_INTERNAL); // ADC clock

setup_adc_ports(ALL_ANALOG);

delay_ms(20);

while(1) //Infinite loop

{

check_channel();

xx=pata; //last memory location saved

write_int16_eeprom(0,xx);

delay_ms(10);

checktime();

pause();

}

60

RESTRICTED

Page 61: data  logger

RESTRICTED

}

//value is the function for transfering all data values from external memory to PC serially

void value()

{

pata=1;

while(pata<15) //first 14 dedicated bytes

{

yy = read_ext_eeprom(pata);

delay_ms(10);

printf("%u\n\r",yy);

delay_ms(10);

pata++;

}

pata=15;

delay_ms(10);

while(pata<xx) //Loop for sendng all data values saved to PC serially

{

m_inp=(read_ext_eeprom(pata)/255.0)*5;

delay_ms(10);

printf("%3.3f\n\r",m_inp);

delay_ms(10);

pata++;

}

pata=xx;

}

61

RESTRICTED

Page 62: data  logger

RESTRICTED

Appendix B

Driver code for 20x4 LCD

// Flex_LCD420.c

// These pins are for my Microchip PicDem2-Plus board,

// which I used to test this driver.

// An external 20x4 LCD is connected to these pins.

// Change these pins to match your own board's connections.

#define LCD_DB4 PIN_D4

#define LCD_DB5 PIN_D5

#define LCD_DB6 PIN_D6

#define LCD_DB7 PIN_D7

#define LCD_RS PIN_D1

#define LCD_RW PIN_D2

#define LCD_E PIN_D0

// If you want only a 6-pin interface to your LCD, then connect the R/W pin on the LCD to ground, and

//comment out the following line. Doing so will save one PIC pin, but at the cost of losing the ability to

//read from the LCD. It also makes the write time a little longer because a static delay must be used,

//instead of polling the LCD's busy bit. Normally a 6-pin interface is only used if you are running out of

//PIC pins, and you need to use as few as possible for the LCD.

#define USE_RW_PIN 1

// These are the line addresses for most 4x20 LCDs.

#define LCD_LINE_1_ADDRESS 0x00

#define LCD_LINE_2_ADDRESS 0x40

#define LCD_LINE_3_ADDRESS 0x14

#define LCD_LINE_4_ADDRESS 0x54

//========================================

62

RESTRICTED

Page 63: data  logger

RESTRICTED

#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines(or more)

int8 lcd_line;

int8 const LCD_INIT_STRING[4] =

{

0x20 | (lcd_type << 2), // Set mode: 4-bit, 2+ lines, 5x8 dots

0xc, // Display on

1, // Clear display

6 // Increment cursor

};

//-------------------------------------

void lcd_send_nibble(int8 nibble)

{

// Note: !! converts an integer expression to a boolean (1 or 0).

output_bit(LCD_DB4, !!(nibble & 1));

output_bit(LCD_DB5, !!(nibble & 2));

output_bit(LCD_DB6, !!(nibble & 4));

output_bit(LCD_DB7, !!(nibble & 8));

delay_cycles(1);

output_high(LCD_E);

delay_us(2);

output_low(LCD_E);

}

// This sub-routine is only called by lcd_read_byte(). It's not a stand-alone routine. For example, the

// R/W signal is set high by lcd_read_byte() before this routine is called.

#ifdef USE_RW_PIN

int8 lcd_read_nibble(void)

{

int8 retval;

// Create bit variables so that we can easily set individual bits in the retval variable.

#bit retval_0 = retval.0

#bit retval_1 = retval.1

63

RESTRICTED

Page 64: data  logger

RESTRICTED

#bit retval_2 = retval.2

#bit retval_3 = retval.3

retval = 0;

output_high(LCD_E);

delay_us(1);

retval_0 = input(LCD_DB4);

retval_1 = input(LCD_DB5);

retval_2 = input(LCD_DB6);

retval_3 = input(LCD_DB7);

output_low(LCD_E);

delay_us(1);

return(retval);

}

#endif

// Read a byte from the LCD and return it.

#ifdef USE_RW_PIN

int8 lcd_read_byte(void)

{

int8 low;

int8 high;

output_high(LCD_RW);

delay_cycles(1);

high = lcd_read_nibble();

low = lcd_read_nibble();

return( (high<<4) | low);

}

64

RESTRICTED

Page 65: data  logger

RESTRICTED

#endif

//----------------------------------------

// Send a byte to the LCD.

void lcd_send_byte(int8 address, int8 n)

{

output_low(LCD_RS);

#ifdef USE_RW_PIN

while(bit_test(lcd_read_byte(),7)) ;

#else

delay_us(60);

#endif

if(address)

output_high(LCD_RS);

else

output_low(LCD_RS);

delay_cycles(1);

#ifdef USE_RW_PIN

output_low(LCD_RW);

delay_cycles(1);

#endif

output_low(LCD_E);

lcd_send_nibble(n >> 4);

lcd_send_nibble(n & 0xf);

}

//----------------------------

void lcd_init(void)

{

int8 i;

65

RESTRICTED

Page 66: data  logger

RESTRICTED

lcd_line = 1;

output_low(LCD_RS);

#ifdef USE_RW_PIN

output_low(LCD_RW);

#endif

output_low(LCD_E);

// Some LCDs require 15 ms minimum delay after power-up. Others require 30 ms.

// To set it to 35 ms, so it should work with all of them.

delay_ms(35);

for(i=0 ;i < 3; i++)

{

lcd_send_nibble(0x03);

delay_ms(5);

}

lcd_send_nibble(0x02);

for(i=0; i < sizeof(LCD_INIT_STRING); i++)

{

lcd_send_byte(0, LCD_INIT_STRING[i]);

// If the R/W signal is not used, then the busy bit can't be polled. One of the init commands takes

longer //than the hard-coded delay of 50 us, so in that case, lets just do a 5 ms delay after all four of them.

#ifndef USE_RW_PIN

delay_ms(5);

#endif

}

}

//----------------------------

66

RESTRICTED

Page 67: data  logger

RESTRICTED

void lcd_gotoxy(int8 x, int8 y)

{

int8 address;

switch(y)

{

case 1:

address = LCD_LINE_1_ADDRESS;

break;

case 2:

address = LCD_LINE_2_ADDRESS;

break;

case 3:

address = LCD_LINE_3_ADDRESS;

break;

case 4:

address = LCD_LINE_4_ADDRESS;

break;

default:

address = LCD_LINE_1_ADDRESS;

break;

}

address += x-1;

lcd_send_byte(0, 0x80 | address);

}

//-----------------------------

void lcd_putc(char c)

{

67

RESTRICTED

Page 68: data  logger

RESTRICTED

switch(c)

{

case '\f':

lcd_send_byte(0,1);

lcd_line = 1;

delay_ms(2);

break;

case '\n':

lcd_gotoxy(1, ++lcd_line);

break;

case '\b':

lcd_send_byte(0,0x10);

break;

default:

lcd_send_byte(1,c);

break;

}

}

#ifdef USE_RW_PIN

char lcd_getc(int8 x, int8 y)

{

char value;

lcd_gotoxy(x,y);

// Wait until busy flag is low.

while(bit_test(lcd_read_byte(),7));

output_high(LCD_RS);

value = lcd_read_byte();

output_low(LCD_RS);

return(value);

}

#endif

68

RESTRICTED

Page 69: data  logger

RESTRICTED

Appendix C

Driver code for DS-1307

//ds1307_init() - Enable oscillator without clearing the seconds register used when PIC loses power

and //DS1307 run from 3V BAT

// ds1307_set_date_time(day,mth,year,dow,hour,min,sec) Set the date/time

// ds1307_get_date(day,mth,year,dow) Get the date

// ds1307_get_time(hr,min,sec) Get the time

#define RTC_SDA PIN_C4

#define RTC_SCL PIN_C3

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bcd2bin(BYTE bcd_value);

BYTE bin2bcd(BYTE binary_value)

{

BYTE temp;

BYTE retval;

temp = binary_value;

retval = 0;

while(1)

{

// Get the tens digit by doing multiple subtraction of 10 from the binary value.

if(temp >= 10)

{

temp -= 10;

retval += 0x10;

}

else // Get the ones digit by adding the remainder.

{

retval += temp;

break;

69

RESTRICTED

Page 70: data  logger

RESTRICTED

}

}

return(retval);

}

// Input range - 00 to 99.

BYTE bcd2bin(BYTE bcd_value)

{

BYTE temp;

temp = bcd_value;

// Shifting upper digit right by 1 is same as multiplying by 8.

temp >>= 1;

// Isolate the bits for the upper digit.

temp &= 0x78;

// Now return: (Tens * 8) + (Tens * 2) + Ones

return(temp + (temp >> 2) + (bcd_value & 0x0f));

}

void ds1307_init(void)

{

BYTE seconds = 0;

i2c_start();

i2c_write(0xD0); // WR to RTC

i2c_write(0x00); // REG 0

i2c_start();

i2c_write(0xD1); // RD from RTC

seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307

i2c_stop();

seconds &= 0x7F;

delay_us(3);

i2c_start();

i2c_write(0xD0); // WR to RTC

i2c_write(0x00); // REG 0

i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value

70

RESTRICTED

Page 71: data  logger

RESTRICTED

i2c_start();

i2c_write(0xD0); // WR to RTC

i2c_write(0x07); // Control Register

i2c_write(0x80); // Disable squarewave output pin

i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE

sec)

{

sec &= 0x7F;

hr &= 0x3F;

i2c_start();

i2c_write(0xD0); // I2C write address

i2c_write(0x00); // Start at REG 0 - Seconds

i2c_write(bin2bcd(sec)); // REG 0

i2c_write(bin2bcd(min)); // REG 1

i2c_write(bin2bcd(hr)); // REG 2

i2c_write(bin2bcd(dow)); // REG 3

i2c_write(bin2bcd(day)); // REG 4

i2c_write(bin2bcd(mth)); // REG 5

i2c_write(bin2bcd(year)); // REG 6

i2c_write(0x80); // REG 7 - Disable squarewave output pin

i2c_stop();

}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)

{

i2c_start();

i2c_write(0xD0);

i2c_write(0x03); // Start at REG 3 - Day of week

i2c_start();

i2c_write(0xD1);

dow = bcd2bin(i2c_read() & 0x7f); // REG 3

day = bcd2bin(i2c_read() & 0x3f); // REG 4

71

RESTRICTED

Page 72: data  logger

RESTRICTED

mth = bcd2bin(i2c_read() & 0x1f); // REG 5

year = bcd2bin(i2c_read(0)); // REG 6

i2c_stop();

}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)

{

i2c_start();

i2c_write(0xD0);

i2c_write(0x00); // Start at REG 0 - Seconds

i2c_start();

i2c_write(0xD1);

sec = bcd2bin(i2c_read() & 0x7f);

min = bcd2bin(i2c_read() & 0x7f);

hr = bcd2bin(i2c_read(0) & 0x3f);

i2c_stop();

}

72

RESTRICTED

Page 73: data  logger

RESTRICTED

Appendix D

MATLAB code for Graphical trend analysis

delete(instrfindall); % Delete all previous COM ports

clear all

clc % Clear screen

s=serial('COM9', 'BaudRate', 9600); % Change to required COM

fopen(s); % Open the serial port for r/w

hold on

n=1;

while (n<4)

Vp = fscanf(s); % Read Data back from serial port

Vp=str2double(Vp); % String to double conversion

E(n) = Vp;

n=n+1;

end

x=1;

while (x<=E(2))

Vt = fscanf(s);

Vt=str2double(Vt);

C(x) = Vt;

x=x+1;

end

fclose(s); % Close the serial port

delete(s); % Delete the serial port

day=C(1); % Assign values to variables

month=C(2);

year=C(3);

hours=C(4);

mins=C(5);

Fhours=C(6);

Fmins=C(7);

73

RESTRICTED

Page 74: data  logger

RESTRICTED

Fday=C(8);

Fmonth=C(9);

Fyear=C(10);

channel=C(11);

number=C(12);

interval=C(13);

for i=14:length(C)

j=i-13;

voltage(j)=C(i);

end

disp(['No. of channels used were: ', num2str(C(11))]) %Display no of channels in command window

disp(['Start time was ', num2str(C(4)),':',num2str(C(5)),':50'])

disp(['Start date was ', num2str(C(1)),'/',num2str(C(2)),'/',num2str(C(3))])

disp(['Finish time was ', num2str(C(6)),':',num2str(C(7))])

disp(['Finish date was ', num2str(C(8)),'/',num2str(C(9)),'/',num2str(C(10))])

if C(12)==1

disp(['Interval used between two sampled input values was: ', num2str(C(13)),' seconds'])

elseif C(12)==2

disp(['Interval used between two sampled input values was: ', num2str(C(13)),' minutes'])

end

hold on;

title('Data Logger Sensor#1 values'); %title of figure

ylabel('Voltage value axis'); % Label of Y axix of figure

xlabel('Reading# axis'); % Label of X axix of figure

ylim([25 45]);

if C(11)==1

Channel_1=C(14:length(C));

Channel_1=Channel_1*100;

74

RESTRICTED

Page 75: data  logger

RESTRICTED

disp(['Number of reading from each sensor are ',num2str(length(Channel_1))])

plot(Channel_1,'--rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',5)

% Characteristics of graph

h = legend('Channel#1',1); % Legend for figure

set(h,'Interpreter','none')

elseif C(11)==2

Channel_1=C(14:2:length(C));

Channel_1=Channel_1*100;

Channel_2=C(14:2:length(C));

disp(['Number of reading from each sensor are ',num2str(length(Channel_2))])

plot(Channel_1,'--rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',5)

h = legend('Channel#1',1);

figure(2); % Plot second figure

plot(Channel_2,'--b*','LineWidth',1,'MarkerEdgeColor','m','MarkerFaceColor','g','MarkerSize',5)

title('Data Logger Sensor#2 values');

ylabel('Voltage value axis');

xlabel('Reading# axis');

ylim([0 5]);

h = legend('Channel#2',1);

set(h,'Interpreter','none')

elseif C(11)==3

Channel_1=C(17:3:length(C));

Channel_1=Channel_1*100;

Channel_2=C(18:3:length(C));

Channel_3=C(19:3:length(C));

disp(['Number of reading from each sensor are ',num2str(length(Channel_2))])

figure(1);

plot(Channel_1,'--rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',5)

h = legend('Channel#1',1);

figure(2);

plot(Channel_2,'--b*','LineWidth',1,'MarkerEdgeColor','m','MarkerFaceColor','g','MarkerSize',5)

title('Data Logger Sensor#2 values');

ylabel('Voltage value axis');

xlabel('Reading# axis');

75

RESTRICTED

Page 76: data  logger

RESTRICTED

ylim([0 5]);

h = legend('Channel#2',1);

figure(3);

plot(Channel_3,'--ms','LineWidth',1,'MarkerEdgeColor','r','MarkerFaceColor','y','MarkerSize',5)

title('Data Logger Sensor#3 values');

ylabel('Voltage value axis');

xlabel('Reading# axis');

ylim([0 5]);

h = legend('Channel#3',1);

set(h,'Interpreter','none')

elseif C(11)==4

Channel_1=C(17:4:length(C));

Channel_1=Channel_1*100;

Channel_2=C(18:4:length(C));

Channel_3=C(19:4:length(C));

Channel_4=C(20:4:length(C));

disp(['Number of reading from each sensor are ',num2str(length(Channel_2))])

plot(Channel_1,'--rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','m','MarkerSize',5)

h = legend('Channel#1',1);

figure(2);

plot(Channel_2,'--b*','LineWidth',1,'MarkerEdgeColor','m','MarkerFaceColor','g','MarkerSize',5)

title('Data Logger Sensor#2 values');

ylabel('Voltage value axis');

xlabel('Reading# axis');

ylim([0 5]);

h = legend('Channel#2',1);

figure(3);

plot(Channel_3,'--ms','LineWidth',1,'MarkerEdgeColor','r','MarkerFaceColor','y','MarkerSize',5)

title('Data Logger Sensor#3 values');

ylabel('Voltage value axis');

xlabel('Reading# axis');

76

RESTRICTED

Page 77: data  logger

RESTRICTED

ylim([0 5]);

h = legend('Channel#3',1);

figure(4);

plot(Channel_4,'--go','LineWidth',1,'MarkerEdgeColor','y','MarkerFaceColor','r','MarkerSize',5)

title('Data Logger Sensor#4 values');

ylabel('Voltage value axis');

xlabel('Reading# axis');

ylim([0 5]);

h = legend('Channel#4',1);

set(h,'Interpreter','none')

end

77

RESTRICTED