problem in driving the internal ADC of PIC18F4550

Status
Not open for further replies.

abhishekdixit

Full Member level 2
Joined
Dec 30, 2011
Messages
124
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
India
Visit site
Activity points
2,182
hello,
I am using PIC18F4550 micocontroller. i want to read the temprature from LM35 temprature sensor. for this i am using internal ADC of microcontroller & display this value on LCD. now i got success to enable the internal ADC of microcontroller. but its gives a wrong value on LCD.
please help me to remove this error.
my co& its proteus diagram shown below.
Code:
#include<htc.h>
#include<string.h>
#define _XTAL_FREQ 20000000


#define rs PORTAbits.RA1
#define en PORTAbits.RA2
#define lcdport LATB

void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void adc_con(unsigned int);
void adc_init();

unsigned char data[20]="ADC OUTPUT=";
unsigned int digital_out[10],avg_output=0,temp;
unsigned int i=0;


void main()
{
    TRISA=0x01;        // Configure RA0 as input pin
    LATA=0;
    TRISB=0;        // Configure Port B as output port
    LATB=0;
    TRISD=0;
    LATD=0;
	ADCON0 = 0x00;
	ADCON1 = 0b00001111;
	CMCON = 0x07;
    lcd_ini();               // LCD initialization
    while(data[i]!='\0')
    {
        lcddata(data[i]);      // Call lcddata function to send character one by from 'data' array
        i++;
    }
	
   adc_init();        //ADC Initialization

    while(1)
    {
        temp=0;
        for(i=0;i<10;i++)
        {
            /*ADCON0|=(1<<GO);                              // Start A/D conversion
            while(!(ADCON0 & (1<<GO)));*/                   // Wait until conversion gets over
			ADCON0=0x03;
			while(GO!=0);
			digital_out[i]=((ADRESL)|(ADRESH<<8));        // Store 10-bit output into a 16-bit variable
            __delay_ms(20);
			temp=temp+digital_out[i];
        }
        avg_output=temp/10;                           // Take average of ten digital values for stablity
        adc_con(avg_output);                          // Function to convert the decimal vaule to its corresponding ASCII

    }
}


void adc_init()
{
    ADCON1=0x0E;                            // Make RA0/AN0 pin as analog pin (Other pins remain to be digital I/O)
    ADCON0=0x00;                            // Select Channel0 & ADC off
    ADCON2=0x8A;                            // Left justified, 2TAD acquiciation time, Fosc/32 clock option
    ADCON0=0x01;                      // Enable ADC
}


void lcd_ini()
{
    lcdcmd(0x38);        // Configure the LCD in 8-bit mode, 2 line and 5x7 font
    lcdcmd(0x0C);        // Display On and Cursor Off
    lcdcmd(0x01);        // Clear display screen
    lcdcmd(0x06);        // Increment cursor
    lcdcmd(0x80);        // Set cursor position to 1st line, 1st column
}


void adc_con(unsigned int adc_out)
{
    unsigned int adc_out1;
    int i=0;
    char position=0xC1;

    for(i=0;i<=3;i++)
    {
        adc_out1=adc_out%10;                     // To exract the unit position digit
        adc_out=adc_out/10;
        lcdcmd(position);
        lcddata(48+adc_out1);                    // Convert into its corresponding ASCII
        position--;

    }
}


void lcdcmd(unsigned char cmdout)
{
    lcdport=cmdout;        //Send command to lcdport=PORTB
    rs=0;                        
   // rw=0;
    en=1;
    __delay_ms(10);
    en=0;
}


void lcddata(unsigned char dataout)
{
    lcdport=dataout;    //Send data to lcdport=PORTB
    rs=1;
    //rw=0;
    en=1;
    __delay_ms(10);
    en=0;
}

& its proteus diagram




with regards,
Abhishek Dixit
 

You are using Vref+ 5v and Vref- = 0v. So for 5v your raw adc value will be 1023. LM35 o/p voltage for 0 deg C will be 0v and for 150 deg C it will be 1.5v. So, for 0 deg C you should get 0 reading and for 150 deg C you should get 306.9 raw adc value. Try to use 1.8v as the vref+. Have you set the ADCON1 and ADCON2 registers properly? Tad should be according to your Fosc and RA0 pin should be analog input. Also use right jestified for ADRESx register. You have chosen left justified. Max value ADRESL = 11 and ADRESH = 11111111 and ADRES will be 1111111111000000 which is wrong. If you use right justified it will be 1111111111 = 1023
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…