Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
thermistor.cpp@0:854742598c2c, 2021-02-19 (annotated)
- Committer:
- mahphalke
- Date:
- Fri Feb 19 16:05:58 2021 +0530
- Revision:
- 0:854742598c2c
- Child:
- 1:851dbb04c1e5
Added temperature sensor library source files
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mahphalke |
0:854742598c2c | 1 | /*! |
| mahphalke |
0:854742598c2c | 2 | ***************************************************************************** |
| mahphalke |
0:854742598c2c | 3 | @file: thermistor.cpp |
| mahphalke |
0:854742598c2c | 4 | |
| mahphalke |
0:854742598c2c | 5 | @brief: Thermistor sensor module |
| mahphalke |
0:854742598c2c | 6 | |
| mahphalke |
0:854742598c2c | 7 | @details: |
| mahphalke |
0:854742598c2c | 8 | ----------------------------------------------------------------------------- |
| mahphalke |
0:854742598c2c | 9 | Copyright (c) 2020 Analog Devices, Inc. All rights reserved. |
| mahphalke |
0:854742598c2c | 10 | |
| mahphalke |
0:854742598c2c | 11 | This software is proprietary to Analog Devices, Inc. and its licensors. |
| mahphalke |
0:854742598c2c | 12 | By using this software you agree to the terms of the associated |
| mahphalke |
0:854742598c2c | 13 | Analog Devices Software License Agreement. |
| mahphalke |
0:854742598c2c | 14 | |
| mahphalke |
0:854742598c2c | 15 | *****************************************************************************/ |
| mahphalke |
0:854742598c2c | 16 | |
| mahphalke |
0:854742598c2c | 17 | /******************************************************************************/ |
| mahphalke |
0:854742598c2c | 18 | /***************************** Include Files **********************************/ |
| mahphalke |
0:854742598c2c | 19 | /******************************************************************************/ |
| mahphalke |
0:854742598c2c | 20 | |
| mahphalke |
0:854742598c2c | 21 | #include <math.h> |
| mahphalke |
0:854742598c2c | 22 | #include "thermistor.h" |
| mahphalke |
0:854742598c2c | 23 | |
| mahphalke |
0:854742598c2c | 24 | /******************************************************************************/ |
| mahphalke |
0:854742598c2c | 25 | /************************** Functions Definitions *****************************/ |
| mahphalke |
0:854742598c2c | 26 | /******************************************************************************/ |
| mahphalke |
0:854742598c2c | 27 | |
| mahphalke |
0:854742598c2c | 28 | thermistor::thermistor() {}; |
| mahphalke |
0:854742598c2c | 29 | thermistor::~thermistor() {}; |
| mahphalke |
0:854742598c2c | 30 | |
| mahphalke |
0:854742598c2c | 31 | /*! |
| mahphalke |
0:854742598c2c | 32 | * @brief Convert the thermistor resistance into equivalent temperature |
| mahphalke |
0:854742598c2c | 33 | * @param resistance[in]- Thermistor resistance (Rth) |
| mahphalke |
0:854742598c2c | 34 | * @param coeff_A[in] - A coefficient for conversion |
| mahphalke |
0:854742598c2c | 35 | * @param coeff_B[in] - B coefficient for conversion |
| mahphalke |
0:854742598c2c | 36 | * @param coeff_C[in] - C coefficient for conversion |
| mahphalke |
0:854742598c2c | 37 | * @return Thermistor temperature value |
| mahphalke |
0:854742598c2c | 38 | * @note This function uses Steinhart-Hart equation for converting Thermistor |
| mahphalke |
0:854742598c2c | 39 | * resistance into temperature. Refer below design note for more details: |
| mahphalke |
0:854742598c2c | 40 | * https://www.analog.com/en/design-center/reference-designs/circuits-from-the-lab/cn0545.html |
| mahphalke |
0:854742598c2c | 41 | */ |
| mahphalke |
0:854742598c2c | 42 | float thermistor::convert(const float resistance, float coeff_A, float coeff_B, |
| mahphalke |
0:854742598c2c | 43 | float coeff_C) |
| mahphalke |
0:854742598c2c | 44 | { |
| mahphalke |
0:854742598c2c | 45 | float temperature = 25.0; |
| mahphalke |
0:854742598c2c | 46 | |
| mahphalke |
0:854742598c2c | 47 | /* Get temperature into celcius */ |
| mahphalke |
0:854742598c2c | 48 | temperature = 1 / (coeff_A + (coeff_B * log(resistance)) + (coeff_C * pow(log( |
| mahphalke |
0:854742598c2c | 49 | resistance), 3))); |
| mahphalke |
0:854742598c2c | 50 | temperature -= 273.15; |
| mahphalke |
0:854742598c2c | 51 | |
| mahphalke |
0:854742598c2c | 52 | return temperature; |
| mahphalke |
0:854742598c2c | 53 | } |
| mahphalke |
0:854742598c2c | 54 | |
| mahphalke |
0:854742598c2c | 55 | |
| mahphalke |
0:854742598c2c | 56 | /*! |
| mahphalke |
0:854742598c2c | 57 | * @brief Convert the thermistor resistance into equivalent temperature using |
| mahphalke |
0:854742598c2c | 58 | * lookup table |
| mahphalke |
0:854742598c2c | 59 | * @param lut[in]- Pointer to look-up table |
| mahphalke |
0:854742598c2c | 60 | * @param resistance[in] - thermistor resistance |
| mahphalke |
0:854742598c2c | 61 | * @param size[in] - look-up table size |
| mahphalke |
0:854742598c2c | 62 | * @param offset[in] - look-up table offset |
| mahphalke |
0:854742598c2c | 63 | * @return Thermistor temperature value |
| mahphalke |
0:854742598c2c | 64 | */ |
| mahphalke |
0:854742598c2c | 65 | float thermistor::lookup(const uint32_t *lut, uint32_t resistance, |
| mahphalke |
0:854742598c2c | 66 | uint16_t size, int16_t offset) |
| mahphalke |
0:854742598c2c | 67 | { |
| mahphalke |
0:854742598c2c | 68 | uint16_t first = 0; |
| mahphalke |
0:854742598c2c | 69 | uint16_t last = size - 1; |
| mahphalke |
0:854742598c2c | 70 | uint16_t middle = (first + last) / 2; |
| mahphalke |
0:854742598c2c | 71 | |
| mahphalke |
0:854742598c2c | 72 | while (first <= last) { |
| mahphalke |
0:854742598c2c | 73 | if (resistance < lut[middle]) |
| mahphalke |
0:854742598c2c | 74 | first = middle + 1; |
| mahphalke |
0:854742598c2c | 75 | else if (lut[middle] == resistance) { |
| mahphalke |
0:854742598c2c | 76 | return static_cast<float>(middle + offset); |
| mahphalke |
0:854742598c2c | 77 | } else |
| mahphalke |
0:854742598c2c | 78 | last = middle - 1; |
| mahphalke |
0:854742598c2c | 79 | |
| mahphalke |
0:854742598c2c | 80 | middle = (first + last) / 2; |
| mahphalke |
0:854742598c2c | 81 | } |
| mahphalke |
0:854742598c2c | 82 | |
| mahphalke |
0:854742598c2c | 83 | if (first > last) |
| mahphalke |
0:854742598c2c | 84 | return static_cast<float>(first + offset); |
| mahphalke |
0:854742598c2c | 85 | |
| mahphalke |
0:854742598c2c | 86 | return 0; // should never get here |
| mahphalke |
0:854742598c2c | 87 | } |