Top Banner
Date:14/11/12 ISSUE NO:1 REV NO:01 DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING Lab Mannual B.Tech- ELECTRONICS AND COMMUNICATION ENGINEERING Semester-VI PIC & RTOS LAB
101

Pic Manual 2012

Jul 09, 2016

Download

Documents

ManojMallidi

pic 18 lab manual
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pic Manual 2012

Date:14/11/12 ISSUE NO:1 REV NO:01

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

Lab Mannual

B.Tech- ELECTRONICS AND COMMUNICATION ENGINEERING

Semester-VI

PIC & RTOS LAB

Page 2: Pic Manual 2012

LIST OF EXPERIMENTS

CYCLE IEXPERIMENTS IN 8051

1. Simple programming2. Logic operations3. Timer programming4. Serial port programming5. Interrupts programming6. I/O Devices interfacing7. ADC and DAC interfacing8. DC Motor control using PWM9. Elevator interfacing10. Music tone generator interfacing

CYCLE IIEXPERIMENTS IN PIC16f877

11. PIC I/O Port Programming12. PIC Timers Programming13. PIC Serial Port Programming14. Interrupt Programming15. LCD and Keypad Interface16. External EEPROM and I2C17. USB, ADC and DAC18. Sensor and other Applications19. CCP and ECCP Programming20. Capture Mode Programming and Pulse Width Measurement21. RS232 Interface Programming22. GUI Interface

Page 3: Pic Manual 2012

LIST OF EXPERIMENTS

CYCLE IEXPERIMENTS IN 8051

1. Simple programming

2. Logic operations

3. Timer programming

4. Serial port programming

5. Interrupts programming

6. I/O Devices interfacing

7. ADC and DAC interfacing

8. DC Motor control using PWM

9. Elevator interfacing

10. Music tone generator interfacing

CYCLE II

EXPERIMENTS IN PIC16f877

11.PIC I/O Port Programming

12.PIC Timers & Interrupt Programming (Interrupt Programming)

13.PIC Serial Port Programming (RS232 Interface Programming)

14.LCD and Keypad Interface

15.External EEPROM and I2C

16.USB, ADC and DAC

17.Sensor and other Applications( Temperature Sensor)

18.CCP and ECCP Programming

19.Capture Mode Programming and Pulse Width Measurement

20.GUI Interface

Page 4: Pic Manual 2012

CYCLE I

EXPERIMENTS IN 8051

1. Simple programming

2. Logic operations

3. Timer programming

4. Serial port programming

5. Interrupts programming

6. I/O Devices interfacing

7. ADC and DAC interfacing

8. DC Motor control using PWM

9. Elevator interfacing

10.Music tone generator interfacing

SIMPLE PROGRAMMING

Page 5: Pic Manual 2012

Ex.No:01a

AIM:To write a C program to perform the arithmetic operations addition, subtraction,

multiplication, division in 8051.

APPARATUS REQUIRED: KEIL software. 8051 micro controller

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the header file.

Step 3: Initializing port0 for addition and the output is stored in P0.

Step 4: Initializing port1 for subtraction and the output is stored in P1.

Step 5: Initializing port2 for multiplication and the output is stored in P2.

Step 6: Initializing port3 for division and the output is stored in P3.

Step 7: Stop the program.

FLOW CHART

Start

Assign the hexadecimal or decimal values to the port0 for addition

Assign the hexadecimal or decimal values to the port1 for subtraction

A

Page 6: Pic Manual 2012

PROGRAM:

#include <reg51.h>void main(void){P0=0x35 + 0x0F; P1=0x04 - 0x68; P2=0x54 / 0x78; P3=0x55*0x10; }

RESULT: Thus the C program for performing Arithmetic operations is written and executed in 8051.

Ex.No:01b

Assign the hexadecimal or decimal values to the port2 for multiplication

Assign the hexadecimal or decimal values to the port3 for division

Stop

A

Page 7: Pic Manual 2012

AIM:To write a C program to convert hexadecimal number into a decimal number in 8051.

APPARATUS REQUIRED:

KEIL software. 8051 micro controller

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the header file.

Step 3: Declare the variables.

Step 4: perform division & modulus operations.

Step 5: store the values in ports P0, P1, P2.

Step 6: Stop the program.

FLOWCHART:

Start

Declare 5 a,b,c,d,e variables

Assign a hexadecimal value to ‘a’ variable

Divide the hexadecimal number by 10 store in ‘b’ variable

A

A

Page 8: Pic Manual 2012

PROGRAM:

#include <reg51.h>Unsigned char w, x, y, z;Unsigned char HD=0x96;void main(){w=HD/10;x=HD%10;y=w%10;z=w/10;P0=x;P1=y;P2=z}

RESULT: Thus the C program for code conversion is written and executed in 8051.

Logic operations

Ex.No:02a

Stop

Take 10modulus for the hexadecimal number and store in ‘c’variable

Take 10 modulus for the ‘b’variable and store in’d’

Divide ‘b’variable and store in ‘e’ variable

Assign c,d,e values to the port

Page 9: Pic Manual 2012

AIM:To write a C program to perform the logical operations AND, OR, EX-OR and SHIFT

operations in 8051.

APPARATUS REQUIRED:

KEIL software. 8051 micro controller

ALGORITHM:Step 1: Start the program.

Step 2: Declare the header file.

Step 3: Initializing port0 for AND operation and the output is stored in P0.

Step 4: Initializing port1 for OR operation and the output is stored in P1.

Step 5: Initializing port2 for EX-OR operation and the output is stored in P2.

Step 6: Giving an input in port0 and inverting it and storing the output in P0.

Step 7: Giving an input in port1 and shifting it right three times and storing the output in P1.

Step 8: Giving an input in port2 and shifting it right four times and storing the output in P2.

Step 9: Giving an input in port0 and shifting it left four times and storing the output in P0.

Step 10: Stop the program.

FLOWCHART:

Start

Page 10: Pic Manual 2012

PROGRAM:#include <reg51.h>void main(void){P0=0x35 & 0x0F;

Assign a hexa decimal value to port0 for AND operation

Assign a hexadecimal value to port1 for OR operation

Stop

Assign a hexadecimal value to port2 for EX-OR operation

Assign a hexadecimal value to port0 for NOT operation

Assign a hexadecimal value to port1 for right shift

Assign a hexadecimal value to port2 for left shift

Page 11: Pic Manual 2012

P1=0x04 | 0x68; P2=0x54 ^ 0x78; P0=~0x55; P1=0x9A >> 3; P2=0x77 >> 4; P0=0x6 << 4; }

RESULT: Thus the C program for performing logical operations is written and executed in 8051.

Ex.No:02b

AIM:To write a C program to perform the 2’s complement operation in 8051.

Page 12: Pic Manual 2012

APPARATUS REQUIRED:

KEIL software. 8051 micro controller

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the header file.

Step 3: declare the variable x.

Step 4: perform one’s complement and add one and store the result in P0.

