1.在sdk_config.h中加入宏
// SAADC_ENABLED - nrf_drv_saadc - SAADC peripheral driver - legacy layer
//==========================================================
#ifndef SAADC_ENABLED
#define SAADC_ENABLED 1
#endif
// SAADC_CONFIG_RESOLUTION - Resolution
// 8 bit
// 10 bit
// 12 bit
// 14 bit
#ifndef SAADC_CONFIG_RESOLUTION
#define SAADC_CONFIG_RESOLUTION 2
#endif
// SAADC_CONFIG_OVERSAMPLE - Sample period
// Disabled
// 2x
// 4x
// 8x
// 16x
// 32x
// 64x
// 128x
// 256x
#ifndef SAADC_CONFIG_OVERSAMPLE
#define SAADC_CONFIG_OVERSAMPLE 0
#endif
// SAADC_CONFIG_LP_MODE - Enabling low power mode
#ifndef SAADC_CONFIG_LP_MODE
#define SAADC_CONFIG_LP_MODE 0
#endif
// SAADC_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// 0 (highest)
// 1
// 2
// 3
// 4
// 5
// 6
// 7
#ifndef SAADC_CONFIG_IRQ_PRIORITY
#define SAADC_CONFIG_IRQ_PRIORITY 6
#endif
// TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer
//==========================================================
#ifndef TIMER_ENABLED
#define TIMER_ENABLED 1
#endif
// TIMER_DEFAULT_CONFIG_FREQUENCY - Timer frequency if in Timer mode
// 16 MHz
// 8 MHz
// 4 MHz
// 2 MHz
// 1 MHz
// 500 kHz
// 250 kHz
// 125 kHz
// 62.5 kHz
// 31.25 kHz
#ifndef TIMER_DEFAULT_CONFIG_FREQUENCY
#define TIMER_DEFAULT_CONFIG_FREQUENCY 4
#endif
// TIMER_DEFAULT_CONFIG_MODE - Timer mode or operation
// Timer
// Counter
#ifndef TIMER_DEFAULT_CONFIG_MODE
#define TIMER_DEFAULT_CONFIG_MODE 0
#endif
// TIMER_DEFAULT_CONFIG_BIT_WIDTH - Timer counter bit width
// 16 bit
// 8 bit
// 24 bit
// 32 bit
#ifndef TIMER_DEFAULT_CONFIG_BIT_WIDTH
#define TIMER_DEFAULT_CONFIG_BIT_WIDTH 3
#endif
// TIMER_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// 0 (highest)
// 1
// 2
// 3
// 4
// 5
// 6
// 7
#ifndef TIMER_DEFAULT_CONFIG_IRQ_PRIORITY
#define TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6
#endif
#ifndef TIMER0_ENABLED
#define TIMER0_ENABLED 1
#endif
// TIMER1_ENABLED - Enable TIMER1 instance
#ifndef TIMER1_ENABLED
#define TIMER1_ENABLED 1
#endif
// TIMER2_ENABLED - Enable TIMER2 instance
#ifndef TIMER2_ENABLED
#define TIMER2_ENABLED 1
#endif
// TIMER3_ENABLED - Enable TIMER3 instance
#ifndef TIMER3_ENABLED
#define TIMER3_ENABLED 0
#endif
// TIMER4_ENABLED - Enable TIMER4 instance
#ifndef TIMER4_ENABLED
#define TIMER4_ENABLED 0
#endif
// TIMER_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef TIMER_CONFIG_LOG_ENABLED
#define TIMER_CONFIG_LOG_ENABLED 0
#endif
// TIMER_CONFIG_LOG_LEVEL - Default Severity level
// Off
// Error
// Warning
// Info
// Debug
#ifndef TIMER_CONFIG_LOG_LEVEL
#define TIMER_CONFIG_LOG_LEVEL 3
#endif
// TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix.
// Default
// Black
// Red
// Green
// Yellow
// Blue
// Magenta
// Cyan
// White
#ifndef TIMER_CONFIG_INFO_COLOR
#define TIMER_CONFIG_INFO_COLOR 0
#endif
// TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix.
// Default
// Black
// Red
// Green
// Yellow
// Blue
// Magenta
// Cyan
// White
#ifndef TIMER_CONFIG_DEBUG_COLOR
#define TIMER_CONFIG_DEBUG_COLOR 0
#endif
2.导入文件nrfx_saadc.c,nrfx_timer.c到工程中
3.引入头文件
#include "nrf_drv_timer.h"
#include "nrf_drv_saadc.h"
4.定义变量及宏
#define SAMPLES_IN_BUFFER 4
volatile uint8_t state = 1;
static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0);
static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER];
static nrf_ppi_channel_t m_ppi_channel;
static uint32_t m_adc_evt_counter;
5.事件及定时器函数
void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
ret_code_t err_code;
switch(event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
err_code = nrfx_saadc_sample();
APP_ERROR_CHECK(err_code);
break;
default:
break;
}
}
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
int i;
printf("\nADC event number: %d\n", (int)m_adc_evt_counter);
for(int i=0;idata.done.p_buffer[i]);
m_adc_evt_counter++;
}
}
6.初时化
void saadc_init(void)
{
ret_code_t err_code;
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t channel_config_7 =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN7);
err_code = nrf_drv_saadc_channel_init(0, &channel_config_7);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t channel_config_6 =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);
err_code = nrf_drv_saadc_channel_init(1, &channel_config_6);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t channel_config_5 =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN5);
err_code = nrf_drv_saadc_channel_init(2, &channel_config_5);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t channel_config_4 =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN4);
err_code = nrf_drv_saadc_channel_init(3, &channel_config_4);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
// err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
// APP_ERROR_CHECK(err_code);
}
7.在主函数中的处理
saadc_init();
saadc_sampling_event_init();
