Pages

Monday, 4 May 2015

Programming with PIC Microcontroller: Chapter-4

 Lab 4.Interfacing UART toTransmit and Receive the message

I/O connection:

TX and RX->UART RX and TX

Ground of Ic ->UART ground

#include<htc.h>

#define _XTAL_FREQ 20000000       //crystal frequency of 20MHZ
#include "uart.h"                                    //header file
#include "string.h"                                //header file

char val;

void main()

{
__delay_ms(1000);                        //provide delay for 1s

 UART_Init(9600);                          //calling initialization function with 9600 baud rate

 __delay_ms(1000);                     //provide delay for 1s

UART_Write_Text("RDL");          //Display RDL on hyper terminal

 do

{

 if(UART_Data_Ready())               //check whether it is ready to receive a data

{

 recieve = UART_Read();            //read a data and store in variable

 UART_Write(recieve);                //display on terminal
 UART_Write(10);                        //enter
UART_Write(13);                        //carriage return

__delay_ms(1000);                   //provide delay of 1s

 }while(1);

char UART_Init(const long int baudrate)

unsigned int x;

x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);

if(x>255)

{

x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);

BRGH = 1;                                          //High Baud Rate Select bit set to high
}

if(x<256)

{

 SPBRG = x;                                       //Writing SPBRG register
 SYNC = 0;                                        //Selecting Asynchronous Mode
 SPEN = 1;                                      //enables serial port
 TRISC7 = 1;
 TRISC6 = 1;
 CREN = 1;                                    //enables continuous reception 

 TXEN = 1;                                   //enables continuous transmission

 return 1;

}

 return 0;
}

char UART_TX_Empty()
{

 return TRMT;                               //Returns Transmit Shift Status bit    
}

char UART_Data_Ready()

{

 return RCIF;                                //Flag bit

}

 char UART_Read()                     //this function is used to read a byte
{

 while(!RCIF);                               //Waits for Reception to complete
 return RCREG;                          //Returns the 8 bit data

}

void UART_Read_Text(char *Output, unsigned int length)//this function is used to read a text

{

int i;

for(int i=0;i<length;i++)
Output[i] = UART_Read();

}

void UART_Write(char data)                 //this function is used to write a byte
{

 while(!TRMT); 

 TXREG = data;                                    //transmit register

}

void UART_Write_Text(char *text)          //this function is used to write a string

{

 int i;

 for(i=0;text[i]!='\0';i++) 
 UART_Write(text[i]);

}


Lab 5.Interfacing PWM to vary the brightness of LED

I/O connection:
PORT C1->LED1

PORTC2->LED2

#include<htc.h> 

#define XTAL 20000                    //20Mhz=20000Khz
#define PWM_Freq 1                   //1Khz PWM frequency
#define TMR2_PRE 16                //Timer2 Prescale
#define PR2_Val ((char)((XTAL/(4*TMR2_PRE*PWM_Freq))-1)) 
                                                    //Calculation for Period register PR2 (2Khz)
#define Duty_Cyc PR2_Val*2

unsigned int i;

void PWM_init(void);                            // This function is to initialize the PWM

void PWM_change(unsigned int);       //This function is to change theDuty cycle  routine
void DelayMs(unsigned int);                //this function is to provide a delay

void main(void)

{

 PWM_init();

 while(1)

 {

 i=0;

 PWM_change(i);
 DelayMs(10);

 while(i<PR2_Val)

 {

 i=i+1;

 PWM_change(i);
 DelayMs(200);

 }
}

}

void PWM_init(void)

{

 TRISC2=0;                                 //PWM channel 1 and 2 configured as output
 TRISC1=0;
 PORTC = 0x00;
 CCP1CON=0x0c;                   //CCP1 and CCP2 are configured for PWM
 CCP2CON=0x0c;
 PR2=PR2_Val;                       //Move the PR2 value

 T2CON=0x03;                       //Timer2 Prescale is 16
 TMR2=0x00;
 TMR2ON=1;                         //Turn ON timer2

}

