S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 1 S.E. (I.T) PIC18F LAB Manual Ver 1.0. S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 2 Expt 1 :Writ of two hexadecimal numbers ................................ ................................ ............ 7 Expt 2 :Wr8Fxxx ................................ ................................ ................................ ........................ 9 Expt 3 : /Servo xxx ................................ ................................ ................................ .................. 12 Expt 4 : e ................................ ................................ ................................ ................................ 14 Expt 5 : D ................................ ................................ ................................ ................................ 16 S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 3 Aim: Write an embedded c program to add two hexadecimal numbers Experimental Setup: MicroPIC18F board, USB cable, Power supply adaptor, MPLABx IDE, PICLoader software. Procedure: Step 1: Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step 2: Write the program in C language to add two hexadecimal numbers Step 3: Build the program and create hex file. In case of errors correct program and rebuild to create hex fil e. Step 4: Select Window - >Target memory views - > Configuration bits from tool bar . Select appropriate settings, generate source code and paste the configurations in the main program. Step 5: Select debug project and then Finish debugger session from the tool bar. Step 6: Select Window - >Target memory views - > SFRs to view the output. Result: Expt 1 :Write an Embedded C program for addition of two hexadecimal numbers S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 4 Program: #include<stdio.h> #include<stdlib.h> #include<pic18f4550.h> #include <xc.h> void main(void) { int sum; // Variable declaration of type int sum=0; // Initialize sum to zero sum=0x0A+0x02; TRISB=0; // Initialize port B for output PORTB=sum; // Output written from sum to port B } S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 5 Aim : Embedded C programming for Array addition of n numbers Experimental Setup : MicroPIC18F board, USB cable, Power supply adaptor, MPLA Bx IDE, PICLoader software. Procedure: Step 1: Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step 2: Write the program in C language to add two hexadecimal numbers Step 3: Build the program and create hex file. In case of errors correct program and rebuild to create hex file. Step 4: Select Window - >Target memory views - > Configuration bits from tool bar . Select appropriate settings, generate source code and paste the configur ations in the main program. Step 5: Select debug project and then Finish debugger session from the tool bar. Step 6: Select Window - >Target memory views - > SFRs to view the output. Result : Expt 7:Write an Embedded C program for array addition of n numbers S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 6 Program: #include <stdio.h> #include <stdlib.h> #include <pic18f4550.h> void main(void){ inti,sum; int number[]={1,2,3,4,5,6,7,8,9,10}; sum = 0; for(i=0;i<=9;i++) { sum = sum+number[i]; } TRISB=0; PORTB=sum; } S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 7 Aim : Embedded C program to transfer element from 1 location to another Experimental Setup: MicroPIC18F board, USB cable, Power supply adaptor, MPLABx IDE, PICLoader software. Procedure : Procedure: Step 1: Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step 2: Write the program in C language to add two hexadecimal numbers Step 3: Build the program and create hex file. In case of errors correct program and rebuild to create hex file. Step 4: Select Window - >Target memory views - > Configuration bits from tool bar . Select appropriate settings, generate source code and paste the configurations in the main program. St ep 5: Select debug project and then Finish debugger session from the tool bar. Step 6: Select Window - >Target memory views - > SFRs to view the output. Result : Expt 3 :Write an Embedded C program to transfer element from 1 location to another S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 8 Program: #include<stdio.h> #include<stdlib.h> #include<pic18f4550.h> void main(void) { inttemp,i; int source1[]={0x21,0x22,0x23,0x24,0x25}; intdest[]={0x99,0x99,0x99,0x99,0x99}; for(i=0;i<=4;i++) { temp=source1[i]; source1[i]=dest[i]; dest[i]=temp; } } S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 9 Procedure : Step 1: Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step 2: Write the program in C language for for division operation for 8 bit number by 8 bit number Step 3: Build the program and create hex file. In case of errors correct program and rebuild to create hex file. Step 4: Select Window - >Target memory views - > Config uration bits from tool bar Select appropriate settings, generate source code and paste the configurations in the main program. Step 5: Select debug project and then Finish debugger session from the tool bar. Step 6: Select Window - >Target memory views - > SF Rs to view the output. Expt 4.1:Write an Embedded C program for multiplying and dividing 8 bit number S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 10 Program: MULTIPLYING CODE: #include<stdio.h> #include<stdlib.h> #include<pic18f4550.h> void main(void) { int num1,num2; int result,i; result=0; num1=0x03; num2=0x10; for(i=1;i<=num2;i++) { result= result+num1; } TRISB=0; PORTB=result; } DIVIDING CODE: #include<stdio.h> #include<stdlib.h> #include<pic18f4550.h> void main(void) { in ti, dividend, divisor, quotient, remainder; dividend=0x08; divisor=0x02; quotient=0; remainder=0; for(i=0; dividend>=divisor;i++) { dividend = dividend - divisor; quotient = quotient+1; } remainder =dividend; TRISB=0; PORTB=quotient; } S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 11 S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 12 Aim : 5.1 Embedded C Program for sorting the number s in an array in ascending & descending order 5.2 Unsigned int time dela y Experimental Setup : MicroPIC18F board, USB cable, Power supply adaptor, MPLABx IDE, PICLoader software. Step 1: Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step 2: Write the program in C language for for sorting the number s in an array in ascending & descending order Step 3: Build the program and create hex file. In case of errors correct program and rebuild to create hex file. Step 4: Select Window - >Target memory views - > Configuration bits from tool bar Select appropriate settings , generate source code and paste the configur ations in the main program. Step 5: Select debug project and then Finish debugger session from the tool bar. Step 6: Select Window - >Target memory views - > File Register s to view the output. Expt 5.1: Embedded C program for sorting the numbers in an array in asc and desc order 5.2: Unsigned int time dela y S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 13 Program: 5.1: #include <stdio.h> #include <stdlib.h > #include <pic18f4550.h> void main(void) { inti,j,temp; intnum_asc[]={10,2,5,1,6}; for(i=0;i<=4;i++) { for(j=i+1;j<=4;j++) if(num_asc[i]>num_asc[j]) { temp=num_asc[i]; num_asc[i]=num_asc[j]; num_asc[j]=temp; } } } 5.2: #include <stdio.h> #include <stdlib.h> #include <pic18f4550.h> void delay(unsigned int time) { unsigned inti,j; for(i=0;i<time;i++) for(j=0;j<5000;j++); } void main(void) { TRISB = 0X00; LATB = 0Xff; while(1) { LATB = ~L ATB; delay(50); } } S.E (I.T) PIC18F Lab Manual Microembedded Technologies™, ©2021 Page 14 S.E (I.T) PIC18F Lab Manual Microembedded Technologies ™ , ©2021 Page 1 S.E. (I.T) PIC18F LAB Manual Ver 1.0. S.E (I.T) PIC18F Lab Manual Microembedded Technologies ™ , ©2021 Page 2 Expt 6:Write an Embedded C program for interfacing PIC18FXXX to LED and blinking it using specified delay. ....................................................................................................................... 3 Expt 7:Write an Embedded C program for ISR based buzzer on/off using Timer.......... 5 Expt 8:Write an Embedded C program for External Interrupt input switch press, output at Relay. ...................................................................................................................................... 7 Expt 9:Write an Embedded C program for LCD interfacing with PIC18Fxxx. .................. 9 Expt 10: Write an Embedded C program for generating PWM signal for DC/Servo motor on PIC18Fxxx. ......................................................................................................................... 12 Expt 11: Write an Embedded C program for PC communication using serial interface (UART). ................................................................................................................................................... 14 Expt 12: Write an Embedded C program for interfacing PIC18FXXX to Temperature sensor interfacing using ADC & display on LCD ...................................................................... 16 S.E (I.T) PIC18F Lab Manual Microembedded Technologies ™ , ©2021 Page 3 Expt 6:Write an Embedded C program for interfacing PIC18FXXX to LED and blinking it using specified delay. Aim: To write a C program to interface PIC18F4550 to LED and blink it with a specified delay. Experimental Setup: MicroPIC18F board, USB cable, Power supply adaptor, MPLABx IDE, PICLoader software. Procedure: Step1: Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step2: Write the program in C language for interfacing LEDs to PIC18F4550. (in program properties make sure to add the 0x800 offset) Step3: Build the program and create hex file. In case of errors correct program and rebuild to create hex file. Step4: Prepare the experimental setup by connecting the MicroPIC18F board to the PC using USB cable. Power ON the Board. Check for the USBtoSerial COMx allocated by the PC. Step5: Using the PICLoader Software flash the hex file in the PIC18F4550. Step6: Press reset button and execute the program. Result: Check if the LEDs are blinking. You can change the delay and vary the blinking rate. S.E (I.T) PIC18F Lab Manual Microembedded Technologies ™ , ©2021 Page 4 Program: #include <p18f4550.h> void delay(unsigned int time) { unsigned int i,j; for(i=0;i<time;i++) for(j=0;j<5000;j++); } void main(void) { TRISB = 0x00; LATB = 0xFF; while(1) //Loop forever; { LATB = ~LATB; delay(200); } } S.E (I.T) PIC18F Lab Manual Microembedded Technologies ™ , ©2021 Page 5 Expt 7:Write an Embedded C program for ISR based buzzer on/off using Timer. Aim : To write a C program to interface PIC18F4550 to Buzzer and switch it ON/OFF using Timer ISR.. Experimental Setup : MicroPIC18F board, USB cable, Power supply adaptor, MPLABx IDE, PICLoader software. Procedure : Step1 : Open MPLABX IDE on the PC for program development and create a new project and save it in a new folder. Step2 : Write the program in C language for interfacing Buzzer to PIC18F4550, using Timer ISR. (in program properties make sure to add the 0x800 offset) Step3 : Build the program and create hex file. In case of errors correct program and rebuild to create hex file. Step4 : Prepare the experimental setup by connecting the MicroPIC18F board to the PC using USB cable. Power ON the Board. Check for the USBtoSerial COMx allocated by the PC. Step5 : Using the PICLoader Software flash the hex file in the PIC18F4550. Step6 : Press reset button and execute the program. Result : Check if the buzzer is sounding ON/OFF and the ISR is getting executed with the specified timer delay. You can change the delay and vary the sounding rate. S.E (I.T) PIC18F Lab Manual Microembedded Technologies ™ , ©2021 Page 6 Program: #include <pic18f4550.h> /* Contains PIC18F4550 specifications */ #define Buzzer LATAbits.LATA5 /* Define buzzer pin */ unsigned int count = 0; void interrupt Timer1_ISR() { if(TMR1IF==1) { //1 ms delay time in timer TMR1L = 0x20; TMR1H = 0xD1; count ++; if (count >= 1000) //measure upto 1000 ms i.e. 1 seconds { Buzzer = ~Buzzer; /* Toggle buzzer pin */ count = 0; //reset count } TMR1IF = 0; //timer1 overflow flag to 0 } } void main() { TRISB=0; /* Set as output port */ TRISAbits.TRISA5 = 0; //set buzzer pin RA5 as output GIE=1; /* Enable Global Interrupt */ PEIE=1; /* Enable Peripheral Interrupt */ TMR1IE=1; /* Enable Timer1 Overflow Interrupt */ TMR1IF=0; /* Enable 16-bit TMR1 register,no pre-scale,internal clock, timer OFF */ T1CON=0x80; /*1:8 prescale*/ TMR1L = 0x20; TMR1H = 0xD1; TMR1ON=1; /* Turn ON Timer1 */ while(1); }