LM35 interfaced with PIC16F877A: Weird Problem.

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

LM35 interfaced with PIC16F877A: Weird Problem.

Postby desmond1310 » Thu Apr 07, 2011 8:08 pm

Hi professionals of Cytron, I need some help.

I am using LM35 + PIC16F877A. My feature is to turn off my relay (linked to RC0) whenever the temperature hits 85'C.
With reference to the source codes for PR11/Temperature Control System using LM35, its subroutine ADC code which is stated as below is the same as the one I am currently using.
In their code, the value of 400 correlates to 40'C, and 350 correlates to 35'C, which is also stated below.

CODE: SELECT_ALL_CODE
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
   {
      ADGO = 1;               //ADGO is the bit 2 of the ADCON0 register
      while(ADGO==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;
}


CODE: SELECT_ALL_CODE
if((tempA>400)&&(tempB<350))      //   *****************************************
            {                        //   *   LED A and Fan A activated only for    *
               ledA=1;                  //   *   temperature A greater than 40'C      *
               ledB=0;                  //   *   and temperature B less than 35'C   *
               fanA=1;                  //   *****************************************
               fanB=0;
               buzzer=0;
            }


In comparison to PR11's ADCON0 and ADCON1 with mine,

PR11
ADCON0
CHANNEL0 0b10000001 // AN0
CHANNEL1 0b10001001 // AN1
ADCON1
0b11000101

Mine
ADCON0
CHANNEL2 0b10010001 //AN2
ADCON1
0b11000011

As far as i have listed, I am using Fosc/64 and right justified, which is the same as PR11.
Now, the problem is when I insert my limit as 850 or as known as 85'C, it NEVER triggers my relay, which meant my limit was too high.
After hours of troubleshooting, i found that by setting the value: 200 as my limit, it TRIGGERS my relay to off at precisely 85'C!
Why is 200 correlating to 85'C and not 850?
Can anyone find out my mistake? I'm still new to the ADC settings, ADCON1, ADCON0 settings and I believe my problem lies there.
Here's my piece of code that sets the limit:
CODE: SELECT_ALL_CODE
   ADCON0 = CHANNEL2;
   read_adc();
   temp=read_temp();
   tempA=temp;
   if(tempA>200)
   {
      RC0 = 0;
   }
desmond1310
Apprentice
 
Posts: 33
Joined: Thu Oct 21, 2010 12:50 am

Re: LM35 interfaced with PIC16F877A: Weird Problem.

Postby ober » Thu Apr 07, 2011 10:46 pm

Can you show us the picture of your hardware or circuit?
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: LM35 interfaced with PIC16F877A: Weird Problem.

Postby Brian Griffin » Fri Apr 08, 2011 12:54 pm

Check the voltage reference. If you can set it to 2.56V, it's easier to do some calculations and you can avoid much floating-point operations. (decimal points)

You can use the 10-bit A/D result, multiply it by 2.56V/1024, and then multiply again by 1000 to get back the desired value. But that's tedious because 8-bit microcon has some difficulties handling those floating point stuff. I could propose another example here which doesn't use floating points.

Example:

Obtained A/D result = 104

Then you can multiply the result by 2.5. (2.5m * 1000 = 2.5)

Wait - this is floating point. No worries - you can use this trick too. You don't even have to multiply at all:

(104 << 1) + (104 >> 1) -> 208 + 52 = 260

So 260 = 26 degrees Celcius.

Please try that on other A/D values and confirm it.

:D
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 4 guests

cron