Anda di halaman 1dari 36

Expt No: 7a

Date:19.3.2015
LED INTERFACING
AIM:
To design and set up a circuit to interface an LED with PIC 16F877A
SOFTWARE USED:
MPLAB_IDE_8_40
COMPONENTS REQUIRED:
Microcontroller: PIC16F877A
led
ALGORITHM
1. Start
2. Set PORT B as output port
3. Begin infinite loop
a. Begin
i. Assign H 0F to PORT B
ii Call delay
iii toggle the value of PORT B
iv call delay
b. End
4. Stop
THEORY
An LED is connected to RB0 pin of Port B. This pin has built-in internal pull-up which
can source a current of up to 25mA. The circuit diagram is shown as follows
PORTB is an 8-bit wide, bi-directional port. The corresponding data direction register
is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input (i.e., put
the corresponding output driver in a Hi-Impedance mode). Clearing a TRISB bit (= 0) will make
the corresponding PORTB pin an output (i.e., put the contents of the output latch on the selected
pin). TRISB register belongs to register bank1 and PORTB belongs to bank 0

PROGRAM IN ASSEMBLY
INCLUDE "P16F877.INC" ; Header File
ORG 0000H
; Specify the memory location
CLRF PCLATH
GOTO START
; Reserve the memory locations from 0000H to 0020H for
ISR
ORG 0020H
DELAY: MOVLW H'09'
; invoke delay routine
MOVWF 20H
; move the content of working register to location 20H
Z2:
MOVLW H'09'
; immediately load data FFH to working register.
MOVWF 21H
Z1:
DECFSZ 21H
GOTO Z1
DECFSZ 20H
GOTO Z2
RETURN
START: BCF STATUS, RP1 ; BANK1 select
BSF STATUS, RP0
CLRF TRISB
; Configure port B output
BCF STATUS, RP0 ; BANK0 select
L1:
MOVLW H'F0'
MOVWF PORTB
; move content of working register (F0) to PORTB
CALL DELAY
MOVLW H'0F'
; move content of working register [ie 0F] to PORTB
MOVWF PORTB
CALL DELAY
GOTO L1
END
PROGRAM IN C
#include<pic.h>
void delay(long g)
{
int i,j;
for(i=0;i<=g;i++)
{
for(j=0;j<255;j++);
}

}
// main progam
void main()
{
TRISB=0x00;
while(1)
{
PORTB=0XF0;
delay(300);
PORTB=~PORTB;
delay(300);
}
}
PROGRAM WINDOW
IN ASSEMBLY

IN C

INTERFACING DIAGRAM

RESULT
LED was interfaced successfully to PIC16F877A.

Expt No: 7 b
Date:21.3.2015
7 SEGMENT DISPLAY INTERFACING
AIM:
To design and setup a circuit to interface PIC microcontroller with 7 segment display.
SOFTWARE USED:
MPLAB_IDE_8_40
COMPONENTS REQUIRED
Microcontroller: PIC16F877A
7 segment display [common anode]
ALGORITHM
1. Start
2. Assign {0X03,0X9F,0X25,0X0D,0X99,0X49,0XC1,0X1F,0X01,0X19}to character array a[]
3. Set PORT B as output port
4. Begin Infinite loop
a. Begin
i. for value of K upto 10
1. Begin
a. Assign a[k] to PORT B
b. Call delay
2. End
b. END
5. Stop
THEORY
The segments in a 7-segment display are arranged to form a single digit from 0 to F. We
can display a multi-digit number by connecting additional displays. Even though LCD displays
are more comfortable to work with, 7-segment displays are still standard in the industry. This is
due to their temperature robustness and visibility.

Segments are marked with non-capital letters: a, b, c, d, e, f, g and dp , where dp is the decimal
point. The 8 LEDs inside each display can be arranged with a common cathode or common
anode. With a common cathode display, the common cathode must be connected to the 0V rail
and the LEDs are turned on with a logic one. Common anode displays must have the common
anode connected to the +5V rail. The segments are turned on with a logic zero. 7-segment
displays come in different colors, including: red, orange, and green.

1 Interfacing :
1- Use a display driver such as the 7447 IC which convert the BCD number to it's seven segment
code with the PIC (number of pins used is 4).
2- Alternatively displays can be driven by a micro-controller (number of pins used is 7 plus the
decimal point).
3- Use 7447 with a decade counter (counter counts from 0 to 9) with the microcontroller (uses
one pin but is not so fast)
DESIGN

