Vous êtes sur la page 1sur 77

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 August, 2012

i RESTRICTED

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

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

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

RESTRICTED

TITLE ................................................................................................ ii ACKNOWLEDGEMENT ........................................................................................ iii DEDICATION....iii i 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

5 RESTRICTED

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

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


Advance Features ......................................................................................................................... 24 Variable Time Interval ................................................................................................................... 24 Present And Finish Date And Time ................................................................................................. 25 Variable No Of Channels ............................................................................................................... 25 Graphical Trend Analysis ............................................................................................................... 25 No Of Sample................................................................................................................................ 25 Resolution .................................................................................................................................... 26 Battery ......................................................................................................................................... 26 Frequency ..................................................................................................................................... 27 Memory Allocation ....................................................................................................................... 28 First 14 Bytes Dedicated ................................................................................................................ 28

CHAPTER 05: SERIAL INTERFACING AND GRAPHICAL TREND ANALYSIS ............ 29


Serial Communication ................................................................................................................... 29 USART .......................................................................................................................................... 30 Max 232 Level Conversion ............................................................................................................. 30 RS 232 To Usb Convertor ............................................................................................................... 30 Baud Rate ..................................................................................................................................... 31 Serial Terminal .............................................................................................................................. 31 Hercules ....................................................................................................................................... 31 Advantages ................................................................................................................................... 32 Method ........................................................................................................................................ 32 Graphical Trend Analysis ............................................................................................................... 33

6 RESTRICTED

RESTRICTED

CHAPTER 06: HARDWARE IMPLEMENTATION.................................................. 35


PCB............................................................................................................................................... 35 PCB Design Software ..................................................................................................................... 35 Final product ................................................................................................................................ 36 Sensors ......................................................................................................................................... 38 LM 35 Temperature Sensor ........................................................................................................... 38 Light Dependent Resistor .............................................................................................................. 39 MQ 2 Smoke Sensor ...................................................................................................................... 39 Variable Resistor ........................................................................................................................... 40 Sinewave From Function Generator ............................................................................................... 40

CHAPTER 07: CONCLUSION .............................................................................. 41


Project Deliverables For 8th Semester ............................................................................................ 41 Project Monstration Of 8th Semester ............................................................................................. 41 Additional Tasks ........................................................................................................................... 42

REFERENCES.. .............................................................................................. 44 APPENDICES.. .............................................................................................. 45


Appendix A ................................................................................................................................... 46 Appendix B ................................................................................................................................... 63 Appendix C ................................................................................................................................... 70 Appendix D ................................................................................................................................... 73

7 RESTRICTED

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

8 RESTRICTED

RESTRICTED
Figure 4.1: LCD Showing The Entered Time Interval In Sec .............................................................. 28 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

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

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 cant 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

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

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 Intels 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

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

RESTRICTED

CHAPTER 02 PHASES AND SOFTWARES

PHASES 1. The project has two main phases which are required to be completed:a) b) 2. Phase one is interfacing external components with microcontroller. In second phase PC is to be interfaced with microcontroller. In phase 1 was completed by interfacing the components that are

Phase 1.

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

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

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

RESTRICTED

Figure 2.6: Complete simulation diagram

18 RESTRICTED

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 cant 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

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

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 Enable pin of LCD would be connected to PIN_D0 of PIC,

and seven data pins.

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. Its * button would be used as OK button for selection of any feature.

21 RESTRICTED

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

RESTRICTED

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

23 RESTRICTED

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

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

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

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 14 th 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

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 EEPROMs 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

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

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-toone 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

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

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

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

RESTRICTED

Figure 5.7: Reconstructed wave in MATLAB

34 RESTRICTED

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

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 users 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

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) b) c)

LM-35 temperature sensor Light dependent resistor MQ-2 smoke sensor 37 RESTRICTED

RESTRICTED d) e) Variable resistor 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

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

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

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) c) d) Studying different microcontrollers and understanding PIC16F877A Understanding PIC C programming, IC writer, Proteus and Orcade Compiling C program for keypad, DS1307 and external memory and interfacing them with microcontroller e) f) g) Transfer of logged data through serial port to PC Graphical trend analysis of data in MATLAB Design and fabrication of PCB

Project Deliverables for 8th Semester 2. The project deliverables for 8th semester are as following:a) b) c) Design and fabrication of data logger circuit with user interface. Design and fabrication of data logger interface with PC. Demonstration of maximum capacity of data logging capability with suitable sensor. d) Project documentation in form of project report.

41 RESTRICTED

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) b)

Provide USB capability with the designed data logger. 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) b) c) d) Humidity Wind speed Temperature Voltage

42 RESTRICTED

RESTRICTED

REFERENCES

