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.
Dependencies: tempsensors adi_console_menu platform_drivers
Diff: app/ad7124_temperature_sensor.cpp
- Revision:
- 3:f20f09251190
- Parent:
- 1:c863d7e9e272
--- a/app/ad7124_temperature_sensor.cpp Mon Feb 22 05:16:41 2021 +0000
+++ b/app/ad7124_temperature_sensor.cpp Fri Mar 19 14:34:05 2021 +0530
@@ -42,10 +42,13 @@
/******************************************************************************/
/* NTC thermistor Rsense value (in ohms) */
-#define NTC_RSENSE 10000
+#define NTC_RSENSE 10000U
+
+/* RTD Rref Resistance value (in ohms) */
+#define RTD_RREF 5110U
/* PTC thermistor Ref Resistance value (in ohms) */
-#define PTC_RREF 5110
+#define PTC_RREF 5110U
/******************************************************************************/
/******************** Variables and User Defined Data Types *******************/
@@ -61,12 +64,13 @@
/*!
* @brief Convert the ADC raw value into equivalent RTD resistance
* @param adc_raw[in]- ADC raw sample
+ * @param gain[in] - RTD gain
* @return RTD resistance value
* @note RTD is biased with constant excitation current. Below formula
* is based on ratiometric measurement, where fixed value of RTD RREF
* (reference resistor) and gain is taken into account
*/
-static float convert_adc_raw_into_rtd_resistance(int32_t adc_raw)
+static float convert_adc_raw_into_rtd_resistance(int32_t adc_raw, uint8_t gain)
{
float rtd_res;
@@ -74,7 +78,7 @@
* sensor measurement are having default bipolar mode */
rtd_res = (((float)adc_raw - (1 << (AD7124_ADC_N_BITS - 1))) *
(calibration_iout_ratio * RTD_RREF)) / ((
- AD7124_PGA_GAIN(RTD_GAIN_VALUE)) * (1 << (AD7124_ADC_N_BITS - 1)));
+ AD7124_PGA_GAIN(gain)) * (1 << (AD7124_ADC_N_BITS - 1)));
return rtd_res;
}
@@ -168,15 +172,27 @@
}
+
+/**
+ * @brief Get the RTD reference resistor value
+ * @return RTD reference resistor value
+ */
+uint32_t get_rtd_rref(void)
+{
+ return RTD_RREF;
+}
+
+
/**
* @brief Convert ADC raw value into TC temperature
* @param tc_sample[in] Raw TC sample
* @param cjc_sample[in] Raw CJC sample
* @param cjc_sensor[in] CJC sensor type
+ * @param cjc_temp[in] CJC temperature value
* @return TC temperature
*/
float get_tc_temperature(float tc_sample, float cjc_sample,
- cjc_sensor_type cjc_sensor)
+ cjc_sensor_type cjc_sensor, float *cjc_temp)
{
Thermocouple_Type_T tcSensor;
float tc_mv;
@@ -187,19 +203,23 @@
tc_temperature = tcSensor.convert(tc_mv);
if (cjc_sensor == PT100_4WIRE_RTD) {
- cjc_temperature = get_rtd_temperature(cjc_sample);
+ cjc_temperature = get_rtd_temperature(cjc_sample, RTD_4WIRE_GAIN_VALUE);
} else if (cjc_sensor == THERMISTOR_PTC_KY81_110) {
cjc_temperature = get_ptc_thermistor_temperature(cjc_sample);
- } else if (cjc_sensor == PT1000_4WIRE_RTD) {
+ } else if (cjc_sensor == PT1000_2WIRE_RTD) {
PT1000 rtd_sensor;
float rtd_resistance;
- rtd_resistance = convert_adc_raw_into_rtd_resistance(cjc_sample);
+ rtd_resistance = convert_adc_raw_into_rtd_resistance(cjc_sample,
+ RTD_PT1000_GAIN_VALUE);
cjc_temperature = rtd_sensor.convertResistanceToTemperature(rtd_resistance);
} else {
return 0;
}
+ /* Get the CJC temperature */
+ *cjc_temp = cjc_temperature;
+
/* NOTE The simplest approach of adding the CJC temperature to TC temperature is taken here.
* A better method is to convert RTD back to thermocouple mV, and add that to TC value
* then do the thermocouple to degC conversion.
@@ -211,15 +231,16 @@
/**
* @brief Convert ADC raw value into RTD temperature
* @param rtd_sample[in] Raw RTD sample
+ * @pram gain[in] RTD gain
* @return RTD temperature
* @note Fixed PT100 RTD sensor is used
*/
-float get_rtd_temperature(int32_t rtd_sample)
+float get_rtd_temperature(int32_t rtd_sample, uint8_t gain)
{
PT100 rtd_sensor;
float rtd_resistance;
- rtd_resistance = convert_adc_raw_into_rtd_resistance(rtd_sample);
+ rtd_resistance = convert_adc_raw_into_rtd_resistance(rtd_sample, gain);
return rtd_sensor.convertResistanceToTemperature(rtd_resistance);
}
@@ -259,4 +280,3 @@
return ptc_thermistor.convert(ptc_resistance);
}
-