Вы находитесь на странице: 1из 41

PIC16F877A LAB MANUAL

1
BCET, KARAIKAL

EMBEDDED SYSTEM LAB
MANUAL



EXPERIMENTAL PROGRAMS USING PIC16F877A CONTROLLER








DEPARTMENT OF ELECTRONICS AND COMMUNICATION
ENGINEERING
BHARATHIYAR COLLEGE OF ENGINEERING AND TECHNOLOGY
KARAIKAL-609609
PIC16F877A LAB MANUAL

2
BCET, KARAIKAL


CONTENTS
INTRODUCTION

LIST OF EXPERIMENTS
1. VOLTAGE MEASUREMENT
2. WATER PUMP CONTROLLER
3. DIGITAL CLOCK
4. TEMPERATURE MEASUREMENT
5. PC COMMUNICATION
6. RF TRANSMITER AND RECEIVER
7. HOT CHAMPER CONTROLLER
8. OBSTACLE DETECTION THROUGH ULTRASONIC SENSOR.
9. SPRINKLER CONTROLLER
10. LAMP CONTROLLER








PIC16F877A LAB MANUAL

3
BCET, KARAIKAL
I NTRODUCTI ON
EMBEDDED SYSTEMS

An embedded system is a special-purpose computer system designed to perform one or a
few dedicated functions, often with real-time computing constraints. It is usually embedded as
part of a complete device including hardware and mechanical parts. In contrast, a general-
purpose computer, such as a personal computer, can do many different tasks depending on
programming. Embedded systems control many of the common devices in use today

FEATURES OF EMBEDDED SYSTEMS

i. Cost
Micro controllers with the supplementary circuit components are much cheaper than a computer
with an analog and digital I/O.

ii. Size and Weight
Micro controllers are compact and light compared to computers.

iii. Simple applications
If the application requires very few number of I/O and the code is relatively small, which do not
require extended amount of memory and a simple LCD display is sufficient as a user interface, a
microcontroller would be suitable for this application.

iv. Reliability
Since the architecture is much simpler than a computer it is less likely to fail.

v. Speed
All the components on the microcontroller are located on a single piece of silicon. Hence, the
Applications run much faster than it does on a computer.
FEATURES OF PIC16F877

High-Performance RISC CPU

Only 35 single-word instructions to learn.
All single-cycle instructions except for program branches, which are two cycle.
Operating speed: DC 20 MHz clock input DC 200 ns instruction cycle.
Up to 8K x 14 words of Flash Program Memory, Up to 368 x 8 bytes of Data
Memory (RAM), upto 256 x 8 bytes of EEPROM Data Memory.
Pin out compatible to other 28-pin or 40/44-pin PIC16CXXX and PIC16FXXX micro
controllers.




PIC16F877A LAB MANUAL

4
BCET, KARAIKAL
Peripheral Features

Timer0: 8-bit timer/counter with 8-bit prescaler.
Timer1: 16-bit timer/counter with prescaler, can be incremented during Sleep
via external crystal/clock.
Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler.
Two Capture, Compare, PWM modules.
- Capture is 16-bit, max. Resolution is 12.5 ns
- Compare is 16-bit, max. Resolution is 200 ns
- PWM max. Resolution is 10-bit
* Synchronous Serial Port (SSP) with SPI (Master mode) and I2C
(Master/Slave).
* Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI)
with 9-bit address detection.
* Parallel Slave Port (PSP) 8 bits wide with external RD, WR and CS controls
(40/44-pin only).
* Brownout detection circuitry for Brown-out Reset (BOR).
Analog Features
* 10-bit, up to 8-channel Analog-to-Digital Converter (A/D).
* Brownout Reset (BOR).
* Analog Comparator module with:
- Two analog comparators.
- Programmable on-chip voltage reference (VREF) module.
- Programmable input multiplexing from device inputs and internal
voltage reference.
- Comparator outputs are externally accessible.

Special Micro controller Features
* 100,000-erase/write cycle Enhanced Flash program memory typical.
* 1,000,000-erase/write cycle Data EEPROM memory typical.
* Data EEPROM Retention > 40 years.
* Self-re-programmable under software control.
* In-Circuit Serial Programming (ICSP ) via two pins.
* Single-supply 5V In-Circuit Serial Programming.
* Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable
operation.
* Programmable code protection.
* Power saving Sleep mode.
* Selectable oscillator options.
* In-Circuit Debug (ICD) via two pins.