Ref [1], [2] Ref [3], [4], [8], [9] Ref [5], [6] Ref [7], [12] Ref [10], [19] Ref [10], [11] Ref [14] Ref [15] Ref [16] Ref [17] Ref [18] Ref [20] Ref [21]

www.dataloggerinc.com Programming-8BIT-PIC-MicroController www.microchip.com www.maxim.com www.extreme-electronics.com www.extreme-electronics.com www.sodoityourself.com Proteus software picture www.romux.com www.usbconvertors.com www.engineersgarage.com www.electronics-table.com www.hwsensor.com

43 RESTRICTED

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

RESTRICTED

Appendix A C source code for data logger

#include <16F877A.h> #device adc=8

//PIC 16F877A selected //8 or 10 bit can be selected. 8 bit resolution of ADC is selected

#FUSES NOWDT #FUSES HS #FUSES NOPUT

//No Watch Dog Timer //High speed Osc (> 4mhz) //No Power Up Timer

#use delay(clock=20000000) #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

//clock speed //Baud rate, transmit //and recieve pin selected

#include <lcd44.c> #include <ds1307.c> #define EEPROM_SDA PIN_C1 #define EEPROM_SCL PIN_C2 #include <24256.c>

//driver for 20x4 LCD //driver for ds-1307 RTC //SDA(serial data) pin selected //SCL(serial clock) pin selected //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; float m_inp;

//16 bit values defined

// 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

RESTRICTED
{ int i; while(1) {

//Checking Row 1 output_low(PIN_B4); output_high(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; break; //Return value of x as 1 //Leave the loop //output logic 0 at pin B4 //output logic 1 at pin B5

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

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

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

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

RESTRICTED
ds1307_get_time(hrs,mins,sec); printf(lcd_putc,"\fTime : %02u:%02u:%02u",hrs,mins,sec); lcd_gotoxy(1,2); printf(lcd_putc,"Date : %02u:%02u:%02u",day,month,year); 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 //Display date on LCD //get time from DS-1307 //Display time on LCD

if(number==1){ for(t=0;t<(interval);t++) { delay_ms(900); } }

//Pause loop for 1 second

if(number==2){ for(t=0;t<(interval);t++) { for(s=0;s<60;s++) { delay_ms(950); } }

//Pause loop for 1 minute

} }

//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

RESTRICTED
delay_ms(10); m_Adc_Value=read_adc(); delay_ms(10); m_inp=(m_Adc_Value/255.0)* 5; printf(lcd_putc,"\fvoltage1=%3.3f",m_inp); //Conversion factor //Print voltage value at LCD //get 8 bit readig for channel 0

write_ext_eeprom(pata,m_Adc_Value); //Write to external EEPROM at location variable that is pata delay_ms(10); pata++; delay_ms(1500); } //increment in pata for next value to be saved at next location

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); 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; lcd_gotoxy(1,2); printf(lcd_putc,"voltage2=%3.3f",m_inp); delay_ms(1500); } //Goto 2nd line of LCD //ADC Channel 1 //Donot remove delay

else if(channel==3) {

51 RESTRICTED

RESTRICTED
set_adc_channel(0); 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; printf(lcd_putc,"\fvoltage1=%3.3f",m_inp); set_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); 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); delay_ms(2000); } //Goto 3rd line of LCD //ADC Channel 1 //ADC Channel 1 //ADC Channel 0 //Donot remove delay

else if(channel==4) { set_adc_channel(0); delay_ms(20); m_Adc_Value=read_adc(); delay_ms(10); //ADC Channel 0

52 RESTRICTED

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); delay_ms(20); m_Adc_Value=read_adc(); delay_ms(10); //ADC needs some time for reading input so Donot remove this delay //ADC Channel 1 //Donot remove 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); 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); //ADC Channel 1

53 RESTRICTED

RESTRICTED
}

// makenum is a function due to which we dont 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

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); delay_ms(15); write_ext_eeprom(1,22); delay_ms(15); //Write 11 on location 0 of EEPROM

printf(lcd_putc,"\fEnter complete date"); lcd_gotoxy(1,2); 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); //value entered by keypad numofdigs=thoudig=hunsdig=tensdig=onesdig=0; write_ext_eeprom(pata,day); pata++; delay_ms(20); //Forming the no of //Goto 2nd line of LCD

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

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

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

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

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() { pata=2; lcd_init(); delay_ms(10); init_ext_eeprom(); delay_ms(10); ds1307_init(); delay_ms(10);

//Main function

//Initialize LCD

//Initialize external EEPROM

//Initialize DS-1307

if(input(PIN_C0)) { printf(lcd_putc,"\fSending saved data");

//push button at PIN_C0

59 RESTRICTED

RESTRICTED
xx=read_int16_eeprom(0); EEPROM of PIC yy = read_ext_eeprom(0); delay_ms(10); printf("%u\n\r",yy); printf("%lu\n\r",xx); delay_ms(10); value(); } //send value of variable //read location 0 of internal

if(input(PIN_B3)) { write_int16_eeprom(0,0); location 0 write_int16_eeprom(1,0); } //write on internal EEPROM of PIC at

start();

ds1307_set_date_time(day,month,year,3,hrs,mins,50); delay_ms(10);

//Set date and time of DS-1307

setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(ALL_ANALOG); delay_ms(20);

// ADC clock

while(1) { check_channel(); xx=pata; write_int16_eeprom(0,xx); delay_ms(10); checktime(); pause(); }

//Infinite loop

//last memory location saved

60 RESTRICTED

RESTRICTED
}

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

