Library to support temperature sensor conversions and lookups
thermistor.cpp@2:bcfa5a2f21c9, 2021-03-19 (annotated)
- Committer:
- mahphalke
- Date:
- Fri Mar 19 08:09:19 2021 +0000
- Revision:
- 2:bcfa5a2f21c9
- Parent:
- 1:f65f6fadda5d
Fixed line ending issues
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mahphalke | 1:f65f6fadda5d | 1 | /*! |
mahphalke | 1:f65f6fadda5d | 2 | ***************************************************************************** |
mahphalke | 1:f65f6fadda5d | 3 | @file: thermistor.cpp |
mahphalke | 1:f65f6fadda5d | 4 | |
mahphalke | 1:f65f6fadda5d | 5 | @brief: Thermistor sensor module |
mahphalke | 1:f65f6fadda5d | 6 | |
mahphalke | 1:f65f6fadda5d | 7 | @details: |
mahphalke | 1:f65f6fadda5d | 8 | ----------------------------------------------------------------------------- |
mahphalke | 1:f65f6fadda5d | 9 | Copyright (c) 2021 Analog Devices, Inc. All rights reserved. |
mahphalke | 1:f65f6fadda5d | 10 | |
mahphalke | 1:f65f6fadda5d | 11 | This software is proprietary to Analog Devices, Inc. and its licensors. |
mahphalke | 1:f65f6fadda5d | 12 | By using this software you agree to the terms of the associated |
mahphalke | 1:f65f6fadda5d | 13 | Analog Devices Software License Agreement. |
mahphalke | 1:f65f6fadda5d | 14 | |
mahphalke | 1:f65f6fadda5d | 15 | *****************************************************************************/ |
mahphalke | 1:f65f6fadda5d | 16 | |
mahphalke | 1:f65f6fadda5d | 17 | /******************************************************************************/ |
mahphalke | 1:f65f6fadda5d | 18 | /***************************** Include Files **********************************/ |
mahphalke | 1:f65f6fadda5d | 19 | /******************************************************************************/ |
mahphalke | 1:f65f6fadda5d | 20 | |
mahphalke | 1:f65f6fadda5d | 21 | #include <math.h> |
mahphalke | 1:f65f6fadda5d | 22 | #include "thermistor.h" |
mahphalke | 1:f65f6fadda5d | 23 | |
mahphalke | 1:f65f6fadda5d | 24 | /******************************************************************************/ |
mahphalke | 1:f65f6fadda5d | 25 | /************************** Functions Definitions *****************************/ |
mahphalke | 1:f65f6fadda5d | 26 | /******************************************************************************/ |
mahphalke | 1:f65f6fadda5d | 27 | |
mahphalke | 1:f65f6fadda5d | 28 | thermistor::thermistor() {}; |
mahphalke | 1:f65f6fadda5d | 29 | thermistor::~thermistor() {}; |
mahphalke | 1:f65f6fadda5d | 30 | |
mahphalke | 1:f65f6fadda5d | 31 | /*! |
mahphalke | 1:f65f6fadda5d | 32 | * @brief Convert the thermistor resistance into equivalent temperature |
mahphalke | 1:f65f6fadda5d | 33 | * @param resistance[in]- Thermistor resistance (Rth) |
mahphalke | 1:f65f6fadda5d | 34 | * @param coeff_A[in] - A coefficient for conversion |
mahphalke | 1:f65f6fadda5d | 35 | * @param coeff_B[in] - B coefficient for conversion |
mahphalke | 1:f65f6fadda5d | 36 | * @param coeff_C[in] - C coefficient for conversion |
mahphalke | 1:f65f6fadda5d | 37 | * @return Thermistor temperature value |
mahphalke | 1:f65f6fadda5d | 38 | * @note This function uses Steinhart-Hart equation for converting Thermistor |
mahphalke | 1:f65f6fadda5d | 39 | * resistance into temperature. Refer below design note for more details: |
mahphalke | 1:f65f6fadda5d | 40 | * https://www.analog.com/en/design-center/reference-designs/circuits-from-the-lab/cn0545.html |
mahphalke | 1:f65f6fadda5d | 41 | */ |
mahphalke | 1:f65f6fadda5d | 42 | float thermistor::convert(const float resistance, float coeff_A, float coeff_B, |
mahphalke | 1:f65f6fadda5d | 43 | float coeff_C) |
mahphalke | 1:f65f6fadda5d | 44 | { |
mahphalke | 1:f65f6fadda5d | 45 | float temperature = 25.0; |
mahphalke | 1:f65f6fadda5d | 46 | |
mahphalke | 1:f65f6fadda5d | 47 | /* Get temperature into celcius */ |
mahphalke | 1:f65f6fadda5d | 48 | temperature = 1 / (coeff_A + (coeff_B * log(resistance)) + (coeff_C * pow(log( |
mahphalke | 1:f65f6fadda5d | 49 | resistance), 3))); |
mahphalke | 1:f65f6fadda5d | 50 | temperature -= 273.15; |
mahphalke | 1:f65f6fadda5d | 51 | |
mahphalke | 1:f65f6fadda5d | 52 | return temperature; |
mahphalke | 1:f65f6fadda5d | 53 | } |
mahphalke | 1:f65f6fadda5d | 54 | |
mahphalke | 1:f65f6fadda5d | 55 | |
mahphalke | 1:f65f6fadda5d | 56 | /*! |
mahphalke | 1:f65f6fadda5d | 57 | * @brief Convert the thermistor resistance into equivalent temperature using |
mahphalke | 1:f65f6fadda5d | 58 | * lookup table |
mahphalke | 1:f65f6fadda5d | 59 | * @param lut[in]- Pointer to look-up table |
mahphalke | 1:f65f6fadda5d | 60 | * @param resistance[in] - thermistor resistance |
mahphalke | 1:f65f6fadda5d | 61 | * @param size[in] - look-up table size |
mahphalke | 1:f65f6fadda5d | 62 | * @param offset[in] - look-up table offset |
mahphalke | 1:f65f6fadda5d | 63 | * @return Thermistor temperature value |
mahphalke | 1:f65f6fadda5d | 64 | */ |
mahphalke | 1:f65f6fadda5d | 65 | float thermistor::lookup(const uint32_t *lut, uint32_t resistance, |
mahphalke | 1:f65f6fadda5d | 66 | uint16_t size, int16_t offset) |
mahphalke | 1:f65f6fadda5d | 67 | { |
mahphalke | 1:f65f6fadda5d | 68 | uint16_t first = 0; |
mahphalke | 1:f65f6fadda5d | 69 | uint16_t last = size - 1; |
mahphalke | 1:f65f6fadda5d | 70 | uint16_t middle = (first + last) / 2; |
mahphalke | 1:f65f6fadda5d | 71 | |
mahphalke | 1:f65f6fadda5d | 72 | while (first <= last) { |
mahphalke | 1:f65f6fadda5d | 73 | if (resistance < lut[middle]) |
mahphalke | 1:f65f6fadda5d | 74 | first = middle + 1; |
mahphalke | 1:f65f6fadda5d | 75 | else if (lut[middle] == resistance) { |
mahphalke | 1:f65f6fadda5d | 76 | return static_cast<float>(middle + offset); |
mahphalke | 1:f65f6fadda5d | 77 | } else |
mahphalke | 1:f65f6fadda5d | 78 | last = middle - 1; |
mahphalke | 1:f65f6fadda5d | 79 | |
mahphalke | 1:f65f6fadda5d | 80 | middle = (first + last) / 2; |
mahphalke | 1:f65f6fadda5d | 81 | } |
mahphalke | 1:f65f6fadda5d | 82 | |
mahphalke | 1:f65f6fadda5d | 83 | if (first > last) |
mahphalke | 1:f65f6fadda5d | 84 | return static_cast<float>(first + offset); |
mahphalke | 1:f65f6fadda5d | 85 | |
mahphalke | 1:f65f6fadda5d | 86 | return 0; // should never get here |
mahphalke | 1:f65f6fadda5d | 87 | } |