Step 5: Stop the program

FLOWCHART:

PROGRAM:

#include <reg51.h>Unsigned char x=oxFEvoid main(){

Start

Declare a hexadecimal variable

Perform NOT operation for the variable

Stop

Add 1 to the result is the 2’s complement of the number

Page 13: Pic Manual 2012

x=~x;x=x+1;P0=x;}

RESULT: Thus the C program for 2’s complement is written and executed in 8051.

Timer programmingEx.No:03

AIM:To write a C program to toggle all the bits of P0 and P2 Continuously with delay using

the inverting and Ex-OR operators in 8051.

Page 14: Pic Manual 2012

APPARATUS REQUIRED:

KEIL software. 8051 micro controller

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initializing port0 and port2 with an input value.

Step 4: Inverting the P0 value by a NOT operator.

Step 5: P2 input value is toggled by using EX-OR operation with 0xFF.

Step 6: Calling the delay of 250ms.

Step 7: The bits in P0 and P2 are toggled with the delay.

Step 8: Stop the program.

FLOWCHART:

y

n

Start

Assign the values to the ports

Invert the port 0 variables

Ex-OR the port 1 variables

Stop

While(1)

AA

Page 15: Pic Manual 2012

y

n

PROGRAM:

#include <reg51.h>void MSDelay(unsigned int);void main(void){P0=0x55;P2=0x55;while (1){P0=~P0;P2=P2^0xFF;MSDelay(250);}}

Void MSDelay(){TMOD=0x01;TL0=0x00;TH0=0x35;TR0=1;while (TF0==0);TR0=0; TF0=0;}

Call the delay routine

Initialize the timer register

While(TF0==0)

Toggle the values

Stop

Page 16: Pic Manual 2012

RESULT: Thus the C program to toggle all the bits of P0 and P2 continuously was written and executed in 8051.

SERIAL PORT PROGRAMMINGEx.No:4

AIM:To write a C program to transfer the letter “A” serially at 9600 baud continuously in

8051.

Page 17: Pic Manual 2012

APPARATUS REQUIRED:

KEIL software. 8051 micro controller

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: set the timer1 in mode 2.

Step 4: set the baud rate to 9600.

Step 5: set the SCON register to 50.

Step 6: set the timers run bit.

Step7: start transferring the letter a untill timer interrupt becomes one.

Step 8: Stop the program.

Flowchart: Start

Initialize the timer register

While(1)

A

A

Page 18: Pic Manual 2012

PROGRAM:#include <reg51.h>void main(void){TMOD=0x20;TH1=0XFD;SCON=0x50;TR1=1; While of (1)

{SBUF =’A’;While(TI==0);TI=0;}

}

RESULT: Thus the C program to transfer the letter “A” serially at 9600 baud continuously in 8051 is written and executed.

INTERRUPTS PROGRAMMING

Ex.No:5

AIM:

Start transferring the letter

Stop

Page 19: Pic Manual 2012

To write a C program to continuously get a single bit of data from P1.7 and sends it to P1.0 while simultaneously creating a square wave of 200micro sec period on pin P2.5 .Use timer 0 to create a square wave. Assume that XTAL=24 MHZ.

APPARATUS REQUIRED:

KEIL software. 8051 micro controller

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: set the timer0 in mode 2.

Step 4: Give input to the switch

Step 5: Set the value for TH0

Step 6: set the interrupt enable bit

Step7: start transferring the bit from p1.7 to p1.0 and generate square wave

Step 8: Stop the program.

FLOWCHART:

Start

Set the switch input

Page 20: Pic Manual 2012

No

Yes

PROGRAM:#include <reg51.h>sbit SW =P1^7;sbit IND =P1^0;sbit WAVE=P2^5;void timer0(void) interrupt1{WAVE=~WAVE;}void main(){SW=1;TMOD=0x02;TH0=0xA4;IE=0x82;while(1)

{IND=SW;}

}RESULT: Thus the C program to continuously get a single bit of data from P1.7 and sends it to P1.0 while simultaneously creating a square wave of 200micro sec period on pin P2.5 written and executed.

I/O DEVICES INTERFACING (STEPPER MOTOR)

Ex. No: 6

AIM: To write a C programs to interface STEPPER MOTOR with 8051 microcontroller and

to run the motor in following modes.

Set the timer

Enable the interrupt

While (1)

Move the data

Stop

Page 21: Pic Manual 2012

STEPWISE ROTATIONCLOCKWISE ROTATIONCOUNTER CLOCKWISE ROTATION

APPARATUS REQUIRED: KEIL software.

8051 microcontroller

Stepper motor Interface kit

ALGORITHM:

Step 1: Start the program.

Step 2: Initialize port A, port B and port C as output ports.

Step 3: Move the sequence of input to port A.

Step 4: Call the delay Routine

Step 5: Execute the above steps in a while loop

Step 6: Stop the program

FLOWCHART:

PROCEDURE :

Open the keil software. Click on the project and create a new project. Create a new C

file and type the program. Create the necessary header files and add to the project. Compile the

C file and debug the code. Now the 8051 device is to be connected to PC. Connect the ADC

interface to 26 pin FRC connector of ECAMCB51. Run the program.

Page 22: Pic Manual 2012

PROGRAM:

STEPWISE ROTATION#include<stdio.h>#include <AT89X51.H>void delay(int k);/*Connect the interface over J2 of MCBX51 adapter *//* This program rotates the stepper motor in stepwise */

unsigned char xdata control _at_ 0xe003;unsigned char xdata porta _at_ 0xe000;

void main(){ int i; control = 0x80; while(1) {

for(i=0x11;i<=0x88;i<<= 1)

{ porta = i; P1=i;

delay(25000); }}}

void delay(int k){ int p; for(p=0;p<=k;p++); }

CLOCKWISE ROTATION/*Connect the interface over J2 of MCBX51 adapter *//* This program rotates the stepper motor in clockwise */

#include<stdio.h>#include <AT89X51.H>void delay(int k);

unsigned char xdata control _at_ 0xe003;unsigned char xdata porta _at_ 0xe000;

Page 23: Pic Manual 2012

void main(){int i=0; control = 0x80; while(1) { for( i=0x88;i>=0x11;i>>=1) {porta = i; P1 =i ; delay(1000); } }}void delay(int k){ int p; for(p=0;p<=k;p++) ; }

COUNTER CLOCKWISE ROTATION

/*Connect the interface over J2 of MCBX51 adapter *//* This program rotates the stepper motor in anti clockwise */

#include<stdio.h>#include <AT89X51.H>void delay(int k);

unsigned char xdata control _at_ 0xe003;unsigned char xdata porta _at_ 0xe000;

void main(){ int i; control = 0x80; while(1) { for(i= 0x11;i<=0x88;i<<=1) { porta = i; P1=i; delay(1000);} }}void delay(int k)

Page 24: Pic Manual 2012

{ int p; for(p=0;p<=k;p++); }