void PWM_change(unsigned int DTY)       //Duty cycle change routine

{

 CCPR1L=DTY;                                     //Value is between 0 to 255
 CCPR2L=DTY;

}

void DelayMs(unsigned int Ms)             //Delay Routine

{

 int delay_cnst;
 while(Ms>0)

{

 Ms--;

for(delay_cnst = 0;delay_cnst <220;delay_cnst++); //delay constant for 1Ms @20Mhz

 }

}

 Lab 6. Interfacing KEYPAD to display value on LCD when a key is pressed

I/O connection:

 
PORT D0 to D7->DO to D7 of LCD
ENABLE->C0
R/W->GROUND
R/S->C1
R1,R2,R3,R4->PORT B0 to B3
C1,C2,C3,C4->PORT4 to B7

#include <htc.h>    
#include <stdio.h>                                                            // Define I/O functions
#define XTAL      20000000
#define BAUD_RATE  9.6                                              //9600 Baudrate
#define BAUD_VAL   (char)(XTAL/ (16 * BAUD_RATE )) - 1;  
                                                                              //Calculation For9600 Baudrate @20Mhz
 #define EN RC0
#define RS RC1
void ScanCol(void);                                                          //Column Scan Function
void ScanRow(void);                                                        //Row Scan Function
void DelayMs(unsigned int);
void LCD_Cmd(unsigned char);
void LCD_Init(void);
void LCD_Display( char *addr);
void LCD_SendDataByte(unsigned char);
   

unsigned char KeyArray[4][4]= {   '1','2','3','4',    
                                                    '5','6','7','8',
                                                    '9','A','B','C',
                                                   'D','E','F','0'};         
                                                                           //Keypad value Initialization Function

unsigned char Count[4][4]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int Col=0,Row=0,count=0,i,j;

void main()
{

    TRISD=0x00;                                                  //set registerD as output
    TRISC=0x00;                                                 //set register C as output
      LCD_Init();                                                   //initialize LCD

         DelayMs(1000);
          nRBPU=0;                                                                   //Enable PORTB Pullup values

   while(1)
   { 
      TRISB=0X0f;                                                       // Enable the 4 LSB as I/P & 4 MSB as
                                                                                                 O/P

       PORTB=0X00;
      while(PORTB==0x0f);                                             // Get the ROW value
      ScanRow();
       TRISB=0Xf0;                                                  // Enable the 4 LSB as O/P & 4 MSB as I/P
      PORTB=0X00;
      while(PORTB==0xf0);                                                // Get the Column value
      ScanCol();

      DelayMs(1000);                                                          //provide a delay of 1s
      Count[Row][Col]++;                                                    // Count the Pressed key
     LCD_Cmd(0X01);                                                     //clear the LCD
      LCD_Cmd(0X80);                                                    //1st row of the LCD
    LCD_SendDataByte(KeyArray[Row][Col]);           //send keypad value and display on LCD
 DelayMs(1000);                                                            //provide delay of 1s
   }

}

void ScanRow()                                                             // Row Scan Function

 
   switch(PORTB)
   {
      case 0x07:
      Row=3;                                                                     // 4th Row
      break;
      case 0x0b:
      Row=2;                                                                   // 3rd Row
      break;
      case 0x0d:
      Row=1;                                                                  // 2nd Row
      break;
      case 0x0e:
      Row=0;                                                                // 1st Row
      break;
   }
}

void ScanCol()                                                         // Column Scan Function

 
   switch(PORTB)
   {
      case 0x70:
         Col=3;                                                             // 4th Column
      break;
      case 0xb0:
         Col=2;                                                            // 3rd Column
      break;
      case 0xd0:
         Col=1;                                                            // 2nd Column
      break;
      case 0xe0:
         Col=0;                                                           // 1st Column
      break;
   }
}
/*LCD CODE*/

void LCD_Delay()                                              //delay routine
{
 __delay_ms(1);
}



