样例: ~\nRF5_SDK_15.2.0_9412b96\examples\peripheral\saadc
1. ADC 功能块图
2. ADC 计算公式
3. 于 sdk_config.h 中配置, 以及添加 nrfx_saadc.c 文件
4. nrf_drv_saadc_init 初始化
__STATIC_INLINE ret_code_t nrf_drv_saadc_init(nrf_drv_saadc_config_t const * p_config,
nrf_drv_saadc_event_handler_t event_handler)
{
if (p_config == NULL)
{
static const nrfx_saadc_config_t default_config = NRFX_SAADC_DEFAULT_CONFIG;
p_config = &default_config;
}
return nrfx_saadc_init(p_config, event_handler);
}
--------------------------------------------------------
#define NRFX_SAADC_DEFAULT_CONFIG \
{ \
.resolution = (nrf_saadc_resolution_t)NRFX_SAADC_CONFIG_RESOLUTION, \
.oversample = (nrf_saadc_oversample_t)NRFX_SAADC_CONFIG_OVERSAMPLE, \
.interrupt_priority = NRFX_SAADC_CONFIG_IRQ_PRIORITY, \
.low_power_mode = NRFX_SAADC_CONFIG_LP_MODE \
}
--------------------------------------------------------
typedef enum
{
NRF_SAADC_RESOLUTION_8BIT = SAADC_RESOLUTION_VAL_8bit, ///< 8 bit resolution.
NRF_SAADC_RESOLUTION_10BIT = SAADC_RESOLUTION_VAL_10bit, ///< 10 bit resolution.
NRF_SAADC_RESOLUTION_12BIT = SAADC_RESOLUTION_VAL_12bit, ///< 12 bit resolution.
NRF_SAADC_RESOLUTION_14BIT = SAADC_RESOLUTION_VAL_14bit ///< 14 bit resolution.
} nrf_saadc_resolution_t;
?! 在使用 NRFX_SAADC_DEFAULT_CONFIG 配置时, 不管 sdk_config.h 中设备为 8,10,12,14位时, ADC 解析率始终为 10 bits!
5. ADC 通道配置
nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
// channel_config.gain = NRF_SAADC_GAIN1;
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);
------------------------------------------------------------------
默认通道配置
#define NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P) \
{ \
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
.gain = NRF_SAADC_GAIN1_6, \ 增益: 1/6
.reference = NRF_SAADC_REFERENCE_INTERNAL, \ 内部参考电压: 0.6V
.acq_time = NRF_SAADC_ACQTIME_10US, \
.mode = NRF_SAADC_MODE_SINGLE_ENDED, \ 模式: 单端
.burst = NRF_SAADC_BURST_DISABLED, \
.pin_p = (nrf_saadc_input_t)(PIN_P), \
.pin_n = NRF_SAADC_INPUT_DISABLED \
}
6. 单次 ADC 转换 nrf_drv_saadc_sample_convert()
#define nrf_drv_saadc_sample_convert nrfx_saadc_sample_convert
函数原型
nrfx_err_t nrfx_saadc_sample_convert(uint8_t channel, nrf_saadc_value_t * p_value)
{
nrfx_err_t err_code;
if (m_cb.adc_state != NRF_SAADC_STATE_IDLE)
{
err_code = NRFX_ERROR_BUSY;
NRFX_LOG_WARNING("Function: %s error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
m_cb.adc_state = NRF_SAADC_STATE_BUSY;
nrf_saadc_int_disable(NRF_SAADC_INT_STARTED | NRF_SAADC_INT_END);
nrf_saadc_buffer_init(p_value, 1);
if (m_cb.active_channels > 1)
{
for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; ++i)
{
nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
}
}
nrf_saadc_channel_input_set(channel, m_cb.psel[channel].pselp, m_cb.psel[channel].pseln);
nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
bool result;
NRFX_WAIT_FOR(nrf_saadc_event_check(NRF_SAADC_EVENT_END), HW_TIMEOUT, 0, result);
NRFX_ASSERT(result);
nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED);
nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
NRFX_LOG_INFO("Conversion value: %d, channel %d.", *p_value, channel);
if (m_cb.active_channels > 1)
{
for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; ++i)
{
nrf_saadc_channel_input_set(i, m_cb.psel[i].pselp, m_cb.psel[i].pseln);
}
}
if (m_cb.low_power_mode)
{
nrf_saadc_int_enable(NRF_SAADC_INT_STARTED | NRF_SAADC_INT_END);
}
else
{
nrf_saadc_int_enable(NRF_SAADC_INT_END);
}
m_cb.adc_state = NRF_SAADC_STATE_IDLE;
err_code = NRFX_SUCCESS;
NRFX_LOG_WARNING("Function: %s, error code: %s.",
__func__,
NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
7. 配置 EasyDMA
#define nrf_drv_saadc_buffer_convert nrfx_saadc_buffer_convert
nrfx_err_t nrfx_saadc_buffer_convert(nrf_saadc_value_t * p_buffer, uint16_t size)
-----------------------------------------------------------------
example:
#define SAMPLES_IN_BUFFER 5
static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER];
nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
8. SAADC 正式采集函数
#define nrf_drv_saadc_sample nrfx_saadc_sample