RESULT: Thus the C program to interface STEPPER MOTOR with 8051 microcontroller was written and executed

.

ADC and DAC INTERFACINGEx.No:7

AIM: To write a C Program for interfacing ADC and DAC interfacing with 8051 Microcontroller.

APPARATUS REQUIRED:

Page 25: Pic Manual 2012

KEIL software.

8051 micro controller

ADC&DAC interfacing kit

ALGORITHM:

Step 1: Start the program.

Step 2: Initialize port A & port B as output ports and port C as input.

Step 3: Check whether STC is pressed & released.

Step 4: Sending a digital value to port A.

Step 5: Check whether input and output are equal.

Step 6: Call the delay Routine.

Step 7: Execute the above steps in a while loop.

Step 8: Stop the program.

PROCEDURE :

Open the keil software. Click on the project and create a new project. Create a new C

file and type the program. Create the necessary header files and add to the project. Compile the

C file and debug the code. Now the 8051 device is to be connected to PC. Connect the ADC

interface to 26 pin FRC connector of ECAMCB51. Run the program.

FLOWCHART:

Page 26: Pic Manual 2012

PROGRAM:

#include <REG51xD2.H>volatile unsigned char xdata control at_ 0xe003;volatile unsigned char xdata porta _at_ 0xe000;volatile unsigned char xdata portb _at_ 0xe001;volatile unsigned char xdata portc _at_ 0xe002;void main(){

unsigned int i,j,k; void delay(void); control =0x81; while(1){ while((i=(0x02&portc))==0); delay(); while((i=(0x02&portc))==0x02); for(j=0x00;j<=0xff;j++)

{ porta =j; if((k=portc&0x01)==0) break; delay();}}} void delay()

Page 27: Pic Manual 2012

{ int l;for (l=0;l<=0xfff;l++); }

RESULT: Thus the C program for ADC and DAC interfacing using 8051 microcontroller was written and executed.

\

Page 28: Pic Manual 2012

DC MOTOR INTERFACING USING PWMExp No:8

AIM: To write a C Program to interface DC motor with 8051 Microcontroller.APPARATUS REQUIRED:

KEIL software. 8051 micro controller DC Motor Interface Kit

ALGORITHMStep 1: Start the program.

Step 2: Initialize port A, port B and port C as output ports.

Step 3: Move the sequence of input to port A.

Step 4: Call the delay Routine

Step 5: Execute the above steps in a while loop

Step 6: Stop the program

FLOWCHART:

Page 29: Pic Manual 2012

PROCEDURE Open the Keil software. Click on the project and create a new project. Create a new C file and type the program. Create the necessary header files add to the project. Compile the C file and debug the code. Now the 8051 device is to be connected to PC. Connect the ADC interface to 26 pin FRC connector of ECAMCB51. Run the program.

PROGRAM:

/************************** DCMotor interface *****************************

Object : To demonstrate the DCMotor interfaceConnection : Connect the interface

to 26 pin FRC connector of ESAMCB51Output : When you run the

program it runs with the half speed. To increase the speed press

P3.2/INT0 button. To decrease the

speed press P3.3/INT1 button.

**************************************************************************/#include <REG51xD2.H>#include <stdio.h>

unsigned char xdata control _at_ 0xe003;unsigned char xdata porta _at_ 0xe000;unsigned char xdata portb _at_ 0xe001;unsigned char xdata portc _at_ 0xe002;

void delay(unsigned char d) /* Delay routine */{ for(;d>0;d++);} unsigned char readserial() {

while(!RI); RI = 0;

return SBUF; }

Page 30: Pic Manual 2012

void writeserial(unsigned char ch)

{while(!TI);

SBUF=ch; TI=0;

}unsigned char MeasureSpeed() /* Routine for measuring speed */{ unsigned char SpdCnt=0x0; while(portb&0x02); /* wait for one time pulse */ while(!(portb&0x02)); do

{ while(portb&0x01); /* wait for speed pulse gets low */ SpdCnt++; /* Increment count */ delay(10); while(!(portb&0x01)); /* wait for speed pulse gets high */ delay(10); }while(portb&0x02); /* do until one time pulse */ return(SpdCnt); /* return speed */}

void main(){ unsigned char flag = 2,Cnt=0x7f,Spd,ch;

SCON = 0x52; /*

SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 0xe6; /* TH1: reload value for 1200 baud @ 16MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */

control=0x83; _getkey(); printf("Speed = ");

Page 31: Pic Manual 2012

while(1) { porta = Cnt;

/* Move half count to run with half speed */ ch=readserial(); /* If user choice is for Increment */

if(ch=='i'|| ch=='I')

{ if(Cnt>0x1f) Cnt-=0x10;

/* Decrement count to increase Speed */

} else if(ch=='d'|| ch=='D') { if(Cnt<0xe0)

Cnt+=0x10;

/* Incrament count to decrease the speed */

} else if(ch=='s' || ch=='S') { Spd = MeasureSpeed();

/* measure the speed */writeserial(0x08); /* sending back space to

serial port*/writeserial(0x08);

writeserial((Spd/0x0a)+0x30);

writeserial((Spd%0x0a)+0x30); }

}

}

RESULT:

Thus the C program for DC motor interfacing using PWM in 8051 microcontroller was written and executed.

Page 32: Pic Manual 2012

ELEVATOR INTERFACING

EXP NO: 9

AIM:

To write a C program for elevator interfacing using 8051 microcontroller.

APPARATUS REQUIRED: KEIL software. 8051 micro controller Elevator interface Kit

ALGORITHM

Step 1: Start the program.

Step 2: Initialize port A, port B and port C as output ports.

Step 3: Move the sequence of input to port A.

Step 4: Call the delay Routine

Step 5: Execute the above steps in a while loop

Step 6: Stop the program

FLOWCHART:

INITAILIZE PORTS

INSERT SEQUENCE

DELAY

START

EXECUTE THE SEQUENCE

STOP

Page 33: Pic Manual 2012

PROCEDURE Open the Keil software. Click on the project and create a new project. Create a new C file and type the program. Create the necessary header files add to the project. Compile the C file and debug the code. Now the 8051 device is to be connected to PC. Connect the ADC interface to 26 pin FRC connector of ECAMCB51. Run the program.

PROGRAM:#include<stdio.h>#include <REG51.H>

unsigned char xdata control _at_ 0xe003;unsigned char xdata porta _at_ 0xe000;unsigned char xdata portb _at_ 0xe001;unsigned char xdata portc _at_ 0xe002;

void delay(int g);

void main(){int i,j,k,m,n,o,p;int port[9] = { 0,0x3,0x06,0x09,00,0xe0,0xd3,0xb6,0x79};control =0x82;i=0;o=0;p=i;st: p=p|0xf0;porta = p;do{k=portb;k=k|0xf0;j=k;k=k-0xff;}while(k==0);if (j==0xf7)o=3;if (j==0xfb)o=2;if (j==0xfd)o=1;if (j==0xfe) o=0;dec: delay(10000);delay(10000);

m=port[o];m=m-i;

Page 34: Pic Manual 2012

if(m<0)goto down;elseif(m==0)goto reset;else{i++;p=i;n=i|0xf0;porta= n;goto dec;}down : i--;p=i;n=i|0xf0;porta = n;goto dec;

reset : o=o+0x5;porta = port[o];goto st;}