void LCD_Cmd(unsigned char cmd)                 //this function is to write command to the LCD
{

   
    PORTB=cmd;
    RS=0;                                                          //Set RS pin to low in order to send a
                                                                   command to the LCD

    EN=1;                                              //set EN pin to high in order to send high pulse
    LCD_Delay();                               //give a small delay
    EN=0;                                           //set EN pin to low in order to make pulse low
         LCD_Delay();                       //give a small delay
}

void LCD_Init()                             //Initializing LCD
{
    unsigned char cmd[5]={0X38,0X06,0X0F,0X01,0X80},Count;
                   //0x38 represents 5x7 matrix ,0x06 represent entry mode,0x0f represent display on    cursor blinking,0x01 represents clearing the LCD,0x80 represents 1st row
    for(Count=0;Count<5;Count++)
    LCD_Cmd(cmd[Count]);
}

void LCD_SendDataByte(unsigned char data) //this function is to write a byte on LCD
{
    PORTB=data;
    RS=1;                                              //make RS pin high inorder to send a data
    EN=1;                                                //set enable pin to high in order to send high
                                                                 to low pulse

    LCD_Delay();                              //provide a small delay
    EN=0;   
          LCD_Delay();
}
void LCD_Display( char *addr)                        //this function is to display a string on LCD
{
    while(*addr)
    {
        LCD_SendDataByte(*addr);
        addr++;
    }
}




Lab7.  Interfacing 7segment 


I/O connection:

 
A,B,C,D,E,F,G,DP->B0 to B7
 DIG1,DIG2,DIG3,DIG4 ->A0 to A3

#include<htc.h> 
 #define CNTRL_PORT PORTA
#define DATA_PORT  PORTB

