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 ADC conversion - Some issues..

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
soundsk



Joined: 10 May 2008
Posts: 6

View user's profile Send private message

LM35 ADC conversion - Some issues..
PostPosted: Sat May 17, 2008 3:29 pm     Reply with quote

Hi,

After reading all the posts here, regarding the LM35, i hooked one up to the AN0 pin on a PIC 16F877A.

I am also trying to read atmospheric pressure with an MPX2200AP from freescale and i am currently working on the conversion but for now i would like to get the LM35 working to get that out of the way...

I would like to get accuracy to .5 of a degree or even better .1 but while the code i am using gives float values, the decimal value is always 0, only the units change, ie 22.0, 23.0, 24.0 etc.

How can i get this working properly, and is there anyway i can set a reference voltage or implement something to use the full range of the LM35 (-55ºC to 150ºC)?

The code i am currently using is this:


Code:
#include "lm35.h"
#include <lcdts.c>
#include "picram.h"

void main()
{
int16 temp, press, press1, temp1;

   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_64);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

 lcd_init();

 while(1)
 {

set_adc_channel(0);
delay_ms(700);
temp = read_adc();
temp1 = (temp*49+1)/100;

delay_ms(600);
set_adc_channel(3);
delay_ms(700);
press=read_adc();
press1=press*5.57;

lcd_gotoxy(1,1);
printf(lcd_putc, "Temp : %3.1f %cC   ", (float)temp1,223);
printf(lcd_putc, "\nPressao: %f    ", (float)press1);

delay_ms(500);        //delay 500 ms between reading to prevent self heating of sensor
 }
}
Ttelmah
Guest







PostPosted: Sat May 17, 2008 3:34 pm     Reply with quote

You are not using floats.....
temp is an integer, so is temp1, so is press1.
Take:

temp1 = (temp*49+1)/100;

temp1 is an integer, so the result _has_ to be an integer. Changing it to a 'float' to print, won't make the decimals appear.....

Best Wishes
soundsk



Joined: 10 May 2008
Posts: 6

View user's profile Send private message

PostPosted: Sun May 18, 2008 6:22 am     Reply with quote

Hi,

Yes I know. Sorry i didn't explain myself in the best way.

Anyway, made a few changes to the code, and have verified it is working correctly, displaying changes of half a degree.

To get better resolution i believe the best way would be to use an op-amp to make full use of the 0V to 5V.

And to use the full range of the LM35 i think i would need a dual supply (+ ground and -) and at least two op-amps (one to amplifiy and one to add tension so the whole range is within 0V to 5V, or can this be done only with reference pin on the PIC?).

CODE:
Code:
#include "lm35.h"
#include <lcdts.c>
#include "picram.h"

void main()
{
int16 temp, press;
float temp1, press1;

   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_64);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

 lcd_init();
delay_ms(10);
lcd_putc("\fA inicializar... ");
 while(1)
 {

set_adc_channel(0);
delay_ms(800);
temp = read_adc();
temp1 = (temp*2.5)*2/10;

delay_ms(300);
set_adc_channel(3);
delay_ms(800);
press=read_adc();
press1=press*5.57;

lcd_gotoxy(1,1);
printf(lcd_putc, "\fTemp : %3.1f %cC   ", temp1,223);
//printf(lcd_putc, "\fTemp : %3.1f %cC   ", temp1,223);
printf(lcd_putc, "\nPressao: %3.1f    ", press1);

delay_ms(500);        //delay 500 ms between reading to prevent self heating of sensor
 }
}
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

PostPosted: Mon May 19, 2008 11:38 pm     Reply with quote

Look at the datasheet, it shows various connection configurations one, if not mistaken, enables you to use full range without dual PSU, i considered this chip, but am thinking of using the DS18B20.. makes wiring of multiple long distance sensors more convinient..
Guest








LM35
PostPosted: Mon May 19, 2008 11:55 pm     Reply with quote

Hi,

I agree with Crystal_Lattice, use digital in preference to analog device.

I recently used MAX6675 to measure vehicle exhaust temp. Nice an easy, temp. tp 1024 degrees C.

Easy I2C bus.

Regards,
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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