void delay(int g){int h;for(h=0;h<=g;g++){;}}

RESULT:

Thus the C program for elevator interfacing using 8051 microcontroller was written and Executed.

Page 35: Pic Manual 2012

MUSIC TONE GENERATOR

EXP NO: 10

AIM:

To write a C program for music tone generation using 8051 microcontroller.

APPARATUS REQUIRED: KEIL software. 8051 micro controller Music tone generation interfacing kit

ALGORITHMStep 1: Start the program.

Step 2: Initialize port A, port B and port C as output ports.

Step 3: Move the sequence of input to port A.

Step 4: Call the delay Routine

Step 5: Execute the above steps in a while loop

Step 6: Stop the program

FLOWCHART:

Set the switch input

Set the timer0 in mode 2

Enable the interrupt

Stop

START

A

Page 36: Pic Manual 2012

N

Y

PROCEDURE Open the Keil software. Click on the project and create a new project. Create a new C file and type the program. Create the necessary header files add to the project. Compile the C file and debug the code. Now the 8051 device is to be connected to PC. Connect the ADC interface to 26 pin FRC connector of ECAMCB51. Run the program.

PROGRAM:

#include <REG51xD2.H>#include <stdio.h>

unsigned char xdata control _at_ 0xe003; // control port addressunsigned char xdata porta _at_ 0xe000; // port a addressunsigned char xdata portb _at_ 0xe001; // port b addressunsigned char xdata portc _at_ 0xe002; // port c address

void main(){

int i,j, k ,l;unsigned char freq[17] = {0X00,0xb7,0xa8,0x96,0x85,0x7e,0x70,0x64,0x59,0x54,0x4a,0x42,0x3e,0x37,0x32,0x2c,0x29};

#ifndef MONITOR51 /* serial port initialization */SCON = 0x52; /* SCON: mode 1, 8-bit UART, enable rcvr */TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */TH1 = 0xe6; /* TH1: reload value for 2400 baud @ 24 MHz */TR1 = 1; /* TR1: timer 1 run */TI = 1; /* TI: set TI to send first char of UART */#endif

A

IF INTERRUPTT

No Operation

Enable music

STOP

Page 37: Pic Manual 2012

l=0;control =0x80;getchar();printf(" Press the key on key board\n");printf(" caps lock should be on ");

while(1){ /* checking for the key between A-P*/while((i=getchar())>=0x41 ){ if (i<=0x51){ i=(i&0x1f);for(j=0;j<=0x57;j++){ portc=0x00; /*sending low on portc*/for (k=0;k<=freq[i];k++){ l=l+1;l=l+1;l=l+1;l=l+1;}portc=0x0ff; /* sending high on port c*/for (k=0;k<=freq[i];k++)

{ l=l+1;l=l+1;l=l+1;l=l+1;l=l+1;l=l+1;}}

}}} }

RESULT: Thus the C Program for music tone interfacing using 8051 microcontroller was written and executed.

Page 38: Pic Manual 2012

CYCLE II

EXPERIMENTS IN PIC

11.PIC I/O Port Programming

12.PIC Timers & Interrupt Programming (Interrupt Programming)

13.PIC Serial Port Programming (RS232 Interface Programming)

14.LCD and Keypad Interface

15.External EEPROM and I2C

16.USB, ADC and DAC

17.Sensor and other Applications( Temperature Sensor)

18.CCP and ECCP Programming

19.Capture Mode Programming and Pulse Width Measurement

20.GUI Interface

Page 39: Pic Manual 2012

PIC18F8722 PROGRAMS

AIM

To compile and execute the programs in PIC18F and PIC32M

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC32F KIT

PIC KIT-3

PROCEDURE

I) Open the Mplab IDE software

II) Create a new project from the project wizard select the appropriate device

III) Select the tools set

IV) Open the text file write the program save the file with the extension .c

V) Add the file to the project

VI) Compile the program check for the errors

VII) Select the programmer PIC kit3 from the program tab

VIII) Download the program to the kit

Page 40: Pic Manual 2012

PIC I/O PORT PROGRAMMING (SWITCH)

Ex.No:11

AIM:

To write a C program to transfer the data in the switch to the LED using I/O Ports in PIC.

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports

Step 4: Get input from the switches

Step 5: Display the output in LED

Step 6: Stop the program.

FLOWCHART:

start

Initialize ports

Display output - LED

Stop

Get input -switches

Page 41: Pic Manual 2012

PROGRAM:

#include<18F8722.h>

#use delay(clock=20000000)

#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7)

#fuses HS,NOWDT,NOBROWNOUT

int sw_val=0x00;

void main()

{

unsigned char value1=0,j,i;

SET_TRIS_D (0xff);

while(1)

{

sw_val = input_d();

value1=0;

for(i=0,j=7;i<8;i++,j--)

{

value1 |= (((sw_val>>i)&0x01)<<j);

}

output_f(value1);

printf("\n\r Switch1 = %x ",value1);

}

}

RESULT: Thus the data is transfered from switches to LED through I/O ports

Page 42: Pic Manual 2012

PIC TIMERS & INTERRUPT PROGRAMMING

(INTERRUPT PROGRAMMING)

Ex.No:12

AIM:To write a C program to interrupt the processor using timer.

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: set the timer2

Step 4: Get input

Step 5: Set the value for Timer2

Step 6: set the interrupt enable bit

Step7: Stop the program.

Page 43: Pic Manual 2012

FLOWCHART:

PROGRAM:

#include<18F8722.h>

#use delay(clock=20000000)

#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)

#fuses HS,NOWDT

#int_TIMER2

void TIMER2_isr(void)

{

disable_interrupts(INT_TIMER2);

output_f(0xff);

delay_ms(10);

output_f(0x00);

delay_ms(500);

Set the timer

Enable the interrupt

While (1)

Move the data

Stop

Start

Page 44: Pic Manual 2012

printf("\n\r Interrupt Running");

}

void main()

{

enable_interrupts(GLOBAL);

set_tris_f(0xff);

setup_timer_2(T2_DIV_BY_1,0,16);

while(1)

{

printf("\n\r PIC18F8722");

enable_interrupts(INT_TIMER2);

}

}

RESULT: Thus the processor was to interrupted using timer

Page 45: Pic Manual 2012

PIC SERIAL PORT PROGRAMMING

(RS232 INTERFACE PROGRAMMING)

Ex.No:13

AIM:To write a C program to transfer and receive data serially at 38400 baud continuously in PIC.

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Initialize serial ports

Step 5: Get the data from the keyboard

Step 6: transfer the data through serial port

Step 7: Display the data