void value() { pata=1; while(pata<15) { yy = read_ext_eeprom(pata); delay_ms(10); printf("%u\n\r",yy); delay_ms(10); pata++; } pata=15; //first 14 dedicated bytes

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

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 #define LCD_RW #define LCD_E

PIN_D1 PIN_D2 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

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), 0xc, 1, 6 }; // Set mode: 4-bit, 2+ lines, 5x8 dots // Display on // Clear display // 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

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

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

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

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

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

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) // ds1307_get_date(day,mth,year,dow) // ds1307_get_time(hr,min,sec) Set the date/time Get the date 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 { retval += temp; break; // Get the ones digit by adding the remainder.

69 RESTRICTED

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); i2c_write(0x00); i2c_start(); i2c_write(0xD1); seconds = bcd2bin(i2c_read(0)); i2c_stop(); seconds &= 0x7F; // RD from RTC // Read current "seconds" in DS1307 // WR to RTC // REG 0

delay_us(3);

i2c_start(); i2c_write(0xD0); i2c_write(0x00); i2c_write(bin2bcd(seconds)); // WR to RTC // REG 0 // Start oscillator with current "seconds value

70 RESTRICTED

RESTRICTED
i2c_start(); i2c_write(0xD0); i2c_write(0x07); i2c_write(0x80); i2c_stop(); // WR to RTC // Control Register // Disable squarewave output pin

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(0x00); i2c_write(bin2bcd(sec)); i2c_write(bin2bcd(min)); i2c_write(bin2bcd(hr)); i2c_write(bin2bcd(dow)); i2c_write(bin2bcd(day)); i2c_write(bin2bcd(mth)); i2c_write(bin2bcd(year)); i2c_write(0x80); i2c_stop(); } // I2C write address // Start at REG 0 - Seconds // REG 0 // REG 1 // REG 2 // REG 3 // REG 4 // REG 5 // REG 6 // REG 7 - Disable squarewave output pin

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow) { i2c_start(); i2c_write(0xD0); i2c_write(0x03); i2c_start(); i2c_write(0xD1); dow = bcd2bin(i2c_read() & 0x7f); // REG 3 day = bcd2bin(i2c_read() & 0x3f); // REG 4 // Start at REG 3 - Day of week

71 RESTRICTED

RESTRICTED
mth = bcd2bin(i2c_read() & 0x1f); // REG 5 year = bcd2bin(i2c_read(0)); i2c_stop(); } // REG 6

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec) { i2c_start(); i2c_write(0xD0); i2c_write(0x00); i2c_start(); i2c_write(0xD1); sec = bcd2bin(i2c_read() & 0x7f); min = bcd2bin(i2c_read() & 0x7f); hr = bcd2bin(i2c_read(0) & 0x3f); i2c_stop(); // Start at REG 0 - Seconds

72 RESTRICTED

RESTRICTED

Appendix D MATLAB code for Graphical trend analysis

delete(instrfindall); clear all clc s=serial('COM9', 'BaudRate', 9600); fopen(s); hold on n=1; while (n<4) Vp = fscanf(s); Vp=str2double(Vp); E(n) = Vp; n=n+1; end

% Delete all previous COM ports

% Clear screen % Change to required COM % Open the serial port for r/w

% Read Data back from serial port % String to double conversion

x=1; while (x<=E(2))

Vt = fscanf(s); Vt=str2double(Vt); C(x) = Vt; x=x+1; end fclose(s); delete(s); day=C(1); month=C(2); year=C(3); hours=C(4); mins=C(5); Fhours=C(6); Fmins=C(7); % Close the serial port % Delete the serial port % Assign values to variables

73 RESTRICTED

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'); ylabel('Voltage value axis'); xlabel('Reading# axis'); ylim([25 45]); %title of figure % Label of Y axix of figure % Label of X axix of figure

if C(11)==1 Channel_1=C(14:length(C)); Channel_1=Channel_1*100;

74 RESTRICTED

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); set(h,'Interpreter','none') % Legend for figure

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

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

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

Vous aimerez peut-être aussi