Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ISSUE NO:1
REV NO:01
Semester-VI
PIC & RTOS LAB
ENGINEERING
LIST OF EXPERIMENTS
CYCLE I
EXPERIMENTS IN 8051
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Simple programming
Logic operations
Timer programming
Serial port programming
Interrupts programming
I/O Devices interfacing
ADC and DAC interfacing
DC Motor control using PWM
Elevator interfacing
Music tone generator interfacing
CYCLE II
EXPERIMENTS IN PIC16f877
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
LIST OF EXPERIMENTS
CYCLE I
EXPERIMENTS IN 8051
1.
Simple programming
2.
Logic operations
3.
Timer programming
4.
5.
Interrupts programming
6.
7.
8.
9.
Elevator interfacing
10.
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
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
A
A
Assign the hexadecimal or
decimal values to the port2 for
multiplication
Stop
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
AIM:
To write a C program to convert hexadecimal number into a decimal number in 8051.
APPARATUS REQUIRED:
KEIL software.
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
A
A
Stop
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
AIM:
To write a C program to perform the logical operations AND, OR, EX-OR and SHIFT
operations in 8051.
APPARATUS REQUIRED:
KEIL software.
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
Stop
PROGRAM:
#include <reg51.h>
void main(void)
{
P0=0x35 & 0x0F;
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 2s complement operation in 8051.
APPARATUS REQUIRED:
ALGORITHM:
KEIL software.
8051 micro controller
FLOWCHART:
Start
Declare a
hexadecimal variable
Stop
PROGRAM:
#include <reg51.h>
Unsigned char x=oxFE
void main()
{
x=~x;
x=x+1;
P0=x;
}
RESULT:
Thus the C program for 2s complement is written and executed in 8051.
Timer programming
Ex.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.
APPARATUS REQUIRED:
ALGORITHM:
Step 1: Start the program
KEIL software.
8051 micro controller
While(
1)
n
Stop
A
A
Invert the port 0
variables
Ex-OR the port 1
variables
While(T
F0==0)
Stop
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;
}
RESULT:
Thus the C program to toggle all the bits of P0 and P2 continuously was written and executed
in 8051.
KEIL software.
8051 micro controller
ALGORITHM:
Step 1: Start the program
Step 2: Declare the header file.
Flowchart:
Start
While(
1)
Start transferring
the letter
Stop
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:
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:
ALGORITHM:
KEIL software.
8051 micro controller
FLOWCHART:
Start
Set the
switch input
Set the timer
Enable the interrupt
While
(1)
No
Yes
Move the data
Stop
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.
STEPWISE ROTATION
CLOCKWISE ROTATION
COUNTER CLOCKWISE ROTATION
APPARATUS REQUIRED:
KEIL software.
8051 microcontroller
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.
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 */
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;
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)
{
int p;
for(p=0;p<=k;p++);
}
RESULT:
Thus the C program to interface STEPPER MOTOR with 8051 microcontroller was
written and executed
.
KEIL software.
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize port A & port B as output ports and port C as input.
FLOWCHART:
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;
RESULT:
Thus the C program for ADC and DAC interfacing using 8051 microcontroller was
written and executed.
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 interface
Connection : Connect the interface
to 26 pin FRC connector of ESAMCB51
Output : 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)
{
for(;d>0;d++);
}
unsigned char readserial()
{
/* Delay routine */
while(!RI);
RI = 0;
return SBUF;
}
{
while(portb&0x01);
speed pulse gets low */
/* wait for
SpdCnt++;
Increment count */
delay(10);
while(!(portb&0x01));
speed pulse gets high */
delay(10);
}while(portb&0x02);
until one time pulse */
return(SpdCnt);
*/
}
/*
/* wait for
/* do
/* 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 =
while(1)
{
/*
");
porta = Cnt;
/* Move half count to run
/* If
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.
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
INSERT SEQUENCE
DELAY
EXECUTE THE
SEQUENCE
STOP
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;
if(m<0)
goto down;
else
if(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.
FLOWCHART:
START
IF
INTERRUP
TT
No Operation
Y
EnableStop
music
STO
P
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;
unsigned char xdata porta _at_ 0xe000;
unsigned char xdata portb _at_ 0xe001;
unsigned char xdata portc _at_ 0xe002;
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
l=0;
control =0x80;
getchar();
printf(" Press the key on key board\n");
printf(" caps lock should be on ");
while(1)
{
while((i=getchar())>=0x41 )
{
if (i<=0x51)
{
i=(i&0x1f);
for(j=0;j<=0x57;j++)
{
portc=0x00;
for (k=0;k<=freq[i];k++)
{
l=l+1;
l=l+1;
l=l+1;
l=l+1;
}
portc=0x0ff;
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.
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
PIC18F8722 PROGRAMS
AIM
To compile and execute the programs in PIC18F and PIC32M
APPARATUS REQUIRED:
PROCEDURE
I)
II)
III)
IV)
V)
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
APPARATUS REQUIRED:
ALGORITHM:
FLOWCHART:
start
Initialize ports
Get input -switches
Display output - LED
Stop
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
APPARATUS REQUIRED:
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.
FLOWCHART:
Start
While
(1)
Move the data
Stop
PROGRAM:
#include<18F8722.h>
#use delay(clock=20000000)
APPARATUS REQUIRED:
ALGORITHM:
FLOWCHART:
Start
No
While(1
)
Yes
Start transferring the
letter
Stop
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()
{
APPARATUS REQUIRED:
ALGORITHM:
FLOWCHART:
PROGRAM:
//--------------------------------------------------------------//
output_f(ch1);
output_high(PIN_A4);
output_low(PIN_A4);
}
void writedata_page2(unsigned long int ch1)
{
busycheck();
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();
}
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);
}
KEYPAD INTERFACING:
Exp.No:14.b.
Aim: To interface keyboard with PIC controller
APPARATUS REQUIRED:
ALGORITHM:
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
#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');
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.
APPARATUS REQUIRED:
ALGORITHM:
FLOWCHART:
GENERAL:
WRITE OPERATION:
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++);
}
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 */
}
void IdleI2C3(void)
{
/* Wait until I2C Bus is Inactive */
void StopI2C3()
{
// Enable the Stop condition
I2C3CONbits.PEN = 1;
}
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();
IdleI2C3();
while(I2C3STATbits.ACKSTAT);
IdleI2C3();
printf("\n\r EEPROM DATA is= %x",data);
//StopI2C3();
}
}
APPARATUS REQUIRED:
ALGORITHM:
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++);
}
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;
PORTA = i+0;
PORTD = 0x0080;
PORTD = 0x00c0;
}
// UWR,URD = 1
// ADDR0,ADDR1 = 0;
// URD = 1
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
Exp.No:16.b.
AIM: To interface ADC with PIC
APPARATUS REQUIRED:
ALGORITHM:
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);
}
}
APPARATUS REQUIRED:
ALGORITHM:
Step 1: Start the program
Step 2: Declare the header file.
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();
}
APPARATUS REQUIRED:
ALGORITHM:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step8:
FLOWCHART
start
Initialize ports
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
APPARATUS REQUIRED:
ALGORITHM:
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.
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 variable
unsigned int cap2=0; // second capture variable
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)
{
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;
}
}
APPARATUS REQUIRED:
ALGORITHM:
PROGRAM:
#include <plib.h>
#define MAX_DUTY 8000
#define SYS_FREQ 20000000
unsigned int Pwm;
// Variable to store calculated PWM value
unsigned char Mode = 0; // Variable to determine ramp up or ramp down
void 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
//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
GUI INTERFACE
Exp.No:20
AIM: To interface GUI with PIC
APPARATUS REQUIRED:
ALGORITHM:
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;
}
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();
}
}
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