PROGRAM
#include<pic.h>
void delay(long g)
{
int i,j;
for(i=0;i<=g;i++)
{
for(j=0;j<255;j++);
}
}
void main()
{
int i,a[10]={0x03,0x9F,0x25,0x0D,0x99,0x49,0xC1,0x1F,0x01,0x19};
TRISB=0x00;
TRISA=0x00;
PORTA=0xFF;
while(1)
{
for(i=0;i<=9;i++)
{
PORTB=a[i];
delay(100);
}
}
}
PROGRAM WINDOW

INTERFACING DIAGRAM

RESULT
7 Segment display was interfaced successfully to PIC16F877A.

Expt No: 7c
Date:21.3.2015

LCD DISPLAY INTERFACING


AIM:
To design and setup a circuit to interface PIC microcontroller with 16*2 LCD display.
SOFTWARE USED:
MPLAB_IDE_8_40
COMPONENTS REQUIRED
Microcontroller: PIC16F877A
16*2 LCD display:
ALGORITHM
1. Start
2 .Initialize command register of LCD using lcd_cmd()
3 .Initialize data register of LCD using lcd_data()
4. Set PORT B and PORT E as output ports
5. Set ADCON1 register as 0X06
6. Assign 0X30, 0X38, 0X0C,0X06, 0X01 to command register one by one
7. Assign string value to the function print _lcd () and .Send each character of string to data
register
8 . Stop

THEORY
Liquid Crystal Display (LCD) is very commonly used electronic display module and having a
wide range of applications such as calculators, laptops, mobile phones etc. 162 character lcd
display is very basic module which is commonly used in electronics devices and projects. It can
display 2 lines of 16 characters. Each character is displayed using 57 or 510 pixel matrix.
PROGRAM
// LCD interfacing
#include <pic.h>
void delay(long lcd_g)
{
int lcd_i,lcd_j;
for(lcd_i=0;lcd_i<=lcd_g;lcd_i++)
{
for(lcd_j=0;lcd_j<250;lcd_j++) ;
}
}
// lcd command mode
void lcd_cmd(char lcd_k)
{
RE0=0;

// select command register

RE1=0;

//write mode

PORTD=lcd_k;
RE2=1;

//enable the lcd

delay(1);
RE2=0;
}
// lcd data mode
void lcd_data(char lcd_c)
{
RE0=1;

//select data register

RE1=0;

//write mode

PORTD=lcd_c;
RE2=1;

// enable the lcd

delay(1);
RE2=0;
}
// send character of a string to LCD one by one.
void print_lcd(const char *ptr){
while(*ptr != 0){ lcd_data(*ptr); ptr++;
}

}
void lcd_initiltn()
{
ADCON1=0X02;

// specify port E digital I/O

TRISE0=0;

// make Port E 0,1 and 2 pin output

TRISE1=0;
TRISE2=0;
TRISD=0;

//make portd output

delay(15);
lcd_cmd(0x30);

//initialize

delay(5);
lcd_cmd(0x30);
delay(1);
lcd_cmd(0x30);
delay(1);
lcd_cmd(0x38);

//2 rows each

lcd_cmd(0x0c);

//cursor off

lcd_cmd(0x06);

//move right

lcd_cmd(0x01);

//clear

lcd_cmd(0x80);
}
void main()
{
lcd_initiltn();
lcd_cmd( 0x86);

// first roww 6th position print_lcd("ASIET");

lcd_cmd( 0xc4);

// second row 4th position print_lcd(" welcome");

while(1);
}

PROGRAM WINDOW

INTERFACING DIAGRAM

RESULT
LCD display was successfully to PIC16F877A

Expt No: 7d
Date:26.3.2015

DC MOTOR INTERFACING
AIM
To design and setup a circuit to interface PIC microcontroller with DC motor.
SOFTWARE USED:
MPLAB_IDE_8_40
COMPONENTS REQUIRED
Microcontroller: PIC16F877A
150 RPM DC motor
Motor Driver IC: L293D
ALGORITHM
1. Start
2. Set Port B as output port and port D as input port
3. Begin infinite loop
a. check if RD0=0 & RD1=0
i.assign RB0=1 & RB1=0 for clock wise rotation
b. Check if RD0=1 &RD1=1
i. assign RB0=0 & RB1=1for anti clock wise rotation
c. else
i. assign RB0=0 & RB1=0
4. Stop

THEORY
DC motor are simple two lead, electrically controlled devices that convert electrical power into
mechanical power through the interaction of 2 magnetic fieldstone field is usually produced by a
stationary permanent magnet and the other field is produced by an electric current flowing in the
motor coil. Rotation of motor can be reversed by simply flipping the terminals of the DC power
supply.
DESIGN
DIRECTION CONTROL

SW1

SW2

MOTOR

