[SOLVED] Interfacing of LM35 with PIC18F4550. Need Help.

Status
Not open for further replies.

SAMO6

Newbie level 5
Joined
Nov 14, 2016
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
139
Plz, Help me. Not able to get correct values on LCD.
I am interfacing LM35 temperature sensor with PIC18F4550 with 20Mhz frequency.
I tried a lot. But, not getting right output.
Below is my code:

Code:
#include<P18F4550.h>
#define RS PORTCbits.RC0
#define En PORTCbits.RC1

void delay()
{
	unsigned int q;
	for(q=0;q<10000;q++);
}

void lcdcmd(unsigned char value)
{
    PORTB=value;
    RS=0;			//Select command register
    En=1;			// High to Low pulse on Enable pin of LCD
    delay();
    En=0;
}
void lcddata(unsigned char value)
{
    PORTB=value;
    RS=1;			//Select data register
    En=1;			// High to Low pulse on Enable pin of LCD
    delay();
    En=0;

}


void lcdinit()
{
     lcdcmd(0x38);	//Call Command subroutine (value=0x38)
    delay();
    lcdcmd(0x0E);
    delay();
    lcdcmd(0x01);
    delay();
    lcdcmd(0x06);
    delay();

}

void adcinit(){
    TRISAbits.RA0=1;
    ADCON1 = 0x01;       //Ref voltages Vdd & Vss; AN0 - AN7 channels Analog
    ADCON2 = 0x8E; // select result format, acquistion time,clock slect bits
    ADCON2bits.ADFM=1;
}

 unsigned int ADCRead( unsigned char ch){

 ADCON0 = 0x00;//configuring analog channel
 ADCON0 = (ch<<2); //selecting analog channel
 ADCON0bits.ADON = 1; //switch on adc module
 ADCON0bits.GO_DONE =1;//Start conversion

 while(ADCON0bits.GO_DONE); //wait for the conversion to finish
 ADCON0bits.ADON=0; //switch off adc

 return ADRESL;
 }


void stringlcd(const char * s)

{
            while(*s)
            lcddata(*s++);
}
unsigned int deci2ascii(unsigned int mb){
    unsigned int ans;
    ans= 0x30 | mb;
    return(ans);
    
}
void main()
{
  
    TRISAbits.RA0=1;
  
    TRISB = 0x00;                           // Configure LED pins (PORT B) as output
    TRISCbits.TRISC0 = 0;
    TRISCbits.TRISC1 = 0;
    lcdinit();
    lcdcmd(0x80);

           unsigned char dt[]="Temperature:";
    stringlcd(dt);
  
    unsigned int val;
    unsigned int ans,n;
 
    while(1){
        lcdcmd(0xC0);

          delay();
          val=ADCRead(0);
          ans= (ADRESL*500)/1023;
       n=deci2ascii(ans);
       
   lcddata(n);
    lcddata(223);
    lcddata('C');
    }
     }


Thanks.
Plz, help me.
 

Try this.

Code:
n=deci2ascii(ans/100);
lcddata(n);
n=deci2ascii((ans/10)%10);
lcddata(n);
n=deci2ascii(ans%10);
lcddata(n);

Which Compiler ? C18 ? with MPLAB or MPLAB X ?
 

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…