0% found this document useful (0 votes)
50 views

PIC Programs (Experiments)

The document contains 10 experiments with PIC16f877A microcontroller programs. The experiments include programs to control LEDs, 7-segment displays, LCD displays, relays and DC motors using switches. The programs demonstrate basic operations like blinking LEDs, displaying numbers and text, and controlling outputs based on switch input state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

PIC Programs (Experiments)

The document contains 10 experiments with PIC16f877A microcontroller programs. The experiments include programs to control LEDs, 7-segment displays, LCD displays, relays and DC motors using switches. The programs demonstrate basic operations like blinking LEDs, displaying numbers and text, and controlling outputs based on switch input state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 32

PIC16f877A Programs

Exp 1: Write a program to first four leds ON and last four leds OFF.
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

Program:
#include<htc.h>
#define _16F877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT );
#define XTAL_FREQ 12MHZ
void main()
{
TRISB=0X00;
PORTB=0Xf0;
}

Exp 2: Write a program to blink all leds (ON & OFF all leds)
Program:
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#include<htc.h>
#define _16F877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT );
#define XTAL_FREQ 12MHZ
void delay(unsigned int a);
void main()
{
TRISB=0X00;
PORTB=0X00;
while(1)
{
PORTB=0XFF;
delay(65000);
PORTB=0X00;
delay(65000);
}
}
void delay(unsigned int a)
{
while(a!=0)
{
a--;
}
}

Exp 3: Write a program to ON LEDS with three switches using linear keypad
switching.
Program:
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#include<htc.h>
#define _16f877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS &WRTEN &
DEBUGDIS & UNPROTECT );
#define XTAL_FREQ 12MHz
#define S1 RD0
#define S2 RD1
#define S3 RD2
#define LED PORTB
void delay(unsigned int a);
void main()
{
TRISB=0X00;
TRISD=0X00;
PORTB=0X00;
S1=1;
S2=1;
S3=1;
while(1)
{
if(S1==0)
{
LED=0XF0;
}

if(S2==0)
{
LED=0x0F;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

}
if(S3==0)
{
LED=0xaa;
}
}
}
void delay(unsigned int a)
{
while(a!=0)
{
a--;
}
}

Exp 4: Write a program to display digit from 0 to 9 on single 7 segment.


Program:
#include<htc.h>
#define _16f877
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS &WRTEN &
DEBUGDIS & UNPROTECT );
#define XTAL_FREQ 12MHz
#define t1 RC0
#define t2 RC1
void delay(unsigned int a);
void main()
{
TRISB =0x00;
TRISC =0x00;
PORTB =0XFF;
t1=0;
t2=1;
while(1)
{
PORTB=0XC0;
delay(65000);
PORTB=0Xf9;
delay(65000);
PORTB=0XA4;
delay(65000);
PORTB=0XB0;
delay(65000);
PORTB=0X99;
delay(65000);
PORTB=0X92;
delay(65000);
PORTB=0X82;
delay(65000);
PORTB=0XD8;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

delay(65000);
PORTB=0X80;
delay(65000);
PORTB=0X98;
delay(65000);
}
}
void delay(unsigned int a)
{
while(a!=0)
{
a--;
}
}

Exp 5: Write a program to display digit from 00 to 99 on double 7 segment.


Program:
#include<htc.h>
#define _16f877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS &WRTEN &
DEBUGDIS & UNPROTECT );
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#define XTAL_FREQ 12MHz


#define t1 RC0
#define t2 RC1
void delay(unsigned int a);
void main()
{
TRISB=0;
TRISC=0;
PORTB=0XFF;
t1=1;
t2=1;
while(1)
{
unsigned char array[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XD8,
0X80, 0X98};
unsigned char a,b,c;
for(a=0;a<10;a++)
{
for(b=0;b<10;b++)
{
for(c=0;c<100;c++)
{
t1=0;
t2=1;
PORTB=array[a];
delay(1000);
t1=1;
t2=0;
PORTB=array[b];
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

delay(1000);
}
}
}
}
}
void delay(unsigned int a)
{
while(a!=0)
{
a--;
}
}

Exp 6: Write a program to display name on LCD 16 X 2


Program:
#include<htc.h>
#define _16f877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS &WRTEN &
DEBUGDIS & UNPROTECT );
#define

XTAL_FREQ 12MHZ

Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12


68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#define PORT_LCD PORTD