ON

ON

CLOCKWISE

ON

OFF

STOP

OFF

ON

STOP

OFF

OFF

ANTICLOCKWISE

PROGRAM:
#include<pic.h>
void delay(long g)
{
int i,j; for(i=0;i<=g;i++)
{
for(j=0;j<255;j++);
}
}
void main()
{
unsigned int k;
TRISB=0x00;
TRISD=0XFF;
while(1)
{
if (RD0==0&&RD1==0)

{
RB0=1;
RB1=0;
}
else if ( RD0==1&&RD1==1)
{
RB0=0;
RB1=1;
}
Else
{
RB0=0;
RB1=0;
}
}
}

PROGRAM WINDOW

INTERFACING DIAGRAM

RESULT
DC motor was interfaced successfully to PIC16F877A

Expt No: 8
Date:26.3.2015

LED CONTROL USING EXTERNAL INTERRUPT


AIM:
To design and setup a circuit to turn ON and turn OFF an LED with External interrupt.
SOFTWARE USED:
MPLAB_IDE_8_40
COMPONENTS REQUIRED
Microcontroller: PIC16F877A
Push Button
ALGORITHM
1. Start
2. Initialize flag=0
3. Make RC 4 pin output and RB) pin input
4. Enable internal pull up
5. Set Global interrupt enable as 1
6. Set external interrupt enable as 1
7. Assign ADCON1=0X07
8. If interrupt is enable go to interrupt intt()
a. Clear INTFF
b. if flag=1
i. Assign RC4=1
c .else

i. Assign RC$=0
9. Stop
THEORY
The interrupt is triggered by an input on RB0, either as the signal goes from 0 to 1, called rising
edge, or as falling edge. This sets the interrupt flag ,and if the interrupt is enabled the
microcontroller goes to the interrupt subroutine ,The use of the RB0/Into to manage interrupt
externally generated requires the setting of a couple of registers of the PIC microcontroller that
are INTCON and OPTION_REG.
PROGRAM
#include<pic.h>
bit flag=0;
void interrupt intt()
{
INTF=0;

// clear interrupt flag

if(flag==1)
{

RC4=1;

// LED ON

flag=0;
}
else
{
RC4=0;
flag=1;
}
}

void main()
{

// LED OFF

TRISC4=0;

// Make Portc 4 output


// Make Portb 0 input

TRISB0=1;
RBPU=0;

// enable internal pull up ( only for portb in INPUT mode)


GIE=1;

INTE=1;
ADCON1=0X07;
while(1);
}
PROGRAM WINDOW

// global interrupt enable


// external interrupt enable

INTERFACING DIAGRAM

RESULT
LED is interfaced to PIC16F877A and controlled using external interrupt

Expt No: 9
Date:28.3.2015

USART SERIAL COMMUNICATION


AIM:
To design and setup a circuit to interface PIC microcontroller with UASRT.
SOFTWARE USED:
MPLAB_IDE_8_40
COMPONENTS REQUIRED
Microcontroller: PIC16F877A
16*2 LCD Display:
ALGORITHM
Transmitter
1 .start
2. Assign string ASIET to a
3. Assign PORT D as input and RC6 as TX output
4. Enable transmission bits
5. Set Baud rate 9600 by assigning SPBRG=129
6. Begin infinite loop
A, Check if RD0=0
i.Transmit each character
ii call delay
7. stop

