Calculate temperature based on resistance from thermistor with lookup chart
Diff: thermister_ert-j0eg1.cpp
- Revision:
- 5:88bf5a42812e
- Parent:
- 1:cc492e6ba333
- Child:
- 6:3c9cb2bd08c6
diff -r 4141caf9f697 -r 88bf5a42812e thermister_ert-j0eg1.cpp --- a/thermister_ert-j0eg1.cpp Thu Mar 10 22:15:04 2016 +0000 +++ b/thermister_ert-j0eg1.cpp Wed Mar 30 13:32:40 2016 +0000 @@ -1,19 +1,45 @@ #include "mbed.h" #include "ohms.h" // from library ohms_law - +#include "thermister_ert-j0eg1.h" // Set of resistance values from data sheet used to lookup temperature to find // location in a 5V band. -static long thermisterResistance[] = {205200,154800,117900,90690,70370,50570,43440,34530,27640,22270,18060,14740,12110, +const long thermisterResistance[] = {205200,154800,117900,90690,70370,50570,43440,34530,27640,22270,18060,14740,12110, 10000,8309,6941,5828,4916,4165,3542,3027,2595,2233,1929,1672,1451,1261,1097,956,836,732,642,565,499}; -static int thermisterBase = -40; -static int thermisterStep = 5; -static int thermisterMaxTemp = 125; -const float adc_ref = 3.30; - - +const int thermisterBase = -40.0f; +const int thermisterStep = 5.0f; +const int thermisterMaxTemp = 125.0f; float conv_c_to_f(float tempC) { - return (tempC * 1.8) + 32.0; + return (tempC * 1.8f) + 32.0f; +} + +float conv_f_to_c(float tempF) { + return (tempF - 32.0f) * 0.55555555555f; +} + +float ERTThermisterRead(PinName apin) { + #ifdef DEBUG3 + printf("ERTThermisterRead start\r\n"); + #endif + AnalogIn adcPin(apin); + float tmp = ERTThermisterReadTemp(adcPin); + #ifdef DEBUG3 + printf("ERTThermisterRead temp=%f done\r\n", tmp); + #endif + return tmp; +} + +// read the temp from thermister and print out +// both the reading and intermediate values. +float ERTThermisterReadPrint(Serial log, char *label, PinName apin) { + AnalogIn adcpin(apin); + float sensorValue = adcpin.read() * ERTThermisterAdcRef; + long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue); + float t1C = ERTThermisterReadTemp(adcpin); + float t1F = conv_c_to_f(t1C); + log.printf("%s\tvin=%4.2f\tresist=%ld\tT1C=%4.1f\tT1F=%4.2f\r\n", + label, sensorValue, sensorResist, t1C, t1F); + return t1C; } @@ -22,40 +48,54 @@ lower than our specified value which gives us temp with 5C spread then adjust by how far our sensor reading is above the lower bound of the range.*/ -float readTemp(AnalogIn adcpin) { +float ERTThermisterReadTemp(AnalogIn adcpin) { //sensorValue = avgRead(adcpin,3,2); - float sensorValue = adcpin.read() * adc_ref; - long sensorResist = calcResistV(22000, adc_ref, sensorValue); + float sensorValue = adcpin.read() * ERTThermisterAdcRef; + long sensorResist = calcResistV(22000, ERTThermisterAdcRef, sensorValue); int tempCnt = thermisterBase; int tempndx = 0; - - if (sensorValue >= thermisterResistance[0]) { - return -255; + #ifdef DEBUG3 + printf("start ERTThermisterReadTemp adc=%4.2f resist=%ld\r\n", sensorValue, sensorResist); + #endif + if (sensorResist >= thermisterResistance[0]) { + #ifdef DEBUG3 + printf("resist too high\r\n"); + #endif + return ERTThermisterErrorUnder; } + #ifdef DEBUG3 + printf("entering while loop sensorResist=%ld thermisterResistance[0]=%ld\r\n",sensorResist, thermisterResistance[0]); + #endif while (sensorResist < thermisterResistance[tempndx]) { tempndx++; tempCnt += thermisterStep; if (tempCnt >= thermisterMaxTemp) { - return 255; + return ERTThermisterErrorOver; } } - tempCnt -= thermisterStep; // backup to begin of range + tempCnt -= thermisterStep; // backup to begin of range + #ifdef DEBUG3 + printf("tempCnt=%d\r\n", tempCnt); + #endif int lowTempResist = thermisterResistance[tempndx -1]; int highTempResist = thermisterResistance[tempndx]; int resistRangeDelta = lowTempResist - highTempResist; float degreesPerUnit = (float) thermisterStep / (float) (resistRangeDelta); int unitOverlow = lowTempResist - sensorResist; float degreeAdj = (float) unitOverlow * degreesPerUnit; + #ifdef DEBUG3 + printf("degreeAdj=%f\r\n", degreeAdj); + #endif return (float) tempCnt + degreeAdj; } -float avgReadTemp(AnalogIn adcpin, int numRead) { +float ERTThermisterAvgReadTemp(AnalogIn adcpin, int numRead) { float sum = 0; int ndx; for (ndx = 0; ndx < numRead; ndx++) { wait(0.005); - sum += readTemp(adcpin); + sum += ERTThermisterReadTemp(adcpin); } return (float) ((float) sum / (float) ndx); }