#define en RC0
#define rs RC1
void initial(void);
void command(unsigned char a);
void data(unsigned char b);
void delay(unsigned int c);
void main()
{
TRISD=0x00;
TRISC=0x00;
initial();
while(1)
{
command(0x80);
data('N');
data('U');
data('M');
data('I');
data('T');
data('E');
data('C');
data('H');
command(0xc0);
data('S');
data('O');
data('L');
data('U');
data('T');
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

data('I');
data('O');
data('N');
data('S');
}
}
void initial(void)
{
command(0x01);
command(0x0C);
command(0x3C);
command(0x38);
}
void data(unsigned char b)
{
PORT_LCD=b;
rs=1;
en=1;
delay(300);
en=0;
}
void command(unsigned char a)
{
PORT_LCD=a;
rs=0;
en=1;
delay(300);
en=0;
}
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

void delay(unsigned int a)


{
while(a!=0)
{
a--;
}
}

Exp 7: Write a program to display name using string on LCD 16 X 2


Program:
#include<htc.h>
#define _16f877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS &WRTEN &
DEBUGDIS & UNPROTECT );
#define

XTAL_FREQ 12MHZ

#define PORT_LCD PORTD


Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#define en RC0
#define rs RC1
void initial(void);
void command(unsigned char a);
void data(unsigned char b);
void delay(unsigned int c);
void string(unsigned char *d);
void main()
{
TRISD=0x00;
TRISC=0x00;
initial();
while(1)
{
command(0x80);
string("NUMITECH");
command(0xC0);
string("SOLUTIONS");
}
}
void initial(void)
{
command(0x01);
command(0x0C);
command(0x3C);
command(0x38);
}
void data(unsigned char b)
{
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

PORT_LCD=b;
rs=1;
en=1;
delay(300);
en=0;
}
void command(unsigned char a)
{
PORT_LCD=a;
rs=0;
en=1;
delay(300);
en=0;
}
void string (unsigned char *d)
{
while(*d)
{
data(*d++);
}
}

void delay(unsigned int a)


{
while(a!=0)
{
a--;
}
}
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

Exp 8: Write a program to display a digital clock on LCD 16 X 2


Program:
#include<htc.h>
#define _16f877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS &WRTEN &
DEBUGDIS & UNPROTECT );
#define

XTAL_FREQ 12MHZ

#define PORT_LCD PORTD


#define en RC0
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#define rs RC1
void initial(void);
void command(unsigned char a);
void data(unsigned char b);
void delay(unsigned int c);
void string(unsigned char *d);
void ascii(unsigned int t);
void main()
{
unsigned int a,b,c;
TRISD=0x00;
TRISC=0x00;
initial();
while(1)
{
for(a=0;a<=24;a++)
{
for(b=0;b<=60;b++)
{
for(c=0;c<=60;c++)
{
command(0x80);
string("DIGITAL CLOCK");
command(0xc0);
ascii(a);
command(0xc2);
data(':');
command(0xc3);
ascii(b);
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

command(0xc5);
data(':');
command(0xc6);
ascii(c);
}
}
}
}
}
void initial(void)
{
command(0x01);
command(0x0C);
command(0x3C);
command(0x38);
}
void data(unsigned char b)
{
PORT_LCD=b;
rs=1;
en=1;
delay(300);
en=0;
}
void command(unsigned char a)
{
PORT_LCD=a;
rs=0;
en=1;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

delay(300);
en=0;
}
void string (unsigned char *d)
{
while(*d)
{
data(*d++);
}
}
void ascii(unsigned int t)
{
unsigned char array[]={'0','1','2','3','4','5','6','7','8','9'};
unsigned int a,b;
a=t/10;
data(array[a]);
b=t%10;
data(array[b]);
}
void delay(unsigned int a)
{
while(a!=0)
{
a--;
}
}

Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12


68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

Exp 9: Write a program to ON and OFF relay with two switches using linear keypad
switching.
Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
#define sw1 RB0
#define sw2 RB1
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#define relay RD0


void main()
{
TRISB=0X00;
TRISD=0X00;
sw1=1;
sw2=1;
relay=1;
while(1)
{
if(sw1==0)
{
relay=0;
}
if(sw2==0)
{
relay=1;
}
}}
Exp 10: Write a program to control the directions (forward, reverse & stop) of DC gear
motor with three switches using linear keypad switching.
Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
#define sw1 RB0
#define sw2 RB1
#define sw3 RB2
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

#define m1 RD0
#define m2 RD1
void main()
{
TRISB=0X00;
TRISD=0X00;
sw1=1;
sw2=1;
m1=0;
m2=0;
while(1)
{
if(sw1==0)
{
m1=0;
m2=1;
}
if(sw2==0)
{
m1=1;
m2=0;
}
if(sw3==0)
{
m1=0;
m2=0;
}
}
}
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

