Mahesh Phalke / tempsensors_prv
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thermistor.cpp Source File

thermistor.cpp

Go to the documentation of this file.
00001 /*!
00002  *****************************************************************************
00003   @file:  thermistor.cpp
00004 
00005   @brief: Thermistor sensor module
00006 
00007   @details:
00008  -----------------------------------------------------------------------------
00009  Copyright (c) 2021 Analog Devices, Inc.  All rights reserved.
00010 
00011  This software is proprietary to Analog Devices, Inc. and its licensors.
00012  By using this software you agree to the terms of the associated
00013  Analog Devices Software License Agreement.
00014 
00015 *****************************************************************************/
00016 
00017 /******************************************************************************/
00018 /***************************** Include Files **********************************/
00019 /******************************************************************************/
00020 
00021 #include <math.h>
00022 #include "thermistor.h"
00023 
00024 /******************************************************************************/
00025 /************************** Functions Definitions *****************************/
00026 /******************************************************************************/
00027 
00028 thermistor::thermistor() {};
00029 thermistor::~thermistor() {};
00030 
00031 /*!
00032  * @brief   Convert the thermistor resistance into equivalent temperature
00033  * @param   resistance[in]- Thermistor resistance (Rth)
00034  * @param   coeff_A[in] - A coefficient for conversion
00035  * @param   coeff_B[in] - B coefficient for conversion
00036  * @param   coeff_C[in] - C coefficient for conversion
00037  * @return  Thermistor temperature value
00038  * @note    This function uses Steinhart-Hart equation for converting Thermistor
00039  *          resistance into temperature. Refer below design note for more details:
00040  *          https://www.analog.com/en/design-center/reference-designs/circuits-from-the-lab/cn0545.html
00041  */
00042 float thermistor::convert(const float resistance, float coeff_A, float coeff_B,
00043               float coeff_C)
00044 {
00045     float temperature = 25.0;
00046 
00047     /* Get temperature into celcius */
00048     temperature = 1 / (coeff_A + (coeff_B * log(resistance)) + (coeff_C * pow(log(
00049                    resistance), 3)));
00050     temperature -= 273.15;
00051 
00052     return temperature;
00053 }
00054 
00055 
00056 /*!
00057  * @brief   Convert the thermistor resistance into equivalent temperature using
00058  *          lookup table
00059  * @param   lut[in]- Pointer to look-up table
00060  * @param   resistance[in] - thermistor resistance
00061  * @param   size[in] - look-up table size
00062  * @param   offset[in] - look-up table offset
00063  * @return  Thermistor temperature value
00064  */
00065 float thermistor::lookup(const uint32_t *lut, uint32_t resistance,
00066              uint16_t size, int16_t offset)
00067 {
00068     uint16_t first = 0;
00069     uint16_t last = size - 1;
00070     uint16_t middle = (first + last) / 2;
00071 
00072     while (first <= last) {
00073         if (resistance < lut[middle])
00074             first = middle + 1;
00075         else if (lut[middle] == resistance) {
00076             return static_cast<float>(middle + offset);
00077         } else
00078             last = middle - 1;
00079 
00080         middle = (first + last) / 2;
00081     }
00082 
00083     if (first > last)
00084         return static_cast<float>(first + offset);
00085 
00086     return 0; // should never get here
00087 }