Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] NTC Thermistor temperature Sensor using PIC16f1937

Status
Not open for further replies.

Nimms

Newbie level 4
Joined
Jul 5, 2012
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,352
Hi All,
Iam very new to Pic,iam designing a 4 channel thermistor temperaturse sensor using pic16f1937.
i wanted to read 4 thermistors inputs and get the ADC values convert it to temprature and send it to serial port.iam getting different adc values in the hyper terminal,is this the correct code.how can i convert the adc values to temperature ands send it to serial port ..could anyone help me




#include <stdio.h>
#include <stdlib.h>
#include <htc.h>


#define _XTAL_FREQ 4000000
int advalue=0;
void ADCInit()
{
ANSELA = 0xF;
ANSELB = 0;
ANSELE = 0;
ADCON1 = 0xA0;
}
void usart_init(void)
{
SYNC = 0; // Transmisson is asynchronous

TRISC7 = 1;
TRISC6 = 1;
BRGH = 1;
SPBRG = 25; // 4mhz xtal 9600 BAUD
TXEN = 1;
CREN = 1;
SPEN = 1; // SPEN and CREN

RCIF = 0; // Clear USART receive interrupt flag
TXIF = 0; // Clear USART transmit interrupt flag

}



void usart_write_data(unsigned char data) //Reads the character and move to TXREG register
{
while(!TXIF);
TXREG = data;
}



void USART_putstring(char* StringPtr)
{
while(*StringPtr != 0x00)
{
usart_write_data(*StringPtr);
StringPtr++;
}
}




unsigned int ADCread(unsigned char ch)
{
if (ch>13)
return 0;
ADCON0 = 0;
ADCON0 = ch<<3; // select ADCchannel
ADCON0 = 1; // swich on adc module
_nop();
_nop();
GO_nDONE=1; //START CONVERSION
while(GO_nDONE); //waiting conversion to finish
ADCON0=0;
return (int)ADRESH *256 + ADRESL;
}






void main(void)
{
OSCCON = SYS_OSC;
_nop();
_nop();
void ADCInit();//Initializing register
unsigned int ADCread(unsigned char ch);
void usart_init(void);
void usart_write_data(unsigned char data) ;
void USART_putstring(char* StringPtr);
//double getTemp();
//double convert_ADC2Temp( int adc_value );
//double temp_value;
//double y2;

char buffer[5];
int adc_value;
//int i=0;
sysinit () ;
ADCInit();
usart_init();



for(;;)
{
USART_putstring("Reading channel ");
USART_putstring(" : ");
adc_value= ADCread(0);
itoa( buffer,adc_value, 10);
USART_putstring(buffer);

USART_putstring(" ");
usart_write_data('\r');
usart_write_data('\n');



}
}
 

Unfortunatly, the output from the thermister is not linear.
Assuming your reading in volts is R, then you could calculate the temperature with this function.

const double NtcA = 0.00110653;
const double NtcB = 0.00025454;
const double NtcC = 0.00000016;
const double AbsZero = 273.15;
double Temperature;


Temperature = (1 / (NtcA + NtcB * Math.Log(R) + NtcC * (Math.Pow(Math.Log(R), 3)))) - AbsZero;

But this is not practical in a small micro.

You could use a lookup table taken from the data sheet of the thermister.
You could extrapolate the result from the lookup table for better resolution.
 

i had created a lookup table with my thermistor values anda code to interpolate temperature from that values.but its showing error and i dont know how to convert double to string for sending it to serial port.when i used ftoa function it shows error.here is my temperature conversion code.it shows conflicting declarations with variables.thanks in advance
#include<htc.h>
#include<stdio.h>
#include<htc.h>
#include <pic.h>
struct V2T
{
double temp; //Temperature Value
int adc; //ADC Analogue
}
values[] =
{
{-5, 810},
{0 ,768},
{5 ,721},
{10,671},
{15,619},
{20,565},
{25,512},
{30,460},
{35,411},
{40,366},
{45,323},
{50,284},
{55,249},
{50,218},
{65,191},
{70,167},
{75,146}
};

float getTemp()
{
//Get the ADC Value
//
int adc_value;
double temperature_value;
adc_value = ADCread(0);

//Get Temperature by giving ADC
temperature_value = convert_ADC2Temp( adc_value );
return temperature_value;
}


//This function convert ADC to Temperature
float convert_ADC2Temp(int x2 )
{
int i,j; //Initialising the loop counter
int x1=0; float y1=0; //x1 - nearest lowest adc : y1 - nearest lowest temperature
int x3=0; float y3=0; //x3 - nearest highest adc : y3 - nearest highest temperature
float y2=-1; //x2 is the user input and the y2 is the result
int array_size = sizeof(values) / (sizeof(float)+sizeof(int));

///////////////////////////////////
// Calculation
// y2 = ((x2-x1)(y3-y1)/(x3-x1 ))+y1
///////////////////////////////////

//Check if the temperature is already existing
for ( i = 0; i < array_size; i++ )
{
if ( values.adc == x2 )
{
y2 = values.temp;
break;
}
}

//Loop through the structure and find out the values for x1,y1 and x2 for the calculation
if ( y2 == -1 )
{
for ( i = 0; i < array_size; i++ )
{
//Store the nearest Lowest Value in variables
if ( values.adc < x2 && values.adc > x1 )
{
x1 = values.adc;
y1 = values.temp;
}

if (x3 == 0 && i == 0 ) //set x3 with the highest value for calculation
{
for ( j=0;j<array_size;j++)
{
if (values[j].adc>x3)
x3 = values[j].adc;
}
}

//Store the nearest Highest Value in variables
if ( values.adc > x2 && values.adc <= x3 )
{
x3 = values.adc;
y3 = values.temp;
}
}

y2 = (((x2-x1)*(y3-y1))/(x3-x1)) + y1;
}
return y2;
}
 

I think the array should be defined like:

Code:
values[2][] =
{
{-5, 810},
{0 ,768},
{5 ,721},
{10,671},
{15,619},
{20,565},
{25,512},
{30,460},
{35,411},
{40,366},
{45,323},
{50,284},
{55,249},
{50,218},
{65,191},
{70,167},
{75,146}
};
 

its still showing same error.
 

What is the error?

If you need two decimal places, one way is to multiply the result by 100.
Then you don't need floating point.

The PC program could easily divide by 100 to get the result.
 

tempconversion.c:47: error: conflicting declarations for variable

- - - Updated - - -

and iam not getting how to send it to through serial port to displeay in hyperterminal
 

You have not posted the full code, so I cant see where you have declared an instance of your structure.

x1 = values.adc;
y1 = values.temp;

These statements make no sense to me?
Unless you've declared an array of structures?

Code:
/*--- Separate Structure and array ---*/

struct V2T
 {
 double temp; //Temperature Value
 int adc;     //ADC Analogue
 };

/* --- Declare structure ---*/

V2T Vresult;

/*--- Compare with array ---*/

if(Vresult.adc == values[1][0]){
  Vresult.temp = values[0][0];
  }
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top