View previous topic :: View next topic |
Author |
Message |
evaang2003
Joined: 10 Jun 2009 Posts: 14
|
lm35 temperature sensor |
Posted: Mon Jun 22, 2009 3:54 am |
|
|
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
|
|
Posted: Mon Jun 22, 2009 4:03 am |
|
|
Paste the complete code.
Did you measure the voltage on output pin of LM35? |
|
|
aasief
Joined: 12 Feb 2009 Posts: 12 Location: cape town
|
|
Posted: Mon Jun 22, 2009 4:09 am |
|
|
Show me the LM35 circuit and what is your voltage coming out of your LM35 circuit into the pic |
|
|
Ttelmah Guest
|
|
Posted: Mon Jun 22, 2009 4:37 am |
|
|
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
|
|
Posted: Mon Jun 22, 2009 7:41 pm |
|
|
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
|
|
Posted: Mon Jun 22, 2009 8:11 pm |
|
|
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
|
|
Posted: Tue Jun 23, 2009 1:36 am |
|
|
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
|
Re: lm35 temperature sensor |
Posted: Tue Sep 01, 2009 9:43 pm |
|
|
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
|
Re: lm35 temperature sensor |
Posted: Wed Sep 02, 2009 6:14 am |
|
|
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
|
|
Posted: Tue Nov 30, 2010 9:02 am |
|
|
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: 19552
|
|
Posted: Tue Nov 30, 2010 9:42 am |
|
|
_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: 9246 Location: Greensville,Ontario
|
|
Posted: Tue Nov 30, 2010 9:59 am |
|
|
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
|
|
Posted: Tue Nov 30, 2010 12:05 pm |
|
|
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
|
|
Posted: Wed Dec 01, 2010 7:55 am |
|
|
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: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Dec 01, 2010 9:48 am |
|
|
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 ! |
|
|
|