Step 8: Stop the program.

Page 46: Pic Manual 2012

FLOWCHART:

No

Yes

PROGRAM:

//This Program for Serial Reception and Transmission

#include<18f8722.h>

#use delay(clock=20000000)

#use rs232(baud=19200,xmit=pin_c6,rcv=pin_c7)

#fuses HS,NOWDT,NOBROWNOUT,NOPUT

void main()

{

printf("\n\r Program for Serial Reception and Transmission");

printf("\n\r Press Any Key On the Keyboard");

printf("\n\r");

while(1)

{

putc(getc());

}

}

Start

Initialize the timer register

Start transferring the letter

Stop

While(1)

Page 47: Pic Manual 2012

RESULT: Thus the data was transferred and received data serially at 38400 baud continuously

LCD AND KEYPAD INTERFACE

LCD INTERFACINGExp.No:14.a.

AIM: To write a program to display the character via graphics LCD

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Enable LCD

Step 5: Make RS=0 for data and RS=1 for commands

Step 6: Generate High to low pulse to enable signal to latch the data

Step 7: generate delay

Step 8: Change the control signal according to busy flag condition

Step 9: Check busy flag condition and check D7 bit of data bus if not zero check busy flag the

repeatedly

Step 10: if D7 bit is non zero again put the data on the data bus

Step 11: Stop the program.

Page 48: Pic Manual 2012

FLOWCHART:

PROGRAM://--------------------------------------------------------------////THIS PROGRAM FOR DISPLAY THE CHARACTER VIA GRAPHICS LCD//--------------------------------------------------------------//

#include<18f8722.h>#use delay(clock=20000000)#use rs232(baud=19200,xmit=pin_c6,rcv=pin_c7)#fuses HS,NOWDT,BROWNOUT,NOPUT

