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
#ifndef PPI_ENABLED #define PPI_ENABLED 1 #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 2.导入文件nrfx_ssadc.c,nrfx_ppi.c,nrf_drv_ppi.c,nrfx_timer.c到工程
3.引入头文件
#include "nrf_drv_saadc.h" #include "nrf_drv_ppi.h" #include "nrf_drv_timer.h"
4.定义变量及宏 #define SAMPLES_IN_BUFFER 1 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) {
}
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("ADC event number: %d\n", (int)m_adc_evt_counter);
for (i = 0; i < SAMPLES_IN_BUFFER; i++) { printf("%d", p_event->data.done.p_buffer[i]); } m_adc_evt_counter++; } } 6.初时化函数
void saadc_sampling_event_init(void) { ret_code_t err_code;
err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code);
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32; err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler); APP_ERROR_CHECK(err_code);
/* setup m_timer for compare event every 400ms */ uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 5000); nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false); nrf_drv_timer_enable(&m_timer);
uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0); uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();
/* setup ppi channel so that timer compare event is triggering sample task in SAADC */ err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel); APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_task_addr); APP_ERROR_CHECK(err_code); }
void saadc_sampling_event_enable(void) { ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
APP_ERROR_CHECK(err_code); } 7.在主函数处理 saadc_init(); saadc_sampling_event_init(); saadc_sampling_event_enable();