CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

lm35 temperature sensor
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
evaang2003



Joined: 10 Jun 2009
Posts: 14

View user's profile Send private message Send e-mail

lm35 temperature sensor
PostPosted: Mon Jun 22, 2009 3:54 am     Reply with quote

Hi, I now trying to use pic16f877a and lm35 to read temperature.
Below is the code written:
Code:

void main(void)
{
       int16 temp_adc;
       float temp;
       
        setup_adc(ADC_CLOCK_DIV_8);
        setup_adc_ports(PIN_A1);
        set_adc_channel(1); //read analog input from channel 1
       
       
       
        lcd_init();
   
         printf ( LCD_PutChar, "Temperature is" );
         
          while(1)
            {
            temp_adc = read_adc();
           
            temp = 5.00*temp_adc*100.00/1023.00;
            lcd_display_char(2, 0, ' ');
            printf ( LCD_PutChar, "%f%cC  ", (float)temp, DEGREE_SYM);
            delay_ms(3000);
        }

}

However, it always give me 5.00 degree celcius reading even the actual temperature is around 24 degree.
Can anyone tell me why?
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Mon Jun 22, 2009 4:03 am     Reply with quote

Paste the complete code.
Did you measure the voltage on output pin of LM35?
aasief



Joined: 12 Feb 2009
Posts: 12
Location: cape town

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 22, 2009 4:09 am     Reply with quote

Show me the LM35 circuit and what is your voltage coming out of your LM35 circuit into the pic
Ttelmah
Guest







PostPosted: Mon Jun 22, 2009 4:37 am     Reply with quote

The 877a, does not support setting just pin A1, as an analog input. The only 'single pin', that can be set to analog on it's own, is AN0 Look in the 16F877A.h include file, at the definitions allowed. Section headed:
// Constants used in SETUP_ADC_PORTS() are:

Best Wishes
evaang2003



Joined: 10 Jun 2009
Posts: 14

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 22, 2009 7:41 pm     Reply with quote

the complete code is only involve lcd driver and main function. plus some include header file as below:
Code:
#include <16f877a.h>
#device ADC=10
#use delay(clock=4000000)
#fuses XT, NOWDT, NOPROTECT, NOLVP

as for circuit, I only connect the voltage output pin of lm35 to AN0 and connect the rest to respective power supply and ground. (sorry not posting the schematic).

I have test the component individually.
The voltage output of lm35 is pproportional to 10mV/degree celcius.
When a potentiometer is replaced with lm35 to adjust the voltage output to pic, it shows the analog of pic is working fine.

However, when lm35 and pic16f877a is put together, the reading I get is 5.00 degree celcius. Why?

I have changed the adc setup to setup_adc_ports(AN0).
evaang2003



Joined: 10 Jun 2009
Posts: 14

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 22, 2009 8:11 pm     Reply with quote

hi, everybody, I finally found the problem.
I didn't include the #device ADC=10 into the file that I compile.
That's why I always get the wrong reading.
Thanks for everyone that have try to help me before.
thanks.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 23, 2009 1:36 am     Reply with quote

I'm glad you've found the problem, but this is exactly the reason why we _always_ ask to post the complete program.
serie



Joined: 10 Dec 2008
Posts: 1
Location: selangor

View user's profile Send private message Yahoo Messenger ICQ Number

Re: lm35 temperature sensor
PostPosted: Tue Sep 01, 2009 9:43 pm     Reply with quote

hi, I have tried to compile your program using CCS C compiler but it have an error on lcd_putchar. May I know why? and what kind of compiler that you used?
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

Re: lm35 temperature sensor
PostPosted: Wed Sep 02, 2009 6:14 am     Reply with quote

serie wrote:
hi, I have tried to compile your program using CCS C compiler but it have an error on lcd_putchar. May I know why? and what kind of compiler that you used?


Because he never posted the complete program as requested. It is missing (at least) the LCD include line. However, there is other LM35 code in this forum that is complete. I would suggest using search to find it...
_________________
Google and Forum Search are some of your best tools!!!!
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

PostPosted: Tue Nov 30, 2010 9:02 am     Reply with quote

Code:

setup_adc(ADC_CLOCK_DIV_8);


Hi,

May i know what mean by set the clock_div to 8?
how about set it to other value like 16 or 64?

how the things take effect?

Regards,

sysysy
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Tue Nov 30, 2010 9:42 am     Reply with quote

_Read the chip's data sheet_.....

The ADC requires a clock.
This must be within a certain 'range' of values for the ADC to work correctly.
This line allows you to specify what division is used (out of a limited 'set' of values), to generate the clock.
Data sheet tells you:
What the range of clock values is allowed to be.
Recommended division ratios for several clock rates.
etc. etc...

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Nov 30, 2010 9:59 am     Reply with quote

I think ..

to speed up things...

replace

5.00*temp_adc*100.00/1023.00

with

temp_adc *0.488758 (5.00*100.00/1023.00)=.488758 if my math is right

as floating point operations take a lot of time, esp. division.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Nov 30, 2010 12:05 pm     Reply with quote

For what it's worth, if you have room on the display, during the "debug phase", I always try and display the raw data being read as well as the result. Makes it much easier to debug when you see the raw data read is not what is expected.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

PostPosted: Wed Dec 01, 2010 7:55 am     Reply with quote

temtronic wrote:
I think ..

to speed up things...

replace

5.00*temp_adc*100.00/1023.00

with

temp_adc *0.488758 (5.00*100.00/1023.00)=.488758 if my math is right

as floating point operations take a lot of time, esp. division.


may i know what does ur formula mean by? can explain abit?
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 01, 2010 9:48 am     Reply with quote

The original formula was

temp = 5.00*temp_adc*100.00/1023.00

can be rewritten as

temp=temp_adc*5*100.00/1023.00

which is the same as

temp=temp_adc * (500.00/1023.00)

which is the same as

temp=temp_adc * .488xxxxxx

Be aware that anytime you can reduce floating point operations,you'll speed up the overall program.
In this case I got rid of a huge time killer, FP division !
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group