Add ADC reading to/from voltage conversions
This commit is contained in:
+6
-20
@@ -37,28 +37,14 @@ extern DeviceStatus dev_stat;
|
||||
/**
|
||||
* Average value for ADC channel
|
||||
*
|
||||
* @param channel valid ADC channel pos ADC_POS_..., see board.h
|
||||
* @param channel valid ADC channel position using ADC_POS() macro
|
||||
*/
|
||||
static inline uint32_t adc_value(uint32_t channel)
|
||||
static inline uint32_t adc_raw_filtered(uint32_t channel)
|
||||
{
|
||||
assert(channel < NUM_ADC_CH);
|
||||
return adc_filtered[channel] >> (4 + ADC_FILTER_CONST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured voltage for ADC channel after average
|
||||
*
|
||||
* @param channel valid ADC channel position using ADC_POS() macro
|
||||
* @param vref reference voltage in millivolts
|
||||
* @param offset offset substracted from raw ADC value before applying gain
|
||||
*
|
||||
* @return voltage in millivolts
|
||||
*/
|
||||
static inline float adc_voltage(uint32_t channel, int32_t vref, int32_t offset = 0)
|
||||
{
|
||||
return (((int32_t)adc_value(channel) - offset) * vref) / 4096;
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured current/voltage for ADC channel after average and scaling
|
||||
*
|
||||
@@ -70,7 +56,7 @@ static inline float adc_voltage(uint32_t channel, int32_t vref, int32_t offset =
|
||||
*/
|
||||
static inline float adc_scaled(uint32_t channel, int32_t vref, const float gain, int32_t offset = 0)
|
||||
{
|
||||
return adc_voltage(channel, vref, offset) * (gain/1000.0);
|
||||
return adc_raw_to_voltage(adc_raw_filtered(channel) - offset, vref) * gain;
|
||||
}
|
||||
|
||||
static inline float ntc_temp(uint32_t channel, int32_t vref)
|
||||
@@ -79,7 +65,7 @@ static inline float ntc_temp(uint32_t channel, int32_t vref)
|
||||
https://www.embeddedrelated.com/showarticle/91.php
|
||||
*/
|
||||
|
||||
float v_temp = adc_voltage(channel, vref); // voltage read by ADC (mV)
|
||||
float v_temp = adc_raw_to_voltage(adc_raw_filtered(channel), vref) * 1000; // voltage read by ADC (mV)
|
||||
float rts = NTC_SERIES_RESISTOR * v_temp / (vref - v_temp); // resistance of NTC (Ohm)
|
||||
|
||||
return 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C
|
||||
@@ -222,7 +208,7 @@ void daq_update()
|
||||
#endif
|
||||
|
||||
// internal MCU temperature
|
||||
uint16_t adcval = adc_value(ADC_POS(temp_mcu)) * vref / VREFINT_VALUE;
|
||||
uint16_t adcval = adc_raw_filtered(ADC_POS(temp_mcu)) * vref / VREFINT_VALUE;
|
||||
dev_stat.internal_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) /
|
||||
(TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE;
|
||||
|
||||
@@ -330,7 +316,7 @@ void clear_adc_filtered()
|
||||
}
|
||||
uint32_t get_adc_filtered(uint32_t channel)
|
||||
{
|
||||
return adc_value(channel);
|
||||
return adc_raw_filtered(channel);
|
||||
}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
@@ -33,7 +33,7 @@ extern "C" {
|
||||
#define VREF (3300)
|
||||
#else
|
||||
// internal STM reference voltage
|
||||
#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_value(ADC_POS(vref_mcu)))
|
||||
#define VREF (VREFINT_VALUE * VREFINT_CAL / adc_raw_filtered(ADC_POS(vref_mcu)))
|
||||
#endif
|
||||
|
||||
#define ADC_GAIN(name) ((float)DT_PROP(DT_CHILD(DT_PATH(adc_inputs), name), multiplier) / \
|
||||
@@ -62,7 +62,7 @@ enum {
|
||||
};
|
||||
|
||||
/**
|
||||
* Struct to definie upper and lower limit alerts for any ADC channel
|
||||
* Struct to define upper and lower limit alerts for any ADC channel
|
||||
*/
|
||||
typedef struct {
|
||||
void (*callback)(); ///< Function to be called when limits are exceeded
|
||||
@@ -70,6 +70,32 @@ typedef struct {
|
||||
int16_t debounce_ms; ///< Milliseconds delay for triggering alert
|
||||
} AdcAlert;
|
||||
|
||||
/**
|
||||
* Convert raw ADC reading to voltage
|
||||
*
|
||||
* @param raw 12-bit ADC reading (right-aligned)
|
||||
* @param vref_mV Reference voltage in millivolts
|
||||
*
|
||||
* @return Voltage in Volts
|
||||
*/
|
||||
static inline float adc_raw_to_voltage(int32_t raw, int32_t vref_mV)
|
||||
{
|
||||
return (raw * vref_mV) / (4096.0F * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert voltage to raw ADC reading
|
||||
*
|
||||
* @param voltage Voltage in Volts
|
||||
* @param vref_mV Reference voltage in millivolts
|
||||
*
|
||||
* @return 12-bit ADC reading (right-aligned)
|
||||
*/
|
||||
static inline int32_t adc_voltage_to_raw(float voltage, int32_t vref_mV)
|
||||
{
|
||||
return voltage / vref_mV * 4096.0F * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set offset vs. actual measured value, i.e. sets zero current point.
|
||||
*
|
||||
|
||||
@@ -14,6 +14,34 @@
|
||||
|
||||
static AdcValues adcval;
|
||||
|
||||
void test_adc_voltage_to_raw()
|
||||
{
|
||||
int32_t raw;
|
||||
|
||||
raw = adc_voltage_to_raw(0.0, 3300);
|
||||
TEST_ASSERT_EQUAL_INT32(0, raw);
|
||||
|
||||
raw = adc_voltage_to_raw(3.3, 3300);
|
||||
TEST_ASSERT_EQUAL_INT32(4095, raw);
|
||||
|
||||
raw = adc_voltage_to_raw(1.65, 3300);
|
||||
TEST_ASSERT_EQUAL_INT32(2047, raw);
|
||||
}
|
||||
|
||||
void test_adc_raw_to_voltage()
|
||||
{
|
||||
float voltage;
|
||||
|
||||
voltage = adc_raw_to_voltage(0, 3300);
|
||||
TEST_ASSERT_EQUAL_FLOAT(0.0, voltage);
|
||||
|
||||
voltage = adc_raw_to_voltage(4095, 3300);
|
||||
TEST_ASSERT_FLOAT_WITHIN(0.01, 3.3, voltage);
|
||||
|
||||
voltage = adc_raw_to_voltage(2048, 3300);
|
||||
TEST_ASSERT_FLOAT_WITHIN(0.01, 1.65, voltage);
|
||||
}
|
||||
|
||||
// testing only for 2 values
|
||||
void check_filtering()
|
||||
{
|
||||
@@ -152,6 +180,9 @@ void daq_tests()
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(test_adc_voltage_to_raw);
|
||||
RUN_TEST(test_adc_raw_to_voltage);
|
||||
|
||||
RUN_TEST(check_filtering);
|
||||
|
||||
// call original daq_update function
|
||||
|
||||
Reference in New Issue
Block a user