qbone
Member level 3
- Joined
- Jun 2, 2009
- Messages
- 58
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Location
- Lyngby, Denmark
- Activity points
- 1,778
Hey guys.
I am trying to build a thermostat for a project, and I wanna calculate the temperature using Steinhart equation, and getting the ADC value from a voltage divider made from a 10k Thermister and a 10k 1% resistor.
I read the ADC value just fine, but the equation doesn't work.
It worked when I was playing with Arduino, so there might be some problem I havent thought of.
Here is my code:
Any help would be greatly appreciated
I am trying to build a thermostat for a project, and I wanna calculate the temperature using Steinhart equation, and getting the ADC value from a voltage divider made from a 10k Thermister and a 10k 1% resistor.
I read the ADC value just fine, but the equation doesn't work.
It worked when I was playing with Arduino, so there might be some problem I havent thought of.
Here is my code:
Code:
#include <16F886.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOLVP,NOPUT,NOMCLR,NOBROWNOUT,NOCPD,NOIESO,NOFCMEN,NOWRT,BORV21
#use delay(internal=4000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <math.h>
long Thermister(long RawADC);
void init()
{
setup_timer_0(T0_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
}
void main()
{
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
long temp;
long value;
while(true) {
value = read_adc();
temp = Thermister(value);
printf("A/D value: %Ld = %Ld\n\r", value, temp);
delay_ms(500);
}
}
long Thermister(long RawADC) //Steinhart-Hart Equation
{
long Temp;
Temp = log(((10240000/RawADC) - 10000)); //1024 = ADC Resolution, 10000 = 10kohm Resistor
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); //Steinhart Equation
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
Any help would be greatly appreciated