Problems with LM35

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Problems with LM35

Postby Happy3 » Mon Jul 04, 2011 9:43 pm

Hi, I'm facing problem in my project regarding the PR11 temperature monitoring parts. I connect my LM35 to the PIC(16f877a). Unfortunately, the LCD can't show the correct room temperature. May I know which part of my coding having error? Or what coding i actually miss out ? Thanks for helping me ;)


CODE: SELECT_ALL_CODE
#include<pic.h>

//===============configuration=================================
__CONFIG (0x3F32);

//===============define IO port================================
#define      lcd         PORTB
#define      RS          RA2
#define      E           RA5
#define      CHANNEL0    0b10000001   // AN0
#define      buzzer_led  RD0
#define      fan         RD1

//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);

//====================MAIN================================
unsigned short result;
unsigned short temp,tempA;

void main(void)
{
   ADRESH=0;            //clear A/D result
   ADRESL=0;            //clear A/D result

   //setting ADCON1 Register
   ADCON1=0b11000101;         // A/D result right justified,             
                             // configure RA2 and RA5 as digital I/O

   TRISA=0b11011011;         //configure PORTA I/O direction
   TRISB=0b00000000;         //configure PORTB as output
   TRISD=0b00000000;         //configure PORTD as output

   PORTA=0;
   PORTB=0;

   
   while(1)               
   {
      send_config(0b00000001);      //clear display at lcd
      send_config(0b00000010);      //Lcd Return to home
      send_config(0b00000110);      //entry mode-cursor increase 1
      send_config(0b00001100);      //diplay on, cursor off and cursor blink off
      send_config(0b00111000);      //function set
     
      lcd_goto(0);                  //cursor start from beginning
     
      //display character on LCD
      send_char(' ');
      send_char('T');
      send_char('E');
      send_char('M');
      send_char('P');
      send_char('.');
      send_char('A');
      send_char('=');

      while(1)                  //infinity loop
      {
      //Temperature sensor
      ADCON0=CHANNEL0;         //CHANNEL0=0b10000001           
      lcd_goto(8);
           
      read_adc();

      temp=read_temp();
      dis_num(temp/10);
     send_char('.');
      dis_num(temp%10);
      send_char(0b11011111);
      send_char('C');
      send_char(' ');
      send_char(' ');
     
      tempA=temp;
     

           if(tempA>300)                   
            {                       
               buzzer_led=1;                 
               fan=1;                     
                                       
            }   

         else if(tempA<250)                         
            {                       
               buzzer_led=1;                 
               fan=0;                     
                                   
            }   
         
         else
            {
               buzzer_led=0;
              fan=0;
            }     
      delay(2000);
   
      }
   
   }
     
}
     
               

//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
    RS=0;
   lcd=data;
   delay(500);
   e_pulse();
}

void e_pulse(void)
{
   E=1;
   delay(500);
   E=0;
   delay(500);
}

void send_char(unsigned char data)
{
    RS=1;
   lcd=data;
   delay(500);
   e_pulse();
}


void lcd_goto(unsigned char data)
{
    if(data<16)
   {
       send_config(0x80+data);
   }
   else
   {
       data=data-20;
      send_config(0xc0+data);
   }
}


void lcd_clr(void)
{
   RS=0;
    send_config(0x01);
   delay(600);   
}


void dis_num(unsigned long data)
{
   unsigned char hundred_thousand;
   unsigned char ten_thousand;
   unsigned char thousand;
   unsigned char hundred;
   unsigned char tenth;

   hundred_thousand = data/100000;               
   data = data % 100000;
   ten_thousand = data/10000;
   data = data % 10000;
   thousand = data / 1000;
   data = data % 1000;
   hundred = data / 100;
   data = data % 100;
   tenth = data / 10;
   data = data % 10;

   if(hundred_thousand>0)
   {
      send_char(hundred_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(ten_thousand + 0x30);
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }

   else if(ten_thousand>0)
   {
      send_char(ten_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(thousand>0)
   {
       send_char(thousand + 0x30);      //0x30 added to become ASCII code
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(hundred>0)
   {
       send_char(hundred + 0x30);      //0x30 added to become ASCII code
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(tenth>0)
    {
      send_char(tenth + 0x30);      //0x30 added to become ASCII code
      send_char(data + 0x30);
   }
   else send_char(data + 0x30);         //0x30 added to become ASCII code
}

void increment(unsigned long data)
{   
   unsigned short j;
   for(j=10;j>0;j--)
   {   lcd_goto(32);
      data=data+1;
      dis_num(data);
      delay(10000);
   }

}

//==================subroutine ADC=========================

void read_adc(void)
{
   unsigned short i;
   unsigned long result_temp=0;
   for(i=2000;i>0;i-=1)            //looping 2000 times for getting average value
   {
       ADCON0bits.GO = 1;
   //ADGO is the bit 2 of the ADCON0 register
   while(ADCON0bits.GO==1);
   //ADC start, ADGO=0 after finish ADC progress
   result=ADRESH;
   result=result<<8;
   //shift to left for 8 bit
   result=result|ADRESL;
   //10 bit result from ADC
   result_temp+=result;
   }
   result = result_temp/2000;         //getting average value

}

unsigned short read_temp(void)
{
    unsigned short temp;
   temp=result;
   return temp;

}

//==================subroutine DELAY==========================
void delay(unsigned short i)
{   
   for(;i>0;i--);
}
Happy3
Greenhorn
 
Posts: 2
Joined: Mon Jul 04, 2011 9:35 pm

Re: Problems with LM35

Postby shahrul » Mon Jul 04, 2011 10:00 pm

What is it display and what is your observation?
I did't check your code.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Problems with LM35

Postby Happy3 » Mon Jul 04, 2011 10:05 pm

after i made some changes on the coding ,
unsigned short read_temp(void)
{
unsigned short temp;
temp=result*5/1024;
return temp;

}

the LCD display o.1 degree celsius
Happy3
Greenhorn
 
Posts: 2
Joined: Mon Jul 04, 2011 9:35 pm

Re: Problems with LM35

Postby hyng » Tue Jul 05, 2011 9:58 am

May i know what is the problem? What is show in the LCD? only 0.1 degree everytime?
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Problems with LM35

Postby robosang » Tue Jul 05, 2011 10:41 pm

Not everyone have the hardware to verify your code, and I am sure no one like to check code 8-)

So please elaborate your problem clearly so other can help you.

Please list out what have you done, from original code to the modification you have made.
Also what is the result/output after you load the modified code.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Problems with LM35

Postby Brian Griffin » Tue Jul 05, 2011 11:45 pm

We need to see the circuit diagram and the offensive code bolded. :)

Do you have a voltage reference? Or is it at the default +5V?
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 6 guests

cron