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

LMT85 temperature sensor measurement

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

LMT85 temperature sensor measurement
PostPosted: Wed Aug 12, 2020 8:22 am     Reply with quote

HI,
PIC18F2620, Crystal: 1Mhz internal oscillator

LMT85 temperature sensor.

Code:

#device ADC=10

float adc_value;

//main
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0);
setup_comparator(NC_NC_NC_NC);

Code:

void temperature_meas()
{
unsigned int16 adc_read;
float sqrt_value;

set_adc_channel(0);
delay_us(20);
adc_read = read_adc();
adc_value = (float) adc_read;
sqrt_value = sqrt(((-10.888) * (-10.888)) + (4 * 0.00347 * (1777.3 - adc_value)));    // formula taken from datasheet
adc_value = ((10.888 - sqrt_value)/(2 * (-0.00347))) + 30;

lcd_cmd(0x80);
printf(lcd_data, "%3.1f C", adc_value);
}


Displaying incorrect temperature value. Please help.
Ttelmah



Joined: 11 Mar 2010
Posts: 19536

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 8:42 am     Reply with quote

All the equations, assume you have Vadc, in mV. You don't.
Assuming 5v supply, then:

adc_value = adc_read*4.8828;

(5000/1024 steps)
temtronic



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

View user's profile Send private message

PostPosted: Thu Aug 13, 2020 6:18 am     Reply with quote

just a comment...

Floating point math takes a very,very long time with a PIC so it's best to run the PIC at maximum clock rate.

You'll have to recode the ADC setup for it's clock rate (info is in the ADC section of the datasheet).

You can use 'scaled integers' instead of FP math. They are a LOT faster.
Ttelmah



Joined: 11 Mar 2010
Posts: 19536

View user's profile Send private message

PostPosted: Thu Aug 13, 2020 6:30 am     Reply with quote

and (of course), if you do increase the clock frequency, you will have to
change the ADC clock source. 'Internal' is not recommended above 1Mhz.
avatarengineer



Joined: 13 May 2013
Posts: 51
Location: Arizona

View user's profile Send private message Visit poster's website

Integer Math rules
PostPosted: Mon Aug 31, 2020 10:54 am     Reply with quote

I try to avoid any floating point math due to the
speed hit and inacurracy.
Convert your formula into large integers for enough accuracy as needed.
Likely, you only need 1 degree C accuracy since the sensor has worse accuracy.

Example using MCP9701 temp sensor:

Code:

#DEFINE MCP9701offset   400         
#DEFINE MCP9701scale   1950   

void MeasureTemp() {   // uses MCP9701A temp senser   
int32 j=0;                                 
int16 a;                                                                     
 a=GetADC(TempCH); 
// fast 1st order FIR averaging
Monitor.TempRaw=FIRavg(a,Monitor.TempRaw);
 j=Monitor.TempRaw&0xFFFF;
 j*=Vreference;  j/=ADCscale;  // scales to ~ 1mV/count
 if(j>MCP9701offset) {                                                                                                           
   j-=MCP9701offset;           
   j*=Thermal.gain; // 100=100% coef from eerom
   if(j>MCP9701scale)  { j/=MCP9701scale; } 
   if(Thermal.offset>100) { j+=(Thermal.offset-100); } // coef from eerom
   if(Thermal.offset<100) { j-=(100-Thermal.offset); }
 }
 else {j=0;}
                                                             
 XferLW.asLong=j;    // use a union to change long into word
 Monitor.DegC=XferLW.asWord[0]; // read 0C minimum, 127C max

//.... look for over/under temp, set flags, yada yada...
}

;-)
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