Example program for EVAL-AD4130
Dependencies: tempsensors sdp_k1_sdram
app/ad4130_temperature_sensor.cpp@2:7b2b268ea49c, 22 months ago (annotated)
- Committer:
- Mahesh Phalke
- Date:
- Wed Jul 20 18:12:00 2022 +0530
- Revision:
- 2:7b2b268ea49c
Initial firmware commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mahesh Phalke |
2:7b2b268ea49c | 1 | /***************************************************************************//* |
Mahesh Phalke |
2:7b2b268ea49c | 2 | * @file ad4130_temperature_sensor.cpp |
Mahesh Phalke |
2:7b2b268ea49c | 3 | * @brief AD4130 temperature sensor measurement functionality |
Mahesh Phalke |
2:7b2b268ea49c | 4 | ****************************************************************************** |
Mahesh Phalke |
2:7b2b268ea49c | 5 | * Copyright (c) 2022 Analog Devices, Inc. |
Mahesh Phalke |
2:7b2b268ea49c | 6 | * All Rights Reserved. |
Mahesh Phalke |
2:7b2b268ea49c | 7 | * |
Mahesh Phalke |
2:7b2b268ea49c | 8 | * This software is proprietary to Analog Devices, Inc. and its licensors. |
Mahesh Phalke |
2:7b2b268ea49c | 9 | * By using this software you agree to the terms of the associated |
Mahesh Phalke |
2:7b2b268ea49c | 10 | * Analog Devices Software License Agreement. |
Mahesh Phalke |
2:7b2b268ea49c | 11 | ******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 12 | |
Mahesh Phalke |
2:7b2b268ea49c | 13 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 14 | /***************************** Include Files **********************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 15 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 16 | |
Mahesh Phalke |
2:7b2b268ea49c | 17 | #include <stdint.h> |
Mahesh Phalke |
2:7b2b268ea49c | 18 | #include <math.h> |
Mahesh Phalke |
2:7b2b268ea49c | 19 | |
Mahesh Phalke |
2:7b2b268ea49c | 20 | #include <thermocouple.h> |
Mahesh Phalke |
2:7b2b268ea49c | 21 | #include <ntc_10k_44031.h> |
Mahesh Phalke |
2:7b2b268ea49c | 22 | #include <ptxxx.h> |
Mahesh Phalke |
2:7b2b268ea49c | 23 | #include "ad4130_temperature_sensor.h" |
Mahesh Phalke |
2:7b2b268ea49c | 24 | |
Mahesh Phalke |
2:7b2b268ea49c | 25 | #ifdef __cplusplus |
Mahesh Phalke |
2:7b2b268ea49c | 26 | extern "C" |
Mahesh Phalke |
2:7b2b268ea49c | 27 | { |
Mahesh Phalke |
2:7b2b268ea49c | 28 | #endif // _cplusplus |
Mahesh Phalke |
2:7b2b268ea49c | 29 | |
Mahesh Phalke |
2:7b2b268ea49c | 30 | #include "app_config.h" |
Mahesh Phalke |
2:7b2b268ea49c | 31 | #include "ad4130_support.h" |
Mahesh Phalke |
2:7b2b268ea49c | 32 | |
Mahesh Phalke |
2:7b2b268ea49c | 33 | #ifdef __cplusplus // Closing extern c |
Mahesh Phalke |
2:7b2b268ea49c | 34 | } |
Mahesh Phalke |
2:7b2b268ea49c | 35 | #endif // _cplusplus |
Mahesh Phalke |
2:7b2b268ea49c | 36 | |
Mahesh Phalke |
2:7b2b268ea49c | 37 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 38 | /********************* Macros and Constants Definitions ***********************/ |
Mahesh Phalke |
2:7b2b268ea49c | 39 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 40 | |
Mahesh Phalke |
2:7b2b268ea49c | 41 | /* NTC thermistor Rsense value (in ohms) */ |
Mahesh Phalke |
2:7b2b268ea49c | 42 | #define NTC_RSENSE 10000U |
Mahesh Phalke |
2:7b2b268ea49c | 43 | |
Mahesh Phalke |
2:7b2b268ea49c | 44 | /* RTD Rref Resistance value (in ohms) */ |
Mahesh Phalke |
2:7b2b268ea49c | 45 | #define RTD_RREF 5110U |
Mahesh Phalke |
2:7b2b268ea49c | 46 | |
Mahesh Phalke |
2:7b2b268ea49c | 47 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 48 | /******************** Variables and User Defined Data Types *******************/ |
Mahesh Phalke |
2:7b2b268ea49c | 49 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 50 | |
Mahesh Phalke |
2:7b2b268ea49c | 51 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 52 | /************************** Functions Definitions *****************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 53 | /******************************************************************************/ |
Mahesh Phalke |
2:7b2b268ea49c | 54 | |
Mahesh Phalke |
2:7b2b268ea49c | 55 | /*! |
Mahesh Phalke |
2:7b2b268ea49c | 56 | * @brief Convert the NTC thermistor voltage into equivalent resistance |
Mahesh Phalke |
2:7b2b268ea49c | 57 | * @param ntc_voltage[in] - NTC Thermistor voltage |
Mahesh Phalke |
2:7b2b268ea49c | 58 | * @return NTC Thermistor resistance value |
Mahesh Phalke |
2:7b2b268ea49c | 59 | * @note The NTC is biased with constant ADC reference voltage. Below formula |
Mahesh Phalke |
2:7b2b268ea49c | 60 | * is based on ratiometric measurement, where fixed value of ADC REF |
Mahesh Phalke |
2:7b2b268ea49c | 61 | * and gain is taken into account |
Mahesh Phalke |
2:7b2b268ea49c | 62 | */ |
Mahesh Phalke |
2:7b2b268ea49c | 63 | static float convert_ntc_voltage_into_resistance(float ntc_voltage) |
Mahesh Phalke |
2:7b2b268ea49c | 64 | { |
Mahesh Phalke |
2:7b2b268ea49c | 65 | return ((ntc_voltage * NTC_RSENSE) / (AD4170_1_25V_INT_REF_VOLTAGE - |
Mahesh Phalke |
2:7b2b268ea49c | 66 | ntc_voltage)); |
Mahesh Phalke |
2:7b2b268ea49c | 67 | } |
Mahesh Phalke |
2:7b2b268ea49c | 68 | |
Mahesh Phalke |
2:7b2b268ea49c | 69 | /** |
Mahesh Phalke |
2:7b2b268ea49c | 70 | * @brief Convert ADC raw value into equivalent NTC temperature |
Mahesh Phalke |
2:7b2b268ea49c | 71 | * @param dev[in] - Device instance |
Mahesh Phalke |
2:7b2b268ea49c | 72 | * @param ntc_sample[in] - Raw ADC sample for NTC sensor |
Mahesh Phalke |
2:7b2b268ea49c | 73 | * @param chn[in] - ADC channel |
Mahesh Phalke |
2:7b2b268ea49c | 74 | * @return NTC temperature |
Mahesh Phalke |
2:7b2b268ea49c | 75 | * @note Fixed NTC 10K 44031RC sensor is used |
Mahesh Phalke |
2:7b2b268ea49c | 76 | */ |
Mahesh Phalke |
2:7b2b268ea49c | 77 | float get_ntc_thermistor_temperature(void *dev, uint32_t ntc_sample, |
Mahesh Phalke |
2:7b2b268ea49c | 78 | uint8_t chn) |
Mahesh Phalke |
2:7b2b268ea49c | 79 | { |
Mahesh Phalke |
2:7b2b268ea49c | 80 | ntc_10k_44031rc ntc_thermistor; |
Mahesh Phalke |
2:7b2b268ea49c | 81 | float ntc_voltage; |
Mahesh Phalke |
2:7b2b268ea49c | 82 | float ntc_resistance; |
Mahesh Phalke |
2:7b2b268ea49c | 83 | |
Mahesh Phalke |
2:7b2b268ea49c | 84 | ntc_voltage = convert_adc_sample_into_voltage(dev, ntc_sample, chn); |
Mahesh Phalke |
2:7b2b268ea49c | 85 | ntc_resistance = convert_ntc_voltage_into_resistance(ntc_voltage); |
Mahesh Phalke |
2:7b2b268ea49c | 86 | |
Mahesh Phalke |
2:7b2b268ea49c | 87 | return ntc_thermistor.convert(ntc_resistance); |
Mahesh Phalke |
2:7b2b268ea49c | 88 | } |
Mahesh Phalke |
2:7b2b268ea49c | 89 | |
Mahesh Phalke |
2:7b2b268ea49c | 90 | /** |
Mahesh Phalke |
2:7b2b268ea49c | 91 | * @brief Convert ADC raw value into equivalent RTD temperature |
Mahesh Phalke |
2:7b2b268ea49c | 92 | * @param dev[in] - Device instance |
Mahesh Phalke |
2:7b2b268ea49c | 93 | * @param rtd_sample[in] - Raw ADC sample for RTD sensor |
Mahesh Phalke |
2:7b2b268ea49c | 94 | * @pram chn[in] - ADC channel |
Mahesh Phalke |
2:7b2b268ea49c | 95 | * @return RTD temperature |
Mahesh Phalke |
2:7b2b268ea49c | 96 | * @note Fixed PT100 RTD sensor is used |
Mahesh Phalke |
2:7b2b268ea49c | 97 | */ |
Mahesh Phalke |
2:7b2b268ea49c | 98 | float get_rtd_temperature(void *dev, uint32_t rtd_sample, uint8_t chn) |
Mahesh Phalke |
2:7b2b268ea49c | 99 | { |
Mahesh Phalke |
2:7b2b268ea49c | 100 | PT100 rtd_sensor; |
Mahesh Phalke |
2:7b2b268ea49c | 101 | float rtd_resistance; |
Mahesh Phalke |
2:7b2b268ea49c | 102 | |
Mahesh Phalke |
2:7b2b268ea49c | 103 | rtd_resistance = convert_adc_raw_into_rtd_resistance(dev, rtd_sample, RTD_RREF, |
Mahesh Phalke |
2:7b2b268ea49c | 104 | chn); |
Mahesh Phalke |
2:7b2b268ea49c | 105 | return rtd_sensor.convertResistanceToTemperature(rtd_resistance); |
Mahesh Phalke |
2:7b2b268ea49c | 106 | } |
Mahesh Phalke |
2:7b2b268ea49c | 107 | |
Mahesh Phalke |
2:7b2b268ea49c | 108 | /** |
Mahesh Phalke |
2:7b2b268ea49c | 109 | * @brief Convert ADC raw value into TC temperature |
Mahesh Phalke |
2:7b2b268ea49c | 110 | * @param dev[in] - Device instance |
Mahesh Phalke |
2:7b2b268ea49c | 111 | * @param tc_sample[in] - Raw TC sample |
Mahesh Phalke |
2:7b2b268ea49c | 112 | * @param cjc_sample[in] - Raw CJC sample |
Mahesh Phalke |
2:7b2b268ea49c | 113 | * @param tc_chn[in] - TC ADC channel |
Mahesh Phalke |
2:7b2b268ea49c | 114 | * @param cjc_chn[in] - CJC ADC channel |
Mahesh Phalke |
2:7b2b268ea49c | 115 | * @param cjc_temp[in, out] - CJC temperature value |
Mahesh Phalke |
2:7b2b268ea49c | 116 | * @return TC temperature |
Mahesh Phalke |
2:7b2b268ea49c | 117 | * @note T type thermocouple is used as default. For CJC, PT1000 |
Mahesh Phalke |
2:7b2b268ea49c | 118 | * RTD sensor is used as default. |
Mahesh Phalke |
2:7b2b268ea49c | 119 | */ |
Mahesh Phalke |
2:7b2b268ea49c | 120 | float get_tc_temperature(void *dev, |
Mahesh Phalke |
2:7b2b268ea49c | 121 | uint32_t tc_sample, uint32_t cjc_sample, |
Mahesh Phalke |
2:7b2b268ea49c | 122 | uint8_t tc_chn, uint8_t cjc_chn, |
Mahesh Phalke |
2:7b2b268ea49c | 123 | float *cjc_temp) |
Mahesh Phalke |
2:7b2b268ea49c | 124 | { |
Mahesh Phalke |
2:7b2b268ea49c | 125 | Thermocouple_Type_T tcSensor; |
Mahesh Phalke |
2:7b2b268ea49c | 126 | float tc_mv; |
Mahesh Phalke |
2:7b2b268ea49c | 127 | float tc_temperature; |
Mahesh Phalke |
2:7b2b268ea49c | 128 | float cjc_temperature; |
Mahesh Phalke |
2:7b2b268ea49c | 129 | PT1000 rtd_sensor; |
Mahesh Phalke |
2:7b2b268ea49c | 130 | float rtd_resistance; |
Mahesh Phalke |
2:7b2b268ea49c | 131 | |
Mahesh Phalke |
2:7b2b268ea49c | 132 | tc_mv = convert_adc_sample_into_voltage(dev, tc_sample, tc_chn) * 1000; |
Mahesh Phalke |
2:7b2b268ea49c | 133 | tc_temperature = tcSensor.convert(tc_mv); |
Mahesh Phalke |
2:7b2b268ea49c | 134 | |
Mahesh Phalke |
2:7b2b268ea49c | 135 | #if defined(USE_CJC_AS_RTD) |
Mahesh Phalke |
2:7b2b268ea49c | 136 | rtd_resistance = convert_adc_raw_into_rtd_resistance(dev, cjc_sample, RTD_RREF, |
Mahesh Phalke |
2:7b2b268ea49c | 137 | cjc_chn); |
Mahesh Phalke |
2:7b2b268ea49c | 138 | cjc_temperature = rtd_sensor.convertResistanceToTemperature(rtd_resistance); |
Mahesh Phalke |
2:7b2b268ea49c | 139 | #else |
Mahesh Phalke |
2:7b2b268ea49c | 140 | cjc_temperature = get_ntc_thermistor_temperature(dev, cjc_sample, cjc_chn); |
Mahesh Phalke |
2:7b2b268ea49c | 141 | #endif |
Mahesh Phalke |
2:7b2b268ea49c | 142 | |
Mahesh Phalke |
2:7b2b268ea49c | 143 | /* Get the CJC temperature */ |
Mahesh Phalke |
2:7b2b268ea49c | 144 | *cjc_temp = cjc_temperature; |
Mahesh Phalke |
2:7b2b268ea49c | 145 | |
Mahesh Phalke |
2:7b2b268ea49c | 146 | /* NOTE The simplest approach of adding the CJC temperature to TC temperature is taken here. |
Mahesh Phalke |
2:7b2b268ea49c | 147 | * A better method is to convert RTD back to thermocouple mV, and add that to TC value |
Mahesh Phalke |
2:7b2b268ea49c | 148 | * then do the thermocouple to degC conversion. |
Mahesh Phalke |
2:7b2b268ea49c | 149 | * */ |
Mahesh Phalke |
2:7b2b268ea49c | 150 | return (tc_temperature + cjc_temperature); |
Mahesh Phalke |
2:7b2b268ea49c | 151 | } |