Exp 11: Write a program to ON LEDS using matrix keypad switching.


Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
void main()
{
unsigned char a=0;
TRISB=0x00;
TRISD=0X00;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

PORTB=0X00;
PORTD=0X00;
main: while(1)
{
PORTB=0x0f;
a=PORTB;
a|=0xf0;
if(a==0xff)
{
goto main;
}
PORTB=0xef;
a=PORTB;
a|=0xf0;
if(a!=0xff)
{
if(a==0xf7)
{
PORTD=1;
}
if(a==0xfb)
{
PORTD=2;
}
if(a==0xfd)
{
PORTD=3;
}
if(a==0xfe)
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

{
PORTD=4;
}
}
PORTB=0xdf;
a=PORTB;
a|=0xf0;
if(a!=0xff)
{
if(a==0xf7)
{
PORTD=5;
}
if(a==0xfb)
{
PORTD=6;
}
if(a==0xfd)
{
PORTD=7;
}
if(a==0xfe)
{
PORTD=8;
}
}
PORTB=0xbf;
a=PORTB;
a|=0xf0;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

if(a!=0xff)
{
if(a==0xf7)
{
PORTD=9;
}
if(a==0xfb)
{
PORTD=10;
}
if(a==0xfd)
{
PORTD=11;
}
if(a==0xfe)
{
PORTD=12;
}
}
PORTB=0x7f;
a=PORTB;
a|=0xf0;
if(a!=0xff)
{
if(a==0xf7)
{
PORTD=13;
}
if(a==0xfb)
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

{
PORTD=14;
}
if(a==0xfd)
{
PORTD=15;
}
if(a==0xfe)
{
PORTD=16;
}
}
}
}

Exp 12: Write a program to ON LEDS using LDR (light dependent resistor) sensor.
Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
#define ldr RD0
#define out PORTB
void main()
{
TRISB=0X00;
TRISD0=1;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

out=0X00;
while(1)
{
if(ldr==0)
{
out=0XFF;
}
if(ldr==1)
{
out=0X00;
}
}
}

Exp 13: Write a program to ON LEDS using IR (Infrared) sensor(line of sight).


Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
#define ir RD0
#define out PORTB
void main()
{
TRISB=0X00;
TRISD0=1;
PORTB=0X00;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

while(1)
{
if(ir==1)
{
PORTB=0XFF;
}
if(ir==0)
{
PORTB=0X00;
}
}
}

Exp 14: Write a program to ON LEDS using IR (Infrared) sensor (joint).


Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
#define ir RD0
#define out PORTB
void main()
{
TRISB=0X00;
TRISD0=1;
PORTB=0X00;
while(1)
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

{
if(ir==0)
{
PORTB=0XFF;
}
if(ir==1)
{
PORTB=0X00;
}
}
}

Exp 15: Write a program to store data in internal EEPROM of microcontroller.


Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
unsigned char read_EEPROM(unsigned char address);
unsigned char u=0X00;
void write_EEPROM(unsigned char address, unsigned char datas);
void main()
{
TRISB=0X00;
write_EEPROM(0,0X0F);
while(1)
{
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

u=read_EEPROM(0);
PORTB=u;
}
}
unsigned char read_EEPROM(unsigned char address)
{
EEADR=address;
EEPGD=0;
RD=1;
return EEDATA;
}
void write_EEPROM(unsigned char address, unsigned char datas)
{
EEADR=address;
EEDATA=datas;
EEPGD=0;
WREN=1;
EECON2=0X55;
EECON2=0XAA;
WR=1;
WREN=0;
while(EEIF==0)
{
}
EEIF=0;
}

Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12


68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

Exp 9: Write a program to ON and OFF buzzer with two switches using linear keypad
switching.
Program:
#include<htc.h>
#define _16f877
__CONFIG(HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
DEBUGDIS & UNPROTECT);
#define XTAL_FREQ 12MHZ
#define sw1 RB0
#define sw2 RB1
#define buzzer RD0
void main()
{
TRISB=0X00;
TRISD=0X00;
sw1=1;
sw2=1;
Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12
68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

buzzer=1;
while(1)
{
if(sw1==0)
{
buzzer=0;
}
if(sw2==0)
{
buzzer=1;
}
}}

Add: Level-4, 23-f , BRS nagar, Ferozepur Road, Ludhiana-12


68485,0161-5088805

Ph: 98888-

Email: [email protected] Website : www.numitechsolutions.com

You might also like