PIC16F877A LAB MANUAL

5
BCET, KARAIKAL
Application
* Industrial control
* Medical systems
* Access control
* Point-of-sale
* Communication gateway
* Embedded soft modem
* General purpose applications




















PIC16F877A LAB MANUAL

6
BCET, KARAIKAL
PIN TERMINATION IN PIC 16F877A CONTROLLER `



PIC16F877A LAB MANUAL

7
BCET, KARAIKAL
EX NO: 1 VOLTAGE MEASUREMENT
AIM:
To write a C Program to measure voltage from 0 to 5 volts and display on 2 digits 7
segment displays.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE.
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER.
PROGRAM
#include<16f877.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
long int value;
int i=0;
unsigned char arr[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
void write_seg(int x1,int x2)
{
unsigned char x=0;
x=(arr[x1]|0x80);
i2c_start();
i2c_write(0x40);
i2c_write(arr[x1]|0x80);
i2c_stop();
PIC16F877A LAB MANUAL

8
BCET, KARAIKAL
i2c_start();
i2c_write(0x42);
i2c_write(arr[x2]);
i2c_stop();
}
void main()
{
setup_adc_ports( RA0_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
while(1)
{set_adc_channel( 0 );
value = read_adc();
i=((value*50)/255);
write_seg(i/10,i%10);
printf(" %d.%d \n\r",i/10,i%10);
}
}
PROCEDURE
1. Compile the above C program using MPLAB IDE Software to create HEX file
2. Down Load the HEX file using PICISP software.
3. Vary the Potmeter, which is connected to ADC Channel 0(TP1)
4. While varying the Potmeter, the corresponding digit in the 7 segment (0-5V) will also
vary automatically.
RESULT
Thus the voltmeter is designed to measure voltages from 0 to 5v in the 7 segment display
and the C program was compiled and executed successfully.

PIC16F877A LAB MANUAL

9
BCET, KARAIKAL
EX NO:2 WATER LEVEL CONTROLLER
AIM:
To design a water pump controller by sensing the low and high level in the water tank.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE.
4. ITB 003 Module.
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER.
PROGRAM
#include<16f877a.h>
#include<stdio.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
volatile int a=0,b=0;
void main()
{
while(1)
{
a=input(PIN_D0);
b=input(PIN_D1);
printf("\n\r %d %d ",a,b);
b=a&b;
printf("%d",b);
if(b == 0)
PIC16F877A LAB MANUAL

10
BCET, KARAIKAL
{
output_high(PIN_D4); // MOT
output_high(PIN_D6); // LED:RED
output_low(PIN_D7); // LED:GREEN

}
else
{
output_low(PIN_D4); // MOT
output_low(PIN_D6); // LED:GREEN
output_high(PIN_D7); // LED:RED
}
if(a==0)
{
output_low(PIN_D6); // LED:GREEN
}
}
}







PIC16F877A LAB MANUAL

11
BCET, KARAIKAL

CONNECTION DIAGRAM

RD
1
RD
4
RD
6
RD
7
RD
0
GND
PA
0
PC
0
PC
1
PC
2
PB
0
1
2
3
4
5
9
8
7
6
PIC16F77A Serial port termination in
ITB-003 Module
PROCEDURE
1. Compile the above C program using MPLAB IDE Software to create HEX file
2. Down Load the HEX file using PIC ISP software.
3. Connections are given as per the connection diagram.
4. After executing the program, the low level in the tank is sensed and it is indicated by
green signal and the high level in the water tank is indicated by red signal. When
water reaches the high level the relay will be cut off.





RESULT
Thus water pump controller is designed to sense low and high in the water tank.


PIC16F877A LAB MANUAL

12
BCET, KARAIKAL
EX NO:3 DIGITAL CLOCK
AIM:
To write a C program to display the real time clock in the LCD display.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE.
4. RTC BOARD
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER.
PROGRAM
#include <16F877A.H>
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
void set_rtc_time();
void get_rtc_time();
void lcd_init();
void delay(unsigned long del);
void lcd_cmd(unsigned char cmd);
void lcd_dat(unsigned char dat);
unsigned char time[]={0X56,0X38,0X10,0X01,0X01,0X11};
unsigned char readtime[0X06];
unsigned long int hour,second,minute,date,month,year;
unsigned int i;
unsigned char rtc[13];
PIC16F877A LAB MANUAL

13
BCET, KARAIKAL
unsigned char initi[5]={0x38,0x01,0x06,0x0C,0x80};
char x[]="REALTIME:";
void data1(char x[])
{
output_high(pin_C0);
output_low(pin_C1);
output_low(pin_C2);
for(i=0;x[i]!='\0';i++)
{
output_D(x[i]);
output_high(pin_C2);
delay_us(500);
output_low(pin_C2);
}
}
void data_display(char x[])
{
output_high(pin_E0);
output_low(pin_E1);
output_low(pin_E2);
for(i=0;x[i]!='\0';i++)
{
output_D(x[i]);
output_high(pin_E2);
delay_us(500);
PIC16F877A LAB MANUAL

14
BCET, KARAIKAL
output_low(pin_E2);
}
}
void data_display1(unsigned char rtc[])
{
output_high(pin_E0);
output_low(pin_E1);
output_low(pin_E2);
for(i=0;x[i]!='\0';i++)
{
output_D(rtc[i]);
output_high(pin_E2);
delay_us(500);
output_low(pin_E2);
}
}
void main()
{
lcd_init();
data_display(x);
set_rtc_time();
while(1)
{

get_rtc_time();
PIC16F877A LAB MANUAL

15
BCET, KARAIKAL

printf("T: %x : %x : %x \t",hour,minute,second);
//printf("D: %x : %x : %x \r\n",readtime[3],readtime[4],readtime[5]);
sprintf(rtc,"%02x:%02x:%02x ",hour,minute,second);
lcd_cmd(0xc0);
//data1(rtc);
data_display1(rtc);
//lcd_dat():
}
}
void set_rtc_time()
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x02);
i2c_write(0x55);
i2c_write(0x15);
i2c_write(0x05);
i2c_stop();
}
void get_rtc_time()
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x02);
PIC16F877A LAB MANUAL

16
BCET, KARAIKAL
i2c_stop();
i2c_start();
i2c_write(0xa1);
second = i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0xa0);
i2c_write(0x03);
i2c_stop();
i2c_start();
i2c_write(0xa1);
minute = i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0xa0);
i2c_write(0x04);
i2c_stop();
i2c_start();
i2c_write(0xa1);
hour = i2c_read(0);
i2c_stop();

}


PIC16F877A LAB MANUAL

17
BCET, KARAIKAL
void delay(unsigned long del)
{
while(del=del-1);
}

void lcd_cmd(unsigned char cmd)
{
output_low(pin_E0);
output_low(pin_E1);
delay(500);
output_d(cmd);
delay(500);
output_high(pin_E2);
delay(500);
output_low(pin_E2);
}

void lcd_dat(unsigned char dat)
{
output_high(pin_E0);
output_low(pin_E1);
delay(500);
output_d(dat);
delay(500);
output_high(pin_E2);
PIC16F877A LAB MANUAL

18
BCET, KARAIKAL
delay(500);
output_low(pin_E2);
}

void lcd_init()
{
unsigned int k=0;
for(k=0;k<5;k++)
{
lcd_cmd(initi[k]);
}
}
CONNECTION DIAGRAM

Vcc
RC4
RC3
GND
PIC16F77A RTC Module
Vcc
Sda
Scl
GND
Red
Yellow
Green
Black




PIC16F877A LAB MANUAL

19
BCET, KARAIKAL
PROCEDURE
1. Compile the above C program using MPLAB IDE Software to create HEX file
2. Down Load the HEX file using PIC ISP software.
3. Connections are given as per the connection diagram.
4. After executing the program, the time will be displayed in the LCD display.














RESULT
Thus the digital clock is designed to display the time in the LCD display.





PIC16F877A LAB MANUAL

20
BCET, KARAIKAL
EX NO: 4 TEMPERATURE MEASUREMENT
AIM:
To write a C Program to display temperature value in the 7 segment using LM35
sensor.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE.
4. VI SENSE 17 MODULE
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER.
PROGRAM
#include<16f877.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
float value;
unsigned int i=0;
unsigned char arr[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
void delay(unsigned long int del)
{
while(del--);
}
void write_seg(int x1,int x2)
{
i2c_start();
PIC16F877A LAB MANUAL

21
BCET, KARAIKAL
i2c_write(0x40);
i2c_write(arr[x1]);
i2c_stop();
i2c_start();
i2c_write(0x42);
i2c_write(arr[x2]);
i2c_stop();
}
void main()
{
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
while(1)
{
set_adc_channel( 2 );
value = read_adc();
value = value;
i=((int)(value));
i=(i*2);
printf(" Temperature in Cel %d \n\r",i);
if(i>=99){i=99;}
write_seg(i/10,i%10);
delay(48000);
}
}
PIC16F877A LAB MANUAL

22
BCET, KARAIKAL
CONNECTION DIAGRAM
GND
Vcc
RA
2
GND
Vcc
o/p
PIC16F77A
VI Sense 17
Module
PROCEDURE
1. Connections are given as per the connection diagram.
2. Compile the above C program using MPLAB IDE Software to create HEX file
3. Down Load the HEX file using PIC ISP software.
4. After executing the program, the corresponding temperature will be displayed in the 7
segment.






RESULT
Thus the C program was written to display the temperature value in the 7 segment using
LM35 sensor



PIC16F877A LAB MANUAL

23
BCET, KARAIKAL
EX NO: 5 PC COMMUNICATION
AIM:
To write a C Program to display the message in the serial window.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE.
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER
3. WINX TALK
PROGRAM
#include<16f877a.h>
#include<stdio.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main()
{
while(1)
{
printf(" Vi Microsystems Pvt Ltd Chennai-95 \n");
}
}




PIC16F877A LAB MANUAL

24
BCET, KARAIKAL
PROCEDURE
1. Compile the above C program using MPLAB IDE Software to create HEX file
2. Download the HEX file using PIC ISP software.
3. After executing the program, the message Vi Microsystems Pvt Ltd Chennai-95
will be seen in the WINX TALK communication window with 9600 baud rate.
















RESULT
Thus the above C program to display the message in the serial window was executed
and verified successfully.



PIC16F877A LAB MANUAL

25
BCET, KARAIKAL
EX NO: 6 REMOTE CONTROL THROUGH FM LINK
AIM:
To write a C program to establish a remote link between two microcontroller using RF
transmitter and receiver and send the message VI Microsystem from the transmitter side to
receiver side.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE.
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER
CONNECTION DIAGRAM
VCC
GND
TXD(RC6)

VCC
GND
RXD

PIC CONTROLLER RF TRANSMITTER


VCC
GND
TXD(RC6)

VCC
GND
TXD

PIC CONTROLLER RF RECEIVER
PIC16F877A LAB MANUAL

26
BCET, KARAIKAL
PROGRAM (TRANSMITTER)
#include<16f877a.h>
#include<stdio.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main()
{
while(1)
{
printf(" Vi Microsystems Pvt Ltd Chennai-95 \n");
}
}

PROCEDURE
1. After executing the program, the message Vi Microsystems Pvt Ltd Chennai-95
will be seen in the WINX TALK communication window with 9600 baud rate IN
THE RECEIVER SIDE.






RESULT
Thus the above Cprogram to establish a remote link between two microcontroller using
RF transmitter and receiver was implemented successfully.
PIC16F877A LAB MANUAL

27
BCET, KARAIKAL
EX NO: 7 HOT CHAMBER CONTROLLER
AIM:
To write a C Program to design a hot chamber to maintain the temperature say at 40
degree centigrade.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE
4. AD590 MOULE
5. MINI FURNANCE
6. TEMPERATURE ON/OFF CONTROLLER.
SOFTWARE USED:
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER
3. WINX TALK
PROGRAM
#include<16f877.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
float value;
unsigned int i=0;
void main()
{
while(1)
{
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 1 );
PIC16F877A LAB MANUAL

28
BCET, KARAIKAL
value = read_adc();
i=(int)(value*0.3);
printf(" %d \n\r",i);
if(i>40)
{
output_low(PIN_D0);
printf(" Above ");
}
else if(i<35)
{
output_high(PIN_D0);
printf(" Below ");
}
}
}
PROCEDURE
1. Connections are given as per the connection diagram.
2. Compile the above C program using MPLAB IDE Software to create HEX file
3. Down Load the HEX file using PIC ISP software.
4. After executing the program, the relay connected to the RD0 will be cut off after the
temperature exceeds 40 degree centigrade which will be displayed in the
communication port.


RESULT
Thus a hot chamber was designed to maintain the temperature say at 40 degrees centigrade and
C program was compiled and executed successfully.
PIC16F877A LAB MANUAL

29
BCET, KARAIKAL
EX NO: 8 OBSTACLE DETECTOR USING ULTRASONIC
AIM:
To write a C Program to design an obstacle detection system using ultrasonic
transmitter receiver.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE
4. ULTRA SONIC SENSOR MODULE
SOFTWARE USED:
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER
PROGRAM
#include<16f877a.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stream=GPS)
int i=0;int a1;char a[];
void delay(unsigned long del)
{
while(del--);
}
void lcd_cmd(unsigned char cmd)
{
output_low(pin_E0);
PIC16F877A LAB MANUAL

30
BCET, KARAIKAL
output_low(pin_E1);
delay(500);
output_D(cmd);
delay(500);
output_high(pin_E2);
delay(500);
output_low(pin_E2);
}

void lcd_dat(unsigned char dat)
{
output_high(pin_E0);
output_low(pin_E1);
delay(500);
output_D(dat);
delay(500);
output_high(pin_E2);
delay(500);
output_low(pin_E2);
}
void lcd_init(void)
{
lcd_cmd(0X38);
delay(500);
lcd_cmd(0X06);
PIC16F877A LAB MANUAL

31
BCET, KARAIKAL
delay(500);
lcd_cmd(0X0C);
delay(500);
lcd_cmd(0X01);
delay(500);
lcd_cmd(0X80);
delay(500);
}
void lcd_disp(unsigned char w[],unsigned char add)
{
unsigned char i=0;
for(i=0;w[i]!='\0';i++)
{
lcd_cmd(add+i);
lcd_dat(w[i]);
delay(500);
}
}

void lcd_con(unsigned int con)
{
lcd_dat(((con%1000)/100)+0x30);
lcd_dat(((con%100)/10)+0x30);
lcd_dat((con%10)+0x30);

PIC16F877A LAB MANUAL

32
BCET, KARAIKAL
}
void print()
{
unsigned char arr[]={"DISTANCE IN CMS"};
lcd_disp(arr,0x80);
}
void get()
{
do{;}while(getc()!='R');
a1=( (getc()-48)*100)+( (getc()-48)*10)+ (getc()-48);
}
void main()
{
lcd_init();
print();
while(1)
{
while(1)
{
get();
lcd_cmd(0xC0);
lcd_con(a1);
}
}
}
PIC16F877A LAB MANUAL

33
BCET, KARAIKAL
CONNECTION DIAGRAM


PIC16F8 77A
RED
BLACK
YELLOW
5
4
3
2
1
VCC
GND
ULTRASONIC
SENSOR MODULE
O/P
9 PIN D MALE IN
PIC16F77A
GND
VCC
9
8
7
6


PROCEDURE
1. Connections are given as per the connection diagram.
2. Compile the above C program using MPLAB IDE Software to create HEX file
3. Download the HEX file using PIC ISP software.
4. After executing the program, the distance covered by the ultrasonic sensor is
displayed in the LCD.




RESULT:
Thus an obstacle detection system was designed using ultrasonic sensor and C program
was compiled and executed successfully

PIC16F877A LAB MANUAL

34
BCET, KARAIKAL
EX NO: 9 SPRINKLER CONTROLLER
AIM:
To write a C Program to design a moisture sensor and sprinkler controller.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE
4. MOISTURE SENSOR MODULE.
5. SPRINKLER CONTROLLER MODULE.
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER
3. WINX TALK
PROGRAM
#include<16f877.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
long int value;
int i=0;
void main()
{
while(1)
{
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel( 1 );
value = read_adc();
PIC16F877A LAB MANUAL

35
BCET, KARAIKAL
i=(int)(value/2);
printf("MOISTURE SENSOR: %3d \n",i);
if(i>80)
{
output_low(PIN_D0);
}
else
{
output_high(PIN_D0);
}
}
}

CONNECTION DIAGRAM
PIC16F877A
RD 0
GND
MOISTURE
SENSOR MODE
RELAY O/P
VCC
BLACK
RED
YELLOW
O/P
VCC
GND
BLACK
YELLOW
RA 1
SPRINKLER
CONTROLLER MODULE

PIC16F877A LAB MANUAL

36
BCET, KARAIKAL
PROCEDURE
1. Connections are given as per the connection diagram.
2. Compile the above C program using MPLAB IDE Software to create HEX file
3. Down Load the HEX file using PIC ISP software.
5. After executing the program, the relay connected to the RD0 will be cut off after the
sensor value exceeds 80, which will be displayed in the communication port.
















RESULT:
Thus a moisture sensor and sprinkler controller was designed and C program was
compiled and executed successfully.


PIC16F877A LAB MANUAL

37
BCET, KARAIKAL
EX NO: 10 LAMP CONTROLLER USING RTC
AIM:
To design a light sensor and real time clock to control a lamp for 1 minute delay.
COMPONENTS REQUIRED
1. PIC16F877A KIT
2. 5 V ADAPTER
3. RS232 CABLE
4. RTC MODULE
5. LDR SET UP.
SOFTWARE USED
1. MPLAB IDE COMPILER.
2. PIC ISP DOWNLOADER
3. WINX TALK.
PROGRAM
#include<16f877a.h>
#include<stdio.h>
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
float value=0;
unsigned int i=0;
unsigned long int hour,second,minute,date,month,year;
void delay(unsigned int del)
{
while(del--);
}

PIC16F877A LAB MANUAL

38
BCET, KARAIKAL
void set_rtc_time()
{
i2c_start();
i2c_write(0xa0);
//i2c_write(0x02);
//i2c_write(0x00);
//i2c_write(0x15);
//i2c_write(0x09);
i2c_stop();
}
void get_rtc_time()
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x02);
i2c_stop();
i2c_start();
i2c_write(0xa1);
second = i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0xa0);
i2c_write(0x03);
i2c_stop();

PIC16F877A LAB MANUAL

39
BCET, KARAIKAL
i2c_start();
i2c_write(0xa1);
minute = i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0xa0);
i2c_write(0x04);
i2c_stop();
i2c_start();
i2c_write(0xa1);
hour = i2c_read(0);
i2c_stop();
}
void main()
{
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_INTERNAL );
set_rtc_time();
while(1)
{
get_rtc_time();
set_adc_channel( 1 );
value = read_adc();
i=(int)(value/2);

PIC16F877A LAB MANUAL

40
BCET, KARAIKAL
year = 0;

if(minute==0x00)
{
year=1;
}
if(minute==0x02)
{
year=1;
}
if(minute==0x04)
{
year=1;
}

printf(" %d Time : %x %x %x \n\r",i,hour,minute,second);

if((year==1)|| (i>70))
{
output_high(PIN_D1);
}
if((year==0) && (i<70))
{
output_low(PIN_D1);
}
PIC16F877A LAB MANUAL

41
BCET, KARAIKAL
}
}
CONNECTION DIAGRAM
PIC16F877A
VCC
GND
LDR MODULE
AD 1
RED
BLACK
YELLOW
O/P
GND
VCC
RD 1
RTC MODULE
YELLOW
GREEN
RED
VCC
SCK
SDA
BLACK
GND
LAMP SETUP
RED
BLACK
YELLOW
RELAY O/P
GND
VCC
PC 3
PC 4


PROCEDURE
1. Connections are given as per the connection diagram.
2. Compile the above C program using MPLAB IDE Software to create HEX file
3. Down Load the HEX file using PIC ISP software.
4. After executing the program, the time will displayed in the communication port and
the Lamp connected to the RD1 will be glow for one minute delay
RESULT:
Thus the light sensor to control the lamp was designed and C program was compiled
and executed successfully.
SHEET

Вам также может понравиться