dolphinlabs
...
Doplhin labs
News

PIC18F4520-ADC Interfacing


Program Name: Interfacing analog voltage 0-5V to internal ADC and display value on LCD

Connections: RA0=Analog input, RC0=RS, RC1=RW, RC2=EN

                        PORTD=Data pins of LCD

Hardware connections for PIC184520 Board:

1.      Give supply to the Board with the help of adapter.

2.      Connect +5V pin of PIC controller to +5v supply and GND pin of controller to GND pin of supply.

3.      Connect +5V pin of ADC to +5V supply and GND pin of ADC to GND pin of supply.

4.      Connect analog input to ANALOG IN pin of ADC

5.      Connect data lines of LCD to PORT D of controller.

6.      Connect RS to RC0, RW to RC1 and EN to RC2 pin of controller.

 

Circuit Diagram:

 

 

Program Code:

#include<P18F4520.h>

#define output_port PORTD

#define rs PORTCbits.RC0

#define rw PORTCbits.RC1

#define en PORTCbits.RC2

void delay(void);

void lcddata(char);

void lcdcmd(char);

void convert_display(int);

void lcdstr(char[]);

int display,i=0,dig=0,a[5],L_Byte,H_Byte;

char s[]="ADC Value is";

void main()

{

 

                TRISC=0;                                                                             //make PORTC output port

                TRISD=0;                                                                             //make PORTD output port

                TRISAbits.TRISA0=0;                                                       //RA0=input for analog input

                ADCON0=0x81;                                                                  //Fosc/64,channel 0,A/D is on

                ADCON1=0xCE;                                                                 //right justified,Fosc/64

                  lcdcmd(0x38);

                delay();

                lcdcmd(0x0e);

                delay();

                lcdcmd(0x01);

                delay();

                lcdcmd(0x06);

                delay();

                lcdcmd(0x80);

                delay();

                lcdstr(s);

                delay();

                lcdcmd(0xc0);

                delay();

                while(1)

                {

                                delay();

                                ADCON0bits.GO=1;                                                           //start converting

                                while(ADCON0bits.DONE==1);

                                L_Byte=ADRESL;                                                              //display low bytes on PORTC

                                H_Byte=ADRESH;                                                             //display high bytes on PORTD

                                L_Byte>>=2;

                                L_Byte&=0x3F;

                                H_Byte<<=6;

                                H_Byte&=0xC0;

                                display=L_Byte|H_Byte;

                                convert_display(display);

                }

}

void delay()

{             

                int i,j;

                for(i=0;i<100;i++)

                for(j=0;j<100;j++);

}

void lcddata(char x)

{

                output_port=x;

                rs=1;

                rw=0;

                en=1;

                delay();

                en=0;

}

  void lcdcmd(char y)

{

                output_port=y;

                rs=0;

                rw=0;

                en=1;

                delay();

                en=0;

}

void convert_display(int d)

{

                while(d>0)

                {

                                dig=d%10;

                                dig=dig+48;

                                a[i]=dig;

                                i++;

                                d=d/10;

                }

                i--;

                for(;i>=0;i--)

                {

                lcddata(a[i]);

                delay();

                }

               

}

void lcdstr(char s[])

{

                int i;

                for(i=0;i<12;i++)

                lcddata(s[i]);

                delay();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Get SOCIAL







SAFE & SECURE SHOPPING

visa card master card maestro card
Maverick IT Industries Pvt. Ltd.