Practicals









 /* 

 * File:   exp1.c
 * Author: gaurav rathod
 *
 * Created on 19 February, 2025, 12:06 AM
 */


                           //Addition - 1

//#include<stdio.h>
//#include<stdlib.h>
//#include<pic18F4550.h>
//void main(void)
//{
//int i, j, x;
//TRISB=0;
//LATB=0;
//i=0x04;
//j=0x05;
//x=i+j;
//PORTB=x;
//PORTC=i;
//PORTD=j;
//}










/*
 * File:   exp2.c
 * Author:gaurav rathod
 *
 * Created on 19 February, 2025, 12:06 AM
 */



                         //Total sum- 2

//#include <stdio.h>
//#include <stdlib.h>
//#include <pic18f4550.h>
//void main(void) {
//    int i,sum,n;
//int number[] = {1,2,3,4,5,6,7,8,9,10}; // array of 10 numbers
//sum = 0; // initialize sum as zero
//for(i=0; i<=9;i++){ //indexing start from 0 to 9
//    sum = sum+number[i];
//}
//TRISB =0; //initialize Port_B as output
//PORTB = sum; // from sum to PORT_B
////n = 0xFF + 0XFF;
//}
//






/*
 * File:   exp3,1.c
 * Author: gaurav rathod
 *
 * Created on 19 February, 2025, 12:06 AM
 */


                           //Array transform (3,1)

//#include <stdio.h>
//#include <stdlib.h>
//#include <pic18f4550.h>
//void main(void) {
//int temp,i;
//int source1[] = {0x21,0x22,0x23,0x24,0x25}; // source mem block
//int dest[] = {0x00,0x00,0x00,0x00,0x00}; // destination mem block
//for(i=0; i<=4;i++){ // counter = 5
//dest[i] = source1[i]; // source to destination
//}
//}



 









/*
 * File:   exp3,2.c
 * Author:gaurav rathod
 *
 * Created on 19 February, 2025, 12:06 AM
 */

                    //Array exchange (3,2)
/*
                    #include <stdio.h>
                    #include <stdlib.h>
                    #include <pic18f4550.h>
                    void main(void) {
                    int temp,i;
                    int source1[] = {0x21,0x22,0x23,0x24,0x25};
                    int dest[] = {0x91,0x92,0x93,0x94,0x95};
                    
                    for(i=0; i<=4;i++)
                    { temp = source1[i];
                    source1[i] = dest[i];
                    dest[i] = temp;
                    }
                    }
                    
                    
                    */
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    /*
                     * File:   exp4,1.c
                     * Author:gaurav rathod
                     *
                     * Created on 18 February, 2025, 12:06 AM
                     */
                    
                    
                    
                                      //Multiplaction (4,1)
                    
                    //#include <stdio.h>
                    //#include <stdlib.h>
                    //#include <pic18f4550.h>
                    //void main(void) {
                    //int num1, num2;
                    //int result,i;
                    //result = 0;
                    //num1 = 0x5;
                    //num2 = 0x5;
                    //for(i=1; i<=num2; i++)
                    //{
                    //result = result + num1;
                    //}
                    //TRISB =0;
                    //PORTB = result;
                    //}
                    
                    
                    
                    
                    
                    
                    /*
                     * File:   exp4,2.c
                     * Author: gaurav rathod
                     *
                     * Created on 19 February, 2025, 12:06 AM
                     */
                    
                    
                                          //Division(4,2)
                    
                    //#include <stdio.h>
                    //#include <stdlib.h>
                    //#include <pic18f4550.h>
                    //#include <stdio.h>
                    //
                    //int main() {
                    //    int dividend=20, divisor=2, quotient = 0, remainder;
                    //
                    //    while (dividend >= divisor) {
                    //         dividend -= divisor;
                    //        quotient++;
                    //    }
                    //    remainder = dividend;
                    //    TRISB = 0;
                    //PORTB = quotient;
                    //PORTC = remainder;
                    //    return 0;
                    //}
                    
                    
                    
                    
                    
                    
                    
                    /*
                     * File:   exp5,1.c
                     * Author:gaurav rathod
                     *
                     * Created on 18 February, 2025, 12:06 AM
                     */
                    
                    
                    
                    
                                            //ascending order (5,1)
                    
                    
                    //#include <stdio.h>
                    //#include <stdlib.h>
                    //#include <pic18f4550.h>
                    //void main(void) {
                    //int i,j,temp;
                    //int num_asc[] = {10,2,5,1,6};
                    //for(i=0; i<=4; i++){ // point to LHS number
                    //    for(j=i+1;j<=4;j++) // point to RHS number
                    //if (num_asc[i] > num_asc[j]){ // if LHS > RHS , change the position
                    //    temp = num_asc[i];
                    //num_asc[i] =num_asc[j];
                    //num_asc[j]= temp;
                    //}
                    //}
                    //}
                    
                    
                    
                    
                    
                    
                    /*
                     * File:   exp5,2.c
                     * Author:gaurav rathod
                     *
                     * Created on 20 February, 2025, 10:36 AM
                     */
                    
                    
                                                            //desending ordere (5,2)
                    //#include <stdio.h>
                    //#include <stdlib.h>
                    //#include <pic18f4550.h>
                    //void main(void) {
                    //    int i,j,temp;
                    //int num_desc[]= {10,2,5,1,6};
                    //for(i=0; i<=4; i++){ // point to LHS number
                    //   for(j=i+1;j<=4;j++) // point to RHS number
                    //if (num_desc[i] < num_desc[j]){ // if LHS > RHS , change the position
                    //    temp = num_desc[i];
                    //num_desc[i] =num_desc[j];
                    //num_desc[j]= temp;
                    //}
                    //}
                    //}

//LED BLINK

/*
#include <p18f4550.h>

void msdelay(unsigned int time)
{
    unsigned int i, j;
    for (i = 0; i < time; i++)
        for (j = 0; j < 165; j++);
}

void main(void) {
    TRISD = 0;
    LATD = 0;
    while (1) {
        LATD = ~LATD;
        msdelay(2000);
    }
}
    */
/*
//LED INTERFACING

#include <p18f4550.h> //Include Controller specific .h
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming

//Declarations
#define LCD_DATA PORTD
#define ctrl PORTE
#define rs PORTEbits.RE0
#define rw PORTEbits.RE1
#define en PORTEbits.RE2

//Function Prototypes
void init_LCD(void);
void LCD_command(unsigned char cmd);
void LCD_data(unsigned char data);
void LCD_write_string(char *str);
void msdelay(unsigned int time);

void main(void)
{
    char var1[] = " Hello !!!! ";

    ADCON1 = 0x0F; //Configuring the PORTE pins as digital I/O
    TRISD = 0x00; //Configuring PORTD as output
    TRISE = 0x00; //Configuring PORTE as output
    init_LCD();
    msdelay(50);
    LCD_write_string(var1);
}

void msdelay(unsigned int time)
{
    unsigned int i, j;
    for (i = 0; i < time; i++)
            for (j = 0; j < 710; j++); // Calibrated for 1ms delay
}

void init_LCD(void)
{
    LCD_command(0x38); // 16x2 LCD, 8-bit mode
    msdelay(15);
    LCD_command(0x01); // Clear LCD
    msdelay(15);
    LCD_command(0x0C); // Cursor off
    msdelay(15);
    LCD_command(0x80); // Set cursor to first position
    msdelay(15);
}

void LCD_command(unsigned char cmd)
{
    LCD_DATA = cmd;
    rs = 0;
    rw = 0;
    en = 1;
    msdelay(15);
    en = 0;
}

void LCD_data(unsigned char data)
{
    LCD_DATA = data;
    rs = 1;
    rw = 0;
    en = 1;
    msdelay(15);
    en = 0;
}

void LCD_write_string(char *str)
{
    while (*str)
    {
        LCD_data(*str++); // Send data to LCD byte by byte
        msdelay(15);
    }
}

*/

//PRACT8Relay
/*
#include <p18f4550.h>
void delay(unsigned int);  // Function prototype

void main() {
    TRISD = 0; // Set RD0 as output
    while(1) {
        LATD=1;// Turn Relay ON
        delay(2000); // Wait 2 seconds
        LATD=0; // Turn Relay OFF
        delay(2000); // Wait 2 seconds
    }
}


void delay(unsigned int cnt) {
    for(unsigned int i = 0; i < cnt; i++)
        for(unsigned int j = 0; j < 165; j++);
}
*/

/*
#include <p18f4550.h> //Include Controller specific .h
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs
//Declarations for LCD Connection
#define LCD_DATA PORTD //LCD data port
#define en PORTEbits.RE2 // enable signal
#define rw PORTEbits.RE1 // read/write signal
#define rs PORTEbits.RE0 // register select signal
//Function Prototypes
void ADC_Init(void); //Function to initialize the ADC
unsigned int Get_ADC_Result(void); //Function to Get ADC result
void Start_Conversion(void); //Function to Start of Conversion
void msdelay (unsigned int time); //Function to generate delay void
void init_LCD(void); //Function to initialize the LCD
void LCD_command(unsigned char cmd); //Function to pass command to LCD void
void LCD_data(unsigned char data);//Function to write character to LCD
void LCD_write_string( char *str); //Function to write string

int temperature;
//Start of main program
void main()
{
    char msg1[] = "AT lonavla";
    char msg2[] = "Temp :";
    unsigned char i, Thousands,Hundreds,Tens,Ones;
    unsigned int adc_val;
    ADCON1 = 0x0F; //Configuring the PORT pins as digital I/O
    TRISD = 0x00; //Configuring PORTD as output
    TRISE = 0x00; //Configuring PORTE as output
    ADC_Init(); // Init ADC peripheral
    init_LCD(); // Init LCD Module
    LCD_write_string(msg1); // Display Welcome Message
    LCD_command(0xC0); // Goto second line, 0th place of LCD
    LCD_write_string(msg2); // Display Message "ADC O/P"
    while(1)
    {
        Start_Conversion(); //Trigger conversion
        adc_val= Get_ADC_Result(); //Get the ADC output by polling GO bit
        LCD_command (0xC8); //Goto 9th place on second line of LCD
        i = adc_val/1000 ; //Get the thousands place
        Thousands = i + 0x30; // Convert it to ASCII
        LCD_data (Thousands); // Display thousands place
        i = (adc_val%1000)/100; //Get the Hundreds place
        Hundreds = i + 0x30; // Convert it to ASCII
        LCD_data (Hundreds); //Display Hundreds place
        i = ((adc_val%1000)%100)/10; //Get the Tens place
        Tens = i + 0x30; // Convert it to ASCII
        LCD_data (Tens); //Display Tens place
        i = adc_val%10 ; //Get the Ones place
        Ones = i + 0x30; // Convert it to ASCII
        LCD_data (i + 0x30); //Display Ones place
        msdelay(300); //Delay between conversions.
    }
}
//Function Definitions
void ADC_Init()
{
    ADCON0=0b00000100; //A/D Module is OFF and Channel 1 is selected
    ADCON1=0b00001110; // Reference as VDD & VSS, AN0 set as
    ADCON2=0b10001110; // Result is right Justified
    //Acquisition Time 2TAD
    //ADC Clk FOSC/64
    ADCON0bits.ADON=1; //Turn ON ADC module
}
void Start_Conversion()
{
    ADCON0bits.GO=1;
}
//Poll till the conversion complete
unsigned int Get_ADC_Result()
{
    unsigned int ADC_Result=0;
    while(ADCON0bits.GO);
    ADC_Result=ADRESL;
    ADC_Result|=((unsigned int)ADRESH) << 8;
    temperature = (ADC_Result * 500 )/ 1023;
    return temperature;
}
void msdelay (unsigned int time) //Function to generate delay
{
    unsigned int i, j;
    for (i = 0; i < time; i++)
    for (j = 0; j < 710; j++);//Calibrated for a 1 ms delay in MPLAB
}
void init_LCD(void) // Function to initialise the LCD
{
    LCD_command(0x38); // initialization of 16X2 LCD in 8bit mode
    msdelay(15);
    LCD_command(0x01); // clear LCD
    msdelay(15);
    LCD_command(0x0C); // cursor off
    msdelay(15);
    LCD_command(0x80); // go to first line and 0th position
    msdelay(15);
}
void LCD_command(unsigned char cmd) //Function to pass command to the LCD
{
    LCD_DATA = cmd; //Send data on LCD data bus
    rs = 0; //RS = 0 since command to LCD
    rw = 0; //RW = 0 since writing to LCD
    en = 1; //Generate High to low pulse on EN
    msdelay(15);
    en = 0;
}
void LCD_data(unsigned char data)//Function to write data to the LCD
{
    LCD_DATA = data; //Send data on LCD data bus
    rs = 1; //RS = 1 since data to LCD
    rw = 0; //RW = 0 since writing to LCD
    en = 1; //Generate High to low pulse on EN
    msdelay(15);
    en = 0;
}
//Function to write string to LCD
void LCD_write_string( char *str)
{
    while (*str)
    {
        LCD_data(*str++); // Send data to LCD byte by byte
        msdelay(15);
    }
}
*/
Previous Post Next Post

Contact Form