void hex2dec(unsigned char);                                  //function to convert hex value to decimal
void send_seg(unsigned char,unsigned char,unsigned char,unsigned char); //Function to display count on 7seg
void DelayMs(unsigned int);                                        //function to provide delay
unsigned char x;
unsigned char thou=0,hun=0,ten=0,single=0;
unsignedcharCA[10]   = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsignedchar CC[10]   =  {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char CA_CNTRL[4]    =    {0x07,0x0b,0x0d,0x0e};
unsigned char CC_CNTRL[4]    =    {0x08,0x04,0x02,0x01};
unsigned char n=1;
void main()
{
   unsigned char number;
   nRBPU =0;
   TRISB=0x00;                                                           //PORTB configured as O/P
   ADCON1=0x07;                                                    //Configure PORTA & PORTE as Digital
                                                                                   port

   TRISA=0x00;                                                        //PORTA Configured as O/P
   while(1)
   {
      if(x == 200) 
      {
                 x=0;
         single++;                                                           //Increment up to 9 in unit place
         if(single>9)
                 {
            single=0;
                    ten++;                                                    //Increment up to 9 in Tenth place           
            if(ten>9)
                    {
               ten=0;
                        hun++;                                              //Increment up to 9 in Hundredth place
                         if(hun>9)
               {
                          hun=0;
                          thou++;                                         //Increment up to 9 in Thousandth place
                  if(thou>9)
                            thou=0;
               }
            }
         }
      }
      x++;
      send_seg(thou,hun,ten,single);    
   }
}
void send_seg(unsigned char thou,unsigned char hun,unsigned char ten,unsigned char single)
{
   if(n==1)
   {
  CNTRL_PORT=CA_CNTRL[0];                      //Eanble Unit place 7-Segment
      DATA_PORT=CA[single];                            //Display Unit Place Number
      n=2;
      DelayMs(5);
   }

   else if(n==2)
       {
 CNTRL_PORT=CA_CNTRL[1];                       //Eanble Tenth place 7-Segment
 DATA_PORT=CA[ten];                                     //Display Tenth Place Number
          n=3;
               DelayMs(5);
       }
       else if(n==3)
           {
              CNTRL_PORT=CA_CNTRL[2];       //Enable Hundredth place 7-Segment
              DATA_PORT=CA[hun];                    //Display Hundredth Place Number
              n=4;
              DelayMs(5);
           }
           else if(n==4)
           {
             CNTRL_PORT=CA_CNTRL[3];      //Eanble Thousandth place 7-Segment
             DATA_PORT=CA[thou];                 //Display Thousandth Place Number
             n=1;
             DelayMs(5);
           }
}
void DelayMs(unsigned int Ms)
{
   int delay_cnst;
   while(Ms>0)
   {
 Ms--;
      for(delay_cnst = 0;delay_cnst <220;delay_cnst++);
   }
}

 

Lab 8. Interfacing GSM modem to send and receive the message



I/Oconnection:


Vin of GSM->12v

Ground of GSM->Ground

D0,D1 of GSM->TX,RX

#define <htc.h>
#define _XTAL_FREQ 20000000                      //crystal frequency of 20MHZ
#include "uart.h"                                                 //header file
#include "string.h"                                             //header file
                  
char UART_Init(const long int baudrate)
{
            unsigned int x;
            x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
            if(x>255)
            {
                 x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
                        BRGH = 1;                                    //High Baud Rate Select bit set to high
            }
            if(x<256)
            {
                 SPBRG = x;                                          //Writing SPBRG register
                 SYNC = 0;                                            //Selecting Asynchronous Mode
                 SPEN = 1;                                            //enables serial port
                 TRISC7 = 1;
                 TRISC6 = 1;
                 CREN = 1;                                          //enables continuous reception
                 TXEN = 1;                                         //enables continuous transmission
                  return 1;
            }
                  return 0;
}

char UART_TX_Empty()
{
  return TRMT;                                                    //Returns Transmit Shift Status bit
}

char UART_Data_Ready()
{
   return RCIF;                                                                //Flag bit                 
}

char UART_Read()                                          //this function is used to read a byte
{
  while(!RCIF);                                                //Waits for Reception to complete
  return RCREG;                                             //Returns the 8 bit data
}
void UART_Read_Text(char *Output, unsigned int length)
                                                                    //this function is used to read a text
{
            int i;
            for(int i=0;i<length;i++)
            Output[i] = UART_Read();
}

void UART_Write(char data)                         //this function is used to write a byte
{
  while(!TRMT);
  TXREG = data;                                            //transmit register
}

void UART_Write_Text(char *text)              //this function is used to write a string
{                                                                                                                                         
  int i;
  for(i=0;text[i]!='\0';i++)
  UART_Write(text[i]);
}
void main()
{
UART_Init(9600);                                         //initialize the UART function
__delay_ms(1000);                                        //provide the delay of 1s
           
while(1)                                                          //infinite loop
{
__delay_ms(1000);                                                            //provide a delay of 1s
UART_Write_Text("AT");                                                //attention command
UART_Write(13);                                                             //enter
UART_Write(10);                                                             //carriage return
__delay_ms(1000);                                                           //provide delay of 1s
UART_Write_Text("AT+CMGF=1");                             //initialize the modem

UART_Write(13);                                                             //enter
UART_Write(10);                                                            //carriage return
__delay_ms(1000);                                                         //provide delay of 1s
UART_Write_Text("AT+CMGS=\"1234567890\"");     //send a message

UART_Write(13);                                                           //enter
UART_Write(10);                                                          //carriage return
__delay_ms(1000);                                                        //provide delay of 1s
UART_Write_Text("GSM");                                       //display on hyper terminal
UART_Write(13);                                                        //enter
UART_Write(10);                                                        //carriage return
__delay_ms(1000);                                                      //provide delay of 1s
UART_Write(26);                                                        //Ctr +Z
}
}



 

Lab 9. Interfacing RELAY to turn the relays ON and OFF



I/O connection:



B0,B1,B2,B3 ->  to relay shield.

#define _XTAL_FREQ 20000000                       //crystal frequency of 20MHZ
#include "uart.h"                                                  //header file
#include "string.h"                                               //header file
#define relay1 RB1
#define relay2 RB2
#define relay3 RB3
#define relay4 RB4    
                                 
char UART_Init(const long int baudrate)
{
            unsigned int x;
            x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
            if(x>255)
            {
                        x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
                        BRGH = 1;                                    //High Baud Rate Select bit set to high
            }
            if(x<256)
            {
                 SPBRG = x;                                         //Writing SPBRG register
                 SYNC = 0;                                           //Selecting Asynchronous Mode
                 SPEN = 1;                                           //enables serial port
                 TRISC7 = 1;
                 TRISC6 = 1;
                 CREN = 1;                                         //enables continuous reception
                TXEN = 1;                                         //enables continuous transmission
                 return 1;
            }
              return 0;
}

char UART_TX_Empty()
{
  return TRMT;                                                                    //Returns Transmit Shift Status bit
}

char UART_Data_Ready()
{
   return RCIF;                                                                                  //Flag bit                       
}

char UART_Read()                                                           //this function is used to read a byte
{
  while(!RCIF);                                                                      //Waits for Reception to complete
  return RCREG;                                                                    //Returns the 8 bit data
}
void UART_Read_Text(char *Output, unsigned int length)//this function is used to read a text
{
            int i;
            for(int i=0;i<length;i++)
            Output[i] = UART_Read();
}

void UART_Write(char data)                                       //this function is used to write a  byte
{
  while(!TRMT);
  TXREG = data;                                                                   //transmit register
}

void UART_Write_Text(char *text)                   //this function is used to write a string
{
  int i;
  for(i=0;text[i]!='\0';i++)
  UART_Write(text[i]);
}

 
void main()
{
unsigned char ReceivChar;                
TRISB=0X00;                                                                //make register as the output
PORTB=0X00;                                                  //make the PORTB as the output port
UART_Init(9600);                                           //inititalise the UART
DelayMs(1000);                                                //provide delay of 1s

while(1)
{
if(UART_Data_Ready())                                 //check if the data is ready
 {    
       ReceivChar = UART_Read();                  //store the data in a variable
               UART_Write(ReceivChar);              //display on hyperterminal
      __delay_ms(1000);                                   //provide delay of 1s

            if(ReceivChar=='1')                             //check if the  received char is 1if 1
            {
             ReceivChar = UART_Read();           //store the data in a variable
               UART_Write(ReceivChar);             //display on hyperterminal
           

             if(ReceivChar=='N')                        //if received character is N
            relay1=1;                                           //turn ON the 1st relay
            else if(ReceivChar=='F')                   //if received character is F
            relay1=0;                                           //turn OFF the 1st relay
            }

            else if(ReceivChar=='2')                   //check if the  received char is 2if 2

            {
ReceivChar = UART_Read();                      //store the data in a variable
UART_Write(ReceivChar);                         //display on hyperterminal
            if(ReceivChar=='N')                        //if received character is N
            relay2=1;                                          //turn ON the 2nd relay
            else if(ReceivChar=='F')                  //if received character is F
            relay2=0;                                          //turn OFF the 2nd relay
            }

            else if(ReceivChar=='3')                 //check if the  received char is 3if 3
            {
            ReceivChar = UART_Read();         //store the data in a variable
               UART_Write(ReceivChar);         //display on hyperterminal
            if(ReceivChar=='N')                        //if received character is N
            relay3=1;                                          //turn ON the 3rd relay
            else if(ReceivChar=='F')                  //if received character is N        
            relay3=0;                                          //turn OFF the 3rd relay
            }
else if(ReceivChar=='4')                             //check if the  received char is 4if 4
            {
            ReceivChar = UART_Read();        //store the data in a variable
               UART_Write(ReceivChar);         //display on hyperterminal
            if(ReceivChar=='N')                           //if received character is N
            relay4=1;                                             //turn ON the 4th relay
            else if(ReceivChar=='F')                     //if received character is N     
            relay4=0;                                             //turn OFF the 4th relay
            }

 







 


Friday, 3 April 2015

Programming with PIC Microcontroller: Chapter-3

GETTING STARTED WITH EMBED C PROGRAMMING:


                             Lab 1 . LED Blinking using PIC controller (16F877A) with MPLAB



I/O Connections :

PORT B4->LED1
PORTB5->LED2
PORTB6->Switch1
PORTB7->Switch2


#include <htc.h>
#define _XTAL_FREQ 20000000       //crystal frequency of 20MHZ
#define Input1 RB7                               //set port RB7 as input port
#define Input2 RB1                             //set port RB1 as input port
#define Output1 RB4                         //set port RB4 as output port
#define Output2 RB5                        //set port RB5 as output port
void main()
{
TRISB=0X82;                                  //use portB register as input as well as output port
while(1)                                             //infinite loop
{
if(Input1==0)                                   //if switch1 is pressed ie connect port RB7 to sw1
{
Output1=1;                                     //blink both the LED’S
Output2=1;
}
else if(Input2==0)                          //If switch2 is pressed ie connect port RB1 to sw2
{
Output1=0;                                     //both the LED’S are turned off
Output2=0;
}
}
}


                                  Lab2.To display a message on LCD using pic controller



I/O connection:
 
PORT B0 tO B7->DO to D7 of LCD
ENABLE->D7
R/W->GROUND
R/S->D6


#include <htc.h>
#include<string.h>
#define _XTAL_FREQ 20000000                           //crystal frequency of 20MHZ
#define EN RD7                                                        //connect enable pin of LCD to port D7
#define RS RD6                                                       //connect Register select pin of LCD
to port D6
void LCD_Delay()                                                    //delay routine
{
__delay_ms(1);
}
void LCD_Cmd(unsigned char cmd)                 //this function is to write command to
                                                                             the LCD

{
PORTB=cmd;
RS=0;                                                                 //Set RS pin to low in order to send a
                                                                              command to the LCD

EN=1;                                                                 //set EN pin to high in order to send
                                                                              high pulse

LCD_Delay();                                                  //give a small delay
EN=0;                                                               //set EN pin to low in order to make
                                                                           pulse low

LCD_Delay();                                                 //give a small delay

}
void LCD_Init()                                               //Initializing LCD
{
unsigned char cmd[5]={0X38,0X06,0X0F,0X01,0X80},Count;
             //0x38 represents 5x7 matrix ,0x06 represent entry mode,0x0f represent display on     cursor blinking,0x01 represents clearing the LCD,0x80 represents 1st row
for(Count=0;Count<5;Count++)
LCD_Cmd(cmd[Count]);
}
void LCD_SendDataByte(unsigned char data)     //this function is to write a byte on LCD
{
PORTB=data;
RS=1;                                                                    //make RS pin high inorder to send a
                                                                                data

EN=1;                                                                    //set enable pin to high in order to
                                                                                send high to low pulse

LCD_Delay();                                                      //provide a small delay
EN=0;
LCD_Delay();
}
void LCD_Display( char *addr)                         //this function is to display a string on
                                                                            LCD

{
while(*addr)
{
LCD_SendDataByte(*addr);
addr++;
}

}
void main()
{
TRISB=0x00;                                                     //make the registerB as ouput
TRISD=0x00;                                                     //make the registerD as ouput
LCD_Init();                                                          //Initialize the LCD
__delay_ms(1000);
while(1)                                                                //infinite loop
{
LCD_Cmd(0X01);                                               //clearing the LCD
LCD_Cmd(0X84);                                              //1st row 4th position
LCD_Display("123");                                         //display 123 on LCD
LCD_Cmd(0xc0);                                              //2nd row
LCD_Display(" RDL");                                     //display RDL on LCD
__delay_ms(1000);                                         //delay by 1s
LCD_Cmd(0x01);                                           //clear the LCD
LCD_Cmd(0x80);                                           //1st row
LCD_Display(" LCD");                                   //display LCD
LCD_Cmd(0xc0);                                           //2nd row
LCD_Display(" Display");                              //display on LCD
__delay_ms(1000);                                        //delay by 1s

}

 Lab3.Interfacing ADC to display analog to digital conversion values on LCD.

I/O connection:
 
PORT B0 to B7->DO to D7 of LCD
ENABLE->D7
R/W->GROUND
R/S->D6
A0->PORTC0



#include <htc.h>
#include<string.h>
#define _XTAL_FREQ 20000000                            //crystal frequency of 20MHZ
#define EN RD7                                                         //connect enable pin of LCD to port D7
#define RS RD6                                                       //connect Register select pin of LCD                                                                                           to port  D6
/*LCD code */
void LCD_Delay()                                                    //delay routine
{
__delay_ms(1);
}
void LCD_Cmd(unsigned char cmd)               //this function is to write command to the LCD
{
PORTB=cmd;
RS=0;                                          //Set RS pin to low in order to send a command to the LCD
EN=1;                                        //set EN pin to high in order to send high pulse
LCD_Delay();                        //give a small delay
EN=0;                                    //set EN pin to low in order to make pulse low

LCD_Delay();                      //give a small delay
}
void LCD_Init()                   //Initializing LCD
{
               unsigned char cmd[5]={0X38,0X06,0X0F,0X01,0X80},Count;
                                //0x38 represents 5x7 matrix ,0x06 represent entry mode,0x0f represent display on cursor blinking,0x01 represents clearing the LCD,0x80 represents 1st row
for(Count=0;Count<5;Count++)
LCD_Cmd(cmd[Count]);
}
void LCD_SendDataByte(unsigned char data)   //this function is to write a byte on LCD
{
PORTB=data;
RS=1;                                                     //make RS pin high inorder to send a data
EN=1;                                                //set enable pin to high in order to send high to low pulse
LCD_Delay();                                    //provide a small delay
EN=0;
LCD_Delay();
}
void LCD_Display( char *addr)    //this function is to display a string on LCD
{
while(*addr)
{
LCD_SendDataByte(*addr);
addr++;
}
}
void ADC_Init()
{
ADCON0 = 0x41;               //set A/D control register0 to 0x41
ADCON1 = 0xC0;              //set A/D control register1 0xc0 

}
unsigned int ADC_Read(unsigned char channel)
{
if(channel > 7)
return 0;
ADCON0 &= 0xC5;
ADCON0 |= channel<<3;
__delay_ms(2);
GO_nDONE = 1;
while(GO_nDONE);
return ((ADRESH<<8)+ADRESL);                   //left shift the higherorder bits and add the                                                                                  lower order bits
}
void display(unsigned int number)            //this function is for (0-1024)A/D conversion
{
unsigned char digit1,digit2,digit3,digit4,digit[4];
unsigned char x;
unsigned char temp;
digit1 = number / 1000u ;                                       // extract thousands digit
digit2 = (number / 100u) % 10u;                           // extract hundreds digit
digit3 = (number / 10u) % 10u;                            // extract tens digit
digit4 = nu
mber % 10u;                                                       // extract ones digit
digit[3]=digit4;
digit[2]=digit3;
digit[1]=digit2;
digit[0]=digit1;
for(x=0;x<4;x++)                                          //loop for upto 4 digits
{
temp=digit[x]|0x30;                                   //convert to ACII
LCD_SendDataByte(temp);                     //display the value on LCD
}
}
void main()
{
unsigned int value;
unsigned int a;
TRISB = 0x00;                                       //Set registerB as output
TRISC = 0x00;                                      //Set registerC as output
TRISD=0x00;                                       //set registerD as output
LCD_Init();                                          //initialize the LCD
__delay_ms(1000);                            //provide delay for 1s
ADC_Init();                                        //ADC initialisation
do
{
a = ADC_Read(0);                            //read port (A0)
__delay_ms(2000);                          //provide delay for 2s
LCD_Cmd(0x80);                            //1st row
LCD_ display(a);                             //display the value on LCD
__delay_ms(1000);                         //provide delay
} while(1);
}