Receiver
1. Start
2. Assign RC7 as RX input
3. Enable reception bits
4. Enable all interrupt bits
5, Set Baud rate to 9600 by assigning SPBRG =129
6 .Initialize LCD registers
7. Check if RCIF=1 and go to interrupt ()
a. Clear RCIF
b. Assign b=RCREG
c. Display the value of b in LCD
8. Stop
THEORY
USART (Universal Synchronous Asynchronous Receiver transmitter)located within the
PIC is universal communication component, which can be used as transmitter or as receiver. in
this experiment we will set USART in order to allow communication between PC to PIC or
between PIC to PC. If switch SW1 at the transmitter is closed, it continuously transmit the word
ASIET
PROGRAM
TRANSMITTER
#include<pic.h>
char a[6]="ASIET";
void delay(long g)
{
int i,j; for(i=0;i<=g;i++)
{
for(j=0;j<255;j++);
}
}
void data(char k)
{
TXREG=k;
while(TRMT==0);

// delay function

// load 8 bit data transmission register


// wait till 8th bit of data transmit serially

void main()
{
TRISD=0xFF;

// for connecting switch

TRISC=0;

// make TX pin as output

TX9=0;

// 9th bit

TXEN=1;

// Txn enable bit

SYNC=0;
BRGH=1;
SPBRG=129;
SPEN=1;

while(1)
{
if (RD0==0)
{
for(int i=0;i<6;i++)
{
data(a[i]);
delay(300);
}
}
}

// baud rate=9600

PROGRAM WINDOW

INTERFACING DIAGRAM

RECIEVER
PROGRAM:
#include<pic.h>
void delay(long g)
{
int i,j;
for(i=0;i<=g;i++)
{
for(j=0;j<255;j++);
}
}
void lcd_cmd(char lcd_k)
{
RE0=0;

RE1=0;
PORTD=lcd_k;
RE2=1;
delay(1);
RE2=0;
}
void lcd_data(char lcd_c)
{
RE0=1;
RE1=0;
PORTD=lcd_c;
RE2=1;
delay(1);
RE2=0;
}
void lcd_initiltn()
{
ADCON1=0X82;
TRISE0=0;
TRISE1=0;
TRISE2=0;
TRISD=0;
delay(15);
lcd_cmd(0x30);
delay(5);
lcd_cmd(0x30);

delay(1);
lcd_cmd(0x30);
delay(1);
lcd_cmd(0x38);
lcd_cmd(0x0c);
lcd_cmd(0x06);
lcd_cmd(0x01);
lcd_cmd(0x80);
}
void interrupt reception()
{
char b;
RCIF=0;

// clear reception flag

b=RCREG;
lcd_data(b);
}
void main()
{
TRISC7=1;

// make RX pin as input

RX9=0;
SYNC=0;
BRGH=1;
SPBRG=129;
SPEN=1;
CREN=1;
lcd_initiltn();

// baudrate=9600

GIE=1; // enable USART interrupt


PEIE=1;
RCIE=1;
while(1);
}

PROGRAM WINDOW

INTERFACING DIAGRAM

RESULT
USART was interfaced successfully to PIC16F877A.

Expt No: 10
Date:28.3.2015

SQUARE WAVE GENERATION USING TIMER


AIM
To design and setup a circuit to generate a square wave with sec on time using timer.
SOFTWARE USED
MPLAB_IDE_8_40
Components Required
Microcontroller: PIC16F877A
ALGORITHM
1. Start
2. Initialize global variable a=100
3. Assign Port B as output port
4 .Clear TOCS, TOSE,PAS flag bits
5. Set all prescalar rate select bits as 1
6. Enable all interrupts
7. Assign 0XFF to port B
8. Assign 0X3C to TMR0 for 10ms
9. Go to interrupt time() TMR0IF is set
a. Clear TMR0IF
b. Decrement a
c. Check if a=0
i.Toggle Port B

ii Assign a=100
d. Assign TMR0=0X3C
10. Stop
THEORY
PIC 16F877 has 3 different timers PIC Timer0, PIC Timer1, PIC Timer2.We can use
these timers for various important purposes. So for we used delay procedure to implement
some delay in the program, that was counting up to a specific value, before the program could
be continued. Delay procedure had two disadvantages. Now using Timers we can build a very
precise time delays which will be based on the system clock and allow us to achieve our desired
time delay well known in advance PIC Timer Timer 0.
Design

//For 20MHz Oscillator


// Max. time without prescalar : 200ns * 2^8(TMR0)
// = 200ns * 255(TMR0)
// = 51.2us
// Max. time with prescalar : 200ns * 255(TMR0) *
256(prescalar)
// = 13.1ms
// For 10ms prescalar value must be 256
// = 200ns * 256(prescalar) * x(TMR0) = 10ms
// x(TMR0) = 10ms / (200ns * 256(prescalar))
// = 195.31
// Value to TMR0 = 255 - 195
// = 60 ( 3C in HEX)

PROGRAM
#include<pic.h>
unsigned int a;
void interrupt time()
{
TMR0IF=0;

// clear Timer0 flag

a=a-1;
if (a==0)
{
PORTB=~PORTB;
a=100;
}
TMR0=0x3c;
counter whenever the overflow occurs

// Reload the initial value to the

}
void main()
{
a=100;
TRISB=0x00;
T0CS=0;

// Timer 0 clock source select

T0SE=0;

// Timer 0 clock edge select

PSA=0;

// Presale assignment bit

PS2=1;
PS1=2;
PS0=1;
GIE=1;

// Interrupt enable

PEIE=1;
TMR0IE=1;
PORTB=0xFF;
TMR0=0x3c;
10mS.
while (1);
}
PROGRAM WINDOW

// Initial value loaded for

INTERFACING DIAGRAM

OUTPUT WINDOW

RESULT
Square wave is generated using PIC16F877A with time period 20ms