unsigned int a[]={

Page 49: Pic Manual 2012

0x1f, 0x20, 0x40, 0x20, 0x1f, // V 0x00, 0x41, 0x7f, 0x41, 0x00, // I 0x00, 0x00, 0x00, 0x00, 0x00, // BLANK 0x7f, 0x02, 0x0c, 0x02, 0x7f, // M 0x00, 0x41, 0x7f, 0x41, 0x00, // I 0x3e, 0x41, 0x41, 0x41, 0x22, // C 0x7f, 0x09, 0x19, 0x29, 0x46, // R 0x3e, 0x41, 0x41, 0x41, 0x3e // 0 };

unsigned int b[]={ 0x46, 0x49, 0x49, 0x49, 0x31, // S 0x07, 0x08, 0x70, 0x08, 0x07, // Y 0x46, 0x49, 0x49, 0x49, 0x31, // S 0x01, 0x01, 0x7f, 0x01, 0x01, // T 0x7f, 0x49, 0x49, 0x49, 0x41, // E 0x7f, 0x02, 0x0c, 0x02, 0x7f, // M 0x46, 0x49, 0x49, 0x49, 0x31, // S };

void busycheck(){ delay_ms(1);}

void writecmd(unsigned int ch){ busycheck(); output_j(0xc0); //CS1,CS2 - high output_h(0x00); // RS,DIOW - low - command write

output_f(ch); output_high(PIN_A4); output_low(PIN_A4);}

void writedata_page1(unsigned long int ch1){ busycheck(); output_j(0x40); //CS1,CS2 - high output_h(0x40); // RS,DIOW - low - command write

output_f(ch1); output_high(PIN_A4); output_low(PIN_A4);}

void writedata_page2(unsigned long int ch1){ busycheck();

Page 50: Pic Manual 2012

output_j(0x80); //CS1,CS2 - high output_h(0x40); // RS,DIOW - low - command write

output_f(ch1); output_high(PIN_A4); output_low(PIN_A4);}

void writedata_page_all(unsigned long int ch1){ busycheck(); output_j(0xC0); //CS1,CS2 - high output_h(0x40); // RS,DIOW - low - command write

output_f(ch1); output_high(PIN_A4); output_low(PIN_A4);}

void write1() { int i; for(i=0;i<35;i++) writedata_page2(b[i]); }void write() { int i; for(i=0;i<40;i++) writedata_page1(a[i]); }

void pageclear(){ int i,y; for(i=0xb8;i<0xc0;i++) { writecmd(i); writecmd(0x40); for (y=0;y<64;y++) writedata_page_all(0x0); } }

void clear_display(){ pageclear();}

Page 51: Pic Manual 2012

void intlcd(){ writecmd(0x3e); writecmd(0x3f); writecmd(0xc0); writecmd(0xb8);}

void main(){ int i; intlcd(); clear_display(); writecmd(0xb8); writecmd(0x50); write(); writecmd(0xba); writecmd(0x50); write1(); while(1);}

RESULT: the character was displayed

Page 52: Pic Manual 2012

KEYPAD INTERFACING:

Exp.No:14.b.

Aim: To interface keyboard with PIC controller

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Ground all rows

Step 5: Check the keys open repeatedly

Step 6: Wait for key debounce

Step 7: Read all columns and check any key down

Step 8: Ground next row

Step 9: Read all columns and check the key is pressed if pressed get the scan code from the table

Step 10: if the key is not pressed ground next row and repeat the step 9

Step 11: Stop the program.

Page 53: Pic Manual 2012

FLOWCHART:

PROGRAM:#include <18F8722.h>#USE DELAY(CLOCK=20000000)#USE RS232(BAUD=19200,XMIT=PIN_C6,RCV=PIN_C7)unsigned char key,i;unsigned int scan[4] = {0x0e,0x0d,0x0b,0x07};

#use fast_io(A)#use fast_io(B)#use fast_io(C)#use fast_io(D)#use fast_io(E)#use fast_io(F)#use fast_io(G)

#BYTE PORTA = 0xF80

Page 54: Pic Manual 2012

#BYTE PORTB = 0xF81#BYTE PORTC = 0xF82#BYTE PORTD = 0xF83#BYTE PORTE = 0xF84#BYTE PORTF = 0xF85#BYTE PORTG = 0xF86

#fuses HS,NOWDT,NOBROWNOUT,NOPUT

void main(){ set_tris_d(0xf0); output_low(pin_d0); output_high(pin_d1); output_high(pin_d2); output_high(pin_d3);

while(1) {

output_low(pin_d0); output_high(pin_d1); output_high(pin_d2); output_high(pin_d3); key=(PORTD & 0xf0)>>0x04;

if(key==0x07) putc('0'); if(key==0x0b) putc('1'); if(key==0x0d) putc('2'); if(key==0x0e) putc('3');

output_low(pin_d1); output_high(pin_d0); output_high(pin_d2); output_high(pin_d3); key=(PORTD & 0xf0)>>0x04;

if(key==0x07) putc('4'); if(key==0x0b) putc('5'); if(key==0x0d) putc('6'); if(key==0x0e) putc('7');

Page 55: Pic Manual 2012

output_low(pin_d2); output_high(pin_d0); output_high(pin_d1); output_high(pin_d3); key=(PORTD & 0xf0)>>0x04;

if(key==0x07) putc('8'); if(key==0x0b) putc('9'); if(key==0x0d) putc('a'); if(key==0x0e) putc('b');

output_low(pin_d3); output_high(pin_d0); output_high(pin_d1); output_high(pin_d2); key=(PORTD & 0xf0)>>0x04;

if(key==0x07) putc('c'); if(key==0x0b) putc('d'); if(key==0x0d) putc('e'); if(key==0x0e) putc('f');

// printf("\n\r Key Value %x",key); }}

RESULT: The Keyboard was interfaced.

Page 56: Pic Manual 2012

EXTERNAL EEPROM & I2CExp.No:15

AIM: To interface External EEPROM with I2C in PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC32 KIT

PIC KIT-3

External EEPROM Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: initialize USB Interface

Step 5: Initialize Serial Communication

Step 6: Initialize memory location

Step 7: Write data into EEPROM

Step 8: Read data from EEPROM

Step 9: Display the Data

Step 10: Stop the program.

Page 57: Pic Manual 2012

FLOWCHART:GENERAL:

WRITE OPERATION:

Page 58: Pic Manual 2012

READ OPERATION:

PROGRAM:

#include "p32xxxx.h"

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF#pragma config POSCMOD = HS, FNOSC = PRI, FPBDIV = DIV_2

unsigned int i, data;char arr[16] = "VI MICRO SYSTEMS";

void delay() { unsigned int i,j; for(i=0;i<0xff;i++) for(j=0;j<0x1f;j++); }

void delay1() { unsigned int i,j; for(i=0;i<0xff;i++) for(j=0;j<0xf;j++); }

Page 59: Pic Manual 2012

void UART2Init() { U2MODE = 0; // clear control register U2BRG = 0x80; U2MODEbits.BRGH = 1; // Set High Baud Rate U2STA = 0; // clear status register U2MODEbits.ON = 1; // Enable UART U2STAbits.UTXEN = 1; // Enable Transmit }

int Putchar(char ch) { while(U2STAbits.TRMT == 0); return(U2TXREG = ch); }

void CloseI2C3() { I2C3CONbits.ON = 0; I2C3BRGCLR = 1; IEC0bits.I2C3SIE = 0; IEC0bits.I2C3MIE = 0; IFS0bits.I2C3SIF = 0; IFS0bits.I2C3MIF = 0; }

void OpenI2C3(unsigned int config1,unsigned int config2) { I2C3BRGSET = config2; I2C3CON = config1; } void i2c_config() { unsigned int config1 = 0,i=0; unsigned int config2 = 0;

CloseI2C3(); //Disbale I2C3 mdolue if enabled previously config1 = 1<<15; config2 = 0x030; // controller frue = 20MHZ - I2c = 100KHZ OpenI2C3(config1,config2); //configure I2C3 } void StartI2C3(void) { I2C3CONbits.SEN = 1; /* initiate Start on SDA and SCL pins */ }

Page 60: Pic Manual 2012

void IdleI2C3(void) { /* Wait until I2C Bus is Inactive */ while(I2C3CONbits.SEN || I2C3CONbits.PEN || I2C3CONbits.RSEN || I2C3CONbits.RCEN || I2C3CONbits.ACKEN || I2C3STATbits.TRSTAT); }

unsigned int MasterWriteI2C3(unsigned char data_out) { I2C3TRN = data_out;

if(I2C3STATbits.IWCOL) /* If write collision occurs,return -1 */ return -1; else { while( I2C3STATbits.TBF ); // wait until write cycle is complete IdleI2C3(); // ensure module is idle

if ( I2C3STATbits.ACKSTAT ) // test for ACK condition received return ( -2 ); else return ( 0 ); } }

unsigned char MasterReadI2C3(void) { I2C3CONbits.RCEN = 1; while(I2C3CONbits.RCEN); I2C3STATbits.I2COV = 0; data = I2C3RCV; }

void i2c_start() { StartI2C3(); IdleI2C3(); while( I2C3CONbits.SEN ); //Wait till Start sequence is completed }

void StopI2C3() { // Enable the Stop condition I2C3CONbits.PEN = 1; }

Page 61: Pic Manual 2012

unsigned i2c_write(unsigned int data) { MasterWriteI2C3(data); //Write Slave address and set master for transmission while( I2C3STATbits.TBF); //Wait till address is transmitted }

void RestartI2C3(void) { I2C3CONbits.RSEN = 1; /* initiate restart on SDA and SCL pins */ }

void EEPROM_WRITE() { i2c_config(); delay(); i2c_start(); delay(); i2c_write(0xae); delay(); i2c_write(0x00); delay(); i2c_write(0x00); delay(); i2c_write(0x04); delay(); StopI2C3(); }void main(void) { char val; int addr = 0x00; UART2Init(); EEPROM_WRITE(); printf("\n\r Wait for Some Time"); while(1) { i2c_config(); delay(); i2c_start();

i2c_write(0xAE);

i2c_write(0x00);

i2c_write(0x00);

RestartI2C3(); IdleI2C3(); delay();

i2c_write(0xAF);

data = MasterReadI2C3();

Page 62: Pic Manual 2012

IdleI2C3(); while(I2C3STATbits.ACKSTAT); IdleI2C3();

printf("\n\r EEPROM DATA is= %x",data); //StopI2C3();

} }

RESULT : The EEPROM was interfaced.

Page 63: Pic Manual 2012

USB, ADC & DAC

UNIVERSAL SERIAL BUS

Exp.No:16.a.

AIM: To interface USB with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC32 KIT

PIC KIT-3

USB Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: initialize USB Interface

Step 5: Initialize Serial Communication

Step 6: Send Command

Step 7: process Command

Step 8: Transfer Data

Step 9: Stop the program.

Page 64: Pic Manual 2012

FLOWCHART:

PROGRAM:

#include <p32xxxx.h>#include <stdio.h>

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF#pragma config POSCMOD = HS, FNOSC = PRI, FPBDIV = DIV_2

unsigned int a=0;unsigned int i,j=0,k=0;unsigned char array[65];

void delay() { int i,j; for(i=0;i<0xFF;i++) for(j=0;j<0xFF;j++); }

Page 65: Pic Manual 2012

void Putchar(char ch) { while(U2STAbits.TRMT == 0); U2TXREG = ch; }

void UART2Init() { U2MODE = 0; // clear control register U2BRG = 0x80; U2MODEbits.BRGH = 1; // Set High Baud Rate U2STA = 0; // clear status register U2MODEbits.ON = 1; // Enable UART U2STAbits.UTXEN = 1; // Enable Transmit }

void main() { //disable JTAG port DDPCONbits.JTAGEN = 0; UART2Init(); TRISA = 0x4000; TRISC = 0x0000; TRISD = 0x0000; TRISE = 0x0000; TRISF = 0X0000; PORTD = 0X00C0; // UWR,URD = 1 PORTF = 0x0003; // SLOE,SLCS = 1; TRISD = 0x0000; printf("USB Interface");

while(1) { //READ operation PORTE = 0x0000; // ADDR0,ADDR1 = 0; if((PORTA & 0x4000) == 0) { TRISD = 0x0000; PORTE = 0x0200; //ADDR0 = low,ADDR1 =1 TRISA = 0x00; PORTF = 0x0001; // SLOE = 1,SLCS = 0 PORTD = 0X00C0; // UWR,URD = 1

for(i=0;i<64;i++) { PORTD = 0X00C0; // UWR,URD = 1 PORTA = i+0; PORTD = 0x0080; PORTD = 0x00c0; }

PORTF = PORTF | 0x0001;

Page 66: Pic Manual 2012

}

//Read operation

PORTE = 0x0000; // ADDR0,ADDR1 = 0; TRISA = 0xFFFF;

if((PORTA & 0x4000) != 0) { PORTD = 0X00C0; // UWR,URD = 1 PORTF = 0x0001; // SLOE = 1,SLCS = 0 PORTF = 0x0000; // SLOE = 1,SLCS = 0

PORTD = ( PORTD & 0XFF7F); // URD = 1 PORTD = ( PORTD | 0x0080);

for(i=0;i<64;i++) { array[i]=PORTA; }

PORTD = (PORTD | 0x0080); PORTF = 0x0003; // SLOE = 1,SLCS = 1 }

for(i=0;i<1;i++) { printf("\n\r The Readed Value is %x", array[i]); } }}

Result : Thus the USB is interfaced with PIC

Page 67: Pic Manual 2012

ANALOG TO DIGITAL CONVERTER

Exp.No:16.b.

AIM: To interface ADC with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

ADC Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Select channel and latch the address

Step 5: Send start of conversion pulse

Step 6: Read EOC signal

Step 7: if EOC=1continue else go to step 6

Step 8: Read digital output

Step 9: Stop the program.

FLOWCHART:

Page 68: Pic Manual 2012

PROGRAM:

#include<18F8722.h>#use delay(clock=20000000)#use rs232(baud=19200,xmit=pin_c6,rcv=pin_c7)int adc_data;void main(){ setup_adc_ports(PIN_A0); // RA1 - ADC setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(0);

while(1) { adc_data = read_adc(); printf("\n\r Adc Data : %x",adc_data); }}

RESULT: Thus ADC is interfaced with PIC

Page 69: Pic Manual 2012

DIGITAL TO ANANLOG CONVERTER

Exp.No:16.c.

AIM: To interface DAC with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

DAC Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Send Low value to the port

Step 5: Call delay

Step 6: Send High value to the port

Step 7: Repeat process

Step8: Stop the program.

FLOWCHART:

Page 70: Pic Manual 2012

PROGRAM:#include <18F8722.h>#use delay(clock=20000000)#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)#use I2C(MASTER,sda=PIN_C4,scl=PIN_C3)

#fuses HS,NOWDT,NOPUT,NOBROWNOUT

int8 data,i;

void main(){ i2c_start(); i2c_write(0x9e); //96 write 97 read device address i2c_write(0x40); //control register while(1) { i2c_write(0xff); delay_ms(10); i2c_write(0x00); delay_ms(10); } i2c_stop(); }

RESULT: Thus DAC is interfaced with PIC

Page 71: Pic Manual 2012

SENSOR AND OTHER APPLICATIONS (TEMPERATURE SENSOR)

Exp.No:17

AIM: To interface temperature sensor with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

Temperature Sensor Interface Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Get sensor input

Step 5: Process the data

Step 6: connect to display mode

Step 7: Display the temperature

Step8: Stop the program.

start

Page 72: Pic Manual 2012

FLOWCHART

PROGRAM:#include <p32xxxx.h>#include <plib.h>#include <stdio.h>

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF#pragma config POSCMOD = HS, FNOSC = PRI, FPBDIV = DIV_2

void delay() { int i,j; for(i=0;i<0x1FF;i++) for(j=0;j<0xFF;j++); } void UART2Init() { U2MODE = 0; // clear control register U2BRG = 0x81; U2MODEbits.BRGH = 1; // Set High Baud Rate U2STA = 0; // clear status register U2MODEbits.ON = 1; // Enable UART U2STA = 0x1400; // Enable Transmit }

void Putchar(char ch) { while(U2STAbits.TRMT == 0); U2TXREG = ch; }

void ADC_Init() { AD1PCFG = 0xFFFb; // AN2 as analog, all other pins are digital AD1CON1bits.FORM = 4; AD1CHS = 0x00020000; AD1CSSL = 0; // no scanning required

Initialize ports

Get input temperature

Process the input

Display the temperature

Stop

Page 73: Pic Manual 2012

AD1CON3 = 0x0002; AD1CON2 = 0x0000; AD1CON1SET = 0x8000; /* Turn on the ADC module */ }

int main() { int ADCValue; float val; //disable JTAG port DDPCONbits.JTAGEN = 0; ADC_Init(); UART2Init(); while(1) { AD1CON1bits.SAMP = 1;/* Start sampling the input */ delay();/* Ensure the correct sampling time has elapsed before starting a conversion.*/ AD1CON1bits.SAMP = 0;/* End Sampling and start Conversion*/ while (!AD1CON1bits.DONE); // conversion done? ADCValue = ADC1BUF0; // yes then get ADC value val = ADCValue*3.22; val = val/10; printf("\n\r ADCValue is %f", val); } return(0); }

RESULT: Thus Temperature sensor is interfaced with PIC32 and Measured

Page 74: Pic Manual 2012

CCP AND ECCP PROGRAMMINGExp.No:18

AIM: To interface CCP and ECCP with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC32 KIT

PIC KIT-3

CCP Interface Kit

ALGORITHM:Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Set the PWM period by writing to the PR2 register.

Step 5: Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.

Step 6: Make the CCP1 pin an output by clearing the TRISC<2> bit.

Step 7: Set the TMR2 prescale value and enable Timer2 by writing to T2CON.

Step 8: Configure the CCP1 module for PWM operation.

Step7: Stop the program.

Page 75: Pic Manual 2012

FLOWCHART:

PROGRAM:

#include <plib.h>

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF#pragma config POSCMOD = HS, FNOSC = PRI, FPBDIV = DIV_2

int i;unsigned int cap1=0; // first capture variableunsigned int cap2=0; // second capture variable

Page 76: Pic Manual 2012

void delay() { int i,j; for(i=0;i<0xff;i++) for(j=0;j<0x1f;j++); }

int Putchar(int ch) { while(U2STAbits.TRMT == 0); return(U2TXREG = ch); }

void UART2Init() { U2BRG = 0x81; // 19200 baud rate U2MODE = 0; // clear control register U2MODEbits.BRGH = 1; // Set High Baud Rate U2STA = 0; // clear status register U2MODEbits.UARTEN = 1; //Enable UART U2STAbits.UTXEN = 1; //Enable Transmit }

void Capture1Clear() { IC1CON = 0x0000; }

void Timer2Init() { TMR2=0; PR2 =0x00; T2CONbits.TON = 0; T2CONbits.TCS = 0; // Set Timer freq = Fcy/2 T2CONbits.T32 = 1; T2CONbits.TCKPS = 0; // prescale 1:1 T2CONbits.TGATE = 0; T2CONbits.TSIDL = 0; T2CONbits.TON = 1; //Timer 3 Enable }

void Capture1Init() { IC1CONbits.ICM = 3; IC1CONbits.ICI = 0; IC1CONbits.ICTMR = 1; IC1CONbits.C32 = 1; IC1CONbits.FEDGE = 1; IC1CONbits.ON = 1; }

int main(void)

Page 77: Pic Manual 2012

{ unsigned int counts,i=0; unsigned long frequency; unsigned long int val=0; mIC1ClearIntFlag(); UART2Init(); Capture1Clear(); Timer2Init(); Capture1Init(); printf("welcome"); while(1) { while(IEC0bits.IC1IE); cap1 = IC1BUF; while(IEC0bits.IC1IE); cap2 = IC1BUF; counts = cap2 - cap1; frequency = 10000000/counts; delay(); printf("\n\r The Square wave counter is %ld",frequency); cap1 = 0; cap2 = 0; IC1BUF = 0; val=0; i=0; }}

RESULT: Thus CCP and ECCP is interfaced with PIC32

Page 78: Pic Manual 2012

CAPTURE MODE PROGRAMMING AND PULSE WIDTH MEASUREMENT

Exp.No:19

AIM: To interface CMP and PWM with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC18F8722 KIT

PIC KIT-3

CMP interface Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Set CCPI flag high and then clear

Step 5: Process the input

Step 6: Start CCPI compare operation and measure width

Step7: Stop the program.

FLOWCHART:

Page 79: Pic Manual 2012

PROGRAM:

#include <plib.h> #define MAX_DUTY 8000#define SYS_FREQ 20000000unsigned int Pwm; // Variable to store calculated PWM valueunsigned char Mode = 0; // Variable to determine ramp up or ramp downvoid OC1_Init(void) { OC1CON = 0x0000; // Turn off the OC1 when performing the setup OC1R = 0x0064; // Initialize primary Compare register OC1RS = 0x0064; // Initialize secondary Compare register OC1CON = 0x0006; // Configure for PWM mode without Fault pin enabled PR2 = 0x00C7; // Set period IFS0CLR = 0x00000100; // Clear the T2 interrupt flag IEC0SET = 0x00000100; // Enable T2 interrupt IPC2SET = 0x0000001C; // Set T2 interrupt priority to 7 T2CONSET = 0x8000; // Enable Timer2 OC1CONSET = 0x8000; // Enable OC1} int main(void){ // Configure the proper PB frequency and the number of wait states SYSTEMConfigPerformance(SYS_FREQ); // Allow vector interrupts INTEnableSystemMultiVectoredInt(); // init OC1 module

Page 80: Pic Manual 2012

//OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); OC1_Init(); // init Timer2 mode and period (PR2) (frequency of 1 / 20 kHz = (3999 + 1) / 80MHz * 1 OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, MAX_DUTY); asm("nop"); while(1); CloseOC1();

}

void __ISR( _TIMER_2_VECTOR, ipl7) T2Interrupt( void) { if ( Mode ) { if ( Pwm <= MAX_DUTY ) // ramp up mode { Pwm ++; // If the duty cycle is not at max, increase OC1RS = Pwm; // Write new duty cycle } else { Mode = 0; // PWM is at max, change mode to ramp down } } // end of ramp up else { if ( Pwm > 0 ) // ramp down mode { Pwm --; // If the duty cycle is not at min, increase OC1RS = Pwm; // Write new duty cycle } else { Mode = 1; // PWM is at min, change mode to ramp up } } // end of ramp down // clear interrupt flag and exit mT2ClearIntFlag();} // T2 Interrupt

RESULT: Thus CMP and PWM is interfaced with PIC32

Page 81: Pic Manual 2012

GUI INTERFACEExp.No:20

AIM: To interface GUI with PIC

APPARATUS REQUIRED:

MP LAB IDE 8.76

PIC 32 KIT

PIC KIT-3

GUI Interface Kit

ALGORITHM:

Step 1: Start the program

Step 2: Declare the header file.

Step 3: Initialize ports.

Step 4: Get the input

Step 5: Process the input

Step 6: Enable Display mode

Step7: Transfer the data

Page 82: Pic Manual 2012

Step 8: Stop the program.

FLOWCHART:

PROGRAM:

#include "p32xxxx.h"#include "image3.h"#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF#pragma config POSCMOD = HS, FNOSC = PRI, FPBDIV = DIV_2 void delay() { unsigned int i,j; for(i=0;i<0xff;i++); }

void busycheck() { unsigned int i,j; for(i=0;i<0x1ff;i++); }

void writecmd(unsigned int ch) { busycheck(); PORTB = 0x1800; //RS,RW-LOW,CS1,CS2-HIGH PORTA = ch; PORTB = 0x1c00; PORTB = 0x1800; }

void writedata_page_all(unsigned int ch1) {

Page 83: Pic Manual 2012

busycheck(); PORTB = 0x1900; PORTA = ch1; PORTB |= 0x1d00; PORTB |= 0x1900; }

void writedata_page1(unsigned int ch1) { busycheck(); PORTB = 0X0900; PORTA = ch1; PORTB |= 0x0d00; PORTB |= 0x0900; }

void writedata_page2(unsigned int ch1) { busycheck(); PORTB = 0x1100; PORTA = ch1; PORTB |= 0x1500; PORTB |= 0x1100; }

void glcd_init() { writecmd(0x3e); writecmd(0x3f); writecmd(0xc0); writecmd(0xb8); }

void clear_display(){ int i,y; for(i=0xb8;i<0xc0;i++) { writecmd(i); writecmd(0x40); for (y=0;y<64;y++) writedata_page_all(0x0); delay(); }}

void write_house(unsigned int col){ int i; busycheck();

Page 84: Pic Manual 2012

PORTB = 0X0900; for(i=col;i<(64+col);i++) writedata_page1(house_bmp[i]);}

void write_house1(unsigned int col){ int i; busycheck(); PORTB = 0X0900; for(i=col;i<(64+col);i++) writedata_page2(house_bmp[i]);}

void main() { //disable JTAG port DDPCONbits.JTAGEN = 0; int page,dat; TRISA = 0x0000; TRISB = 0x0000; glcd_init(); clear_display();

while(1) { for(page =0;page<8;page++) { dat = page * 128 ; writecmd(0xB8+page); writecmd(0x40); write_house(dat);

writecmd(0xB8+page); writecmd(0x40); write_house1(dat + 64); } }}

RESULT: Thus GUI is interfaced with PIC32