mn

Dependents:   atmel1602_24c256

Committer:
attila0970
Date:
Mon Aug 12 11:10:04 2019 +0000
Revision:
2:ee3997416bcc
Parent:
0:000a8be2414d
asd;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
unix_guru 0:000a8be2414d 1 /*
unix_guru 0:000a8be2414d 2 * Thermistor Temperature library
unix_guru 0:000a8be2414d 3 * Licensed under the Apache License, Version 2.0 (the "License");
unix_guru 0:000a8be2414d 4 * you may not use this file except in compliance with the License.
unix_guru 0:000a8be2414d 5 * You may obtain a copy of the License at
unix_guru 0:000a8be2414d 6 *
unix_guru 0:000a8be2414d 7 * http://www.apache.org/licenses/LICENSE-2.0
unix_guru 0:000a8be2414d 8 *
unix_guru 0:000a8be2414d 9 * Unless required by applicable law or agreed to in writing, software
unix_guru 0:000a8be2414d 10 * distributed under the License is distributed on an "AS IS" BASIS,
unix_guru 0:000a8be2414d 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
unix_guru 0:000a8be2414d 12 * See the License for the specific language governing permissions and
unix_guru 0:000a8be2414d 13 * limitations under the License.
unix_guru 0:000a8be2414d 14 */
unix_guru 0:000a8be2414d 15 #include "mbed.h"
unix_guru 0:000a8be2414d 16 #include "Thermistor.h"
unix_guru 0:000a8be2414d 17
unix_guru 0:000a8be2414d 18
unix_guru 0:000a8be2414d 19 Thermistor::Thermistor(PinName pin) : _pin(pin) { // _pin(pin) means pass pin to the AnalogIn constructor
unix_guru 0:000a8be2414d 20 init();
unix_guru 0:000a8be2414d 21 }
unix_guru 0:000a8be2414d 22
unix_guru 0:000a8be2414d 23 void Thermistor::init() {
unix_guru 0:000a8be2414d 24 ThermistorNominal = THERMISTORNOMINAL;
unix_guru 0:000a8be2414d 25 TemperatureNominal = TEMPERATURENOMINAL;
unix_guru 0:000a8be2414d 26 BCoefficient = BCOEFFICIENT;
unix_guru 0:000a8be2414d 27 SeriesResistor = SERIESRESISTOR;
unix_guru 0:000a8be2414d 28 }
unix_guru 0:000a8be2414d 29
unix_guru 0:000a8be2414d 30 // This is the workhorse routine that calculates the temperature
unix_guru 0:000a8be2414d 31 // using the Steinhart-Hart equation for thermistors
unix_guru 0:000a8be2414d 32 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
unix_guru 0:000a8be2414d 33 float Thermistor::get_temperature()
unix_guru 0:000a8be2414d 34 {
attila0970 2:ee3997416bcc 35 double temperature =0, resistance =0;
attila0970 2:ee3997416bcc 36 double steinhart =0;
unix_guru 0:000a8be2414d 37 double a=0;
unix_guru 0:000a8be2414d 38 int smooth = 5; // Number of samples to smooth
unix_guru 0:000a8be2414d 39
unix_guru 0:000a8be2414d 40 for(int i=0;i<smooth;i++) {
unix_guru 0:000a8be2414d 41 a += _pin.read_u16(); // Read 16bit Analog value
unix_guru 0:000a8be2414d 42 }
unix_guru 0:000a8be2414d 43 a = a/smooth; // Get average of samples
unix_guru 0:000a8be2414d 44 // pc.printf("Raw Analog Value for Thermistor = %d\r\n",a);
unix_guru 0:000a8be2414d 45
unix_guru 0:000a8be2414d 46 /* Calculate the resistance of the thermistor from analog votage read. */
unix_guru 0:000a8be2414d 47 resistance = (float) SeriesResistor / ((65536.0 / a) - 1);
unix_guru 0:000a8be2414d 48 // pc.printf("Resistance for Thermistor = %f\r\n",resistance);
unix_guru 0:000a8be2414d 49
unix_guru 0:000a8be2414d 50 steinhart = resistance / ThermistorNominal; // (R/Ro)
unix_guru 0:000a8be2414d 51 steinhart = log(steinhart); // ln(R/Ro)
unix_guru 0:000a8be2414d 52 steinhart /= BCoefficient; // 1/B * ln(R/Ro)
unix_guru 0:000a8be2414d 53 steinhart += 1.0 / (TemperatureNominal + 273.15); // + (1/To)
unix_guru 0:000a8be2414d 54 steinhart = 1.0 / steinhart; // Invert
unix_guru 0:000a8be2414d 55 temperature = steinhart - 273.15; // convert to C
unix_guru 0:000a8be2414d 56
unix_guru 0:000a8be2414d 57 return temperature;
unix_guru 0:000a8be2414d 58 }
unix_guru 0:000a8be2414d 59
unix_guru 0:000a8be2414d 60 void Thermistor::set_ThermistorNominal(float thermnom) {
unix_guru 0:000a8be2414d 61 ThermistorNominal = thermnom;
unix_guru 0:000a8be2414d 62 }
unix_guru 0:000a8be2414d 63 void Thermistor::set_TemperatureNominal(float tempnom){
unix_guru 0:000a8be2414d 64 TemperatureNominal = tempnom;
unix_guru 0:000a8be2414d 65 }
unix_guru 0:000a8be2414d 66 void Thermistor::set_BCoefficient(float bcoefficient){
unix_guru 0:000a8be2414d 67 BCoefficient = bcoefficient;
unix_guru 0:000a8be2414d 68 }
unix_guru 0:000a8be2414d 69 void Thermistor::set_SeriesResistor(float resistor){
unix_guru 0:000a8be2414d 70 SeriesResistor = resistor;
unix_guru 0:000a8be2414d 71 }
unix_guru 0:000a8be2414d 72
unix_guru 0:000a8be2414d 73
unix_guru 0:000a8be2414d 74
unix_guru 0:000a8be2414d 75