test: add adc_convert_volt(), a function to test ADC calibration

This commit is contained in:
Roy_01
2024-03-28 11:00:56 +08:00
committed by chain40
parent 901aa2b860
commit d10a7b95cc
2 changed files with 62 additions and 1 deletions
+61
View File
@@ -1,11 +1,14 @@
#include "ads8691.h"
#include "edc20_pin_ctrl.h"
#include "nrf_log.h"
#include "nrf_spim.h"
#include "FreeRTOS.h"
#include "task.h"
#include <string.h>
/*
* ADS8691
* Features:
@@ -189,6 +192,61 @@ int read_range_sel(range_sel_t *range_sel)
return 0;
}
double adc_convert_volt(uint16_t range_sel, int32_t val_18bit)
{
// LSB[uV]
#define LSB_VREF_NP_3P000 93.75
#define LSB_VREF_NP_2P500 78.125
#define LSB_VREF_NP_1P500 48.875
#define LSB_VREF_NP_1P250 39.06
#define LSB_VREF_NP_0P625 19.53
#define LSB_VREF_P_3P000 46.875
#define LSB_VREF_P_2P500 39.06
#define LSB_VREF_P_1P500 23.43
#define LSB_VREF_P_1P250 19.53
// FULL-SCALE RANGE[V]
#define FSR_VREF_NP_3P000 24.576
#define FSR_VREF_NP_2P500 20.48
#define FSR_VREF_NP_1P500 12.288
#define FSR_VREF_NP_1P250 10.24
#define FSR_VREF_NP_0P625 5.12
#define FSR_VREF_P_3P000 12.288
#define FSR_VREF_P_2P500 10.24
#define FSR_VREF_P_1P500 6.144
#define FSR_VREF_P_1P250 5.12
double volt;
if (range_sel == VREF_NP_3P000)
volt = (double)val_18bit * LSB_VREF_NP_3P000 / 1000000 - FSR_VREF_NP_3P000 / 2;
else if (range_sel == VREF_NP_2P500)
volt = (double)val_18bit * LSB_VREF_NP_2P500 / 1000000 - FSR_VREF_NP_2P500 / 2;
else if (range_sel == VREF_NP_1P500)
volt = (double)val_18bit * LSB_VREF_NP_1P500 / 1000000 - FSR_VREF_NP_1P500 / 2;
else if (range_sel == VREF_NP_1P250)
volt = (double)val_18bit * LSB_VREF_NP_1P250 / 1000000 - FSR_VREF_NP_1P250 / 2;
else if (range_sel == VREF_NP_0P625)
volt = (double)val_18bit * LSB_VREF_NP_0P625 / 1000000 - FSR_VREF_NP_0P625 / 2;
else if (range_sel == VREF_P_3P000)
volt = (double)val_18bit * LSB_VREF_P_3P000 / 1000000 - FSR_VREF_P_3P000 / 2;
else if (range_sel == VREF_P_2P500)
volt = (double)val_18bit * LSB_VREF_P_2P500 / 1000000 - FSR_VREF_P_2P500 / 2;
else if (range_sel == VREF_P_3P000)
volt = (double)val_18bit * LSB_VREF_P_1P500 / 1000000 - FSR_VREF_P_1P500 / 2;
else if (range_sel == VREF_P_3P000)
volt = (double)val_18bit * LSB_VREF_P_1P250 / 1000000 - FSR_VREF_P_1P250 / 2;
NRF_LOG_INFO("adc_convert_volt(input_range_idx:%d, val_18bit:%d)", range_sel, val_18bit);
{
char str[32];
snprintf(str, sizeof(str), "%.16lf", volt);
NRF_LOG_INFO("adc_convert_result: %sV", str);
}
return volt;
}
int ads8691_read(uint32_t channel, int32_t *adc_val)
{
if (m_channel != channel)
@@ -198,6 +256,7 @@ int ads8691_read(uint32_t channel, int32_t *adc_val)
nrf_gpio_pin_write(ADCA0_PIN, m_channel & (0x01 << 0));
nrf_gpio_pin_write(ADCA1_PIN, m_channel & (0x01 << 1));
nrf_gpio_pin_write(ADCA2_PIN, m_channel & (0x01 << 2));
NRF_LOG_INFO("channel:%d, [A2,A1,A0]=[%d %d %d]", channel, m_channel & (0x01 << 2), m_channel & (0x01 << 1), m_channel & (0x01 << 0));
}
if (m_flush)
@@ -208,6 +267,8 @@ int ads8691_read(uint32_t channel, int32_t *adc_val)
uint32_t val = read_word();
*adc_val = val >> 14;
adc_convert_volt(m_range_sel.range_sel, *adc_val);
return 0;
}
+1 -1
View File
@@ -290,7 +290,7 @@ void spi2_write(uint32_t cs_pin, uint8_t *p_tx_buffer, uint8_t tx_buffer_length,
xSemaphoreGive(spim2_mutex);
NRF_LOG_INFO("spi(W)");
NRF_LOG_INFO("spi2(W) cs_pin(%d)", cs_pin);
NRF_LOG_HEXDUMP_INFO(p_tx_buffer, tx_buffer_length);
if (rx_buffer_length > 0)