天天看點

nrf52832 ADC for ppi

1.在sdk_config.h中加入宏

// <e> SAADC_ENABLED - nrf_drv_saadc - SAADC peripheral driver - legacy layer

//==========================================================

#ifndef SAADC_ENABLED

#define SAADC_ENABLED 1

#endif

// <o> SAADC_CONFIG_RESOLUTION  - Resolution

// <0=> 8 bit 

// <1=> 10 bit 

// <2=> 12 bit 

// <3=> 14 bit 

#ifndef SAADC_CONFIG_RESOLUTION

#define SAADC_CONFIG_RESOLUTION 2

#endif

// <o> SAADC_CONFIG_OVERSAMPLE  - Sample period

// <0=> Disabled 

// <1=> 2x 

// <2=> 4x 

// <3=> 8x 

// <4=> 16x 

// <5=> 32x 

// <6=> 64x 

// <7=> 128x 

// <8=> 256x 

#ifndef SAADC_CONFIG_OVERSAMPLE

#define SAADC_CONFIG_OVERSAMPLE 0

#endif

// <q> SAADC_CONFIG_LP_MODE  - Enabling low power mode

#ifndef SAADC_CONFIG_LP_MODE

#define SAADC_CONFIG_LP_MODE 0

#endif

// <o> SAADC_CONFIG_IRQ_PRIORITY  - Interrupt priority

// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice

// <0=> 0 (highest) 

// <1=> 1 

// <2=> 2 

// <3=> 3 

// <4=> 4 

// <5=> 5 

// <6=> 6 

// <7=> 7 

#ifndef SAADC_CONFIG_IRQ_PRIORITY

#define SAADC_CONFIG_IRQ_PRIORITY 6

#endif

#ifndef PPI_ENABLED

#define PPI_ENABLED 1

#endif

// <e> TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer

//==========================================================

#ifndef TIMER_ENABLED

#define TIMER_ENABLED 1

#endif

// <o> TIMER_DEFAULT_CONFIG_FREQUENCY  - Timer frequency if in Timer mode

// <0=> 16 MHz 

// <1=> 8 MHz 

// <2=> 4 MHz 

// <3=> 2 MHz 

// <4=> 1 MHz 

// <5=> 500 kHz 

// <6=> 250 kHz 

// <7=> 125 kHz 

// <8=> 62.5 kHz 

// <9=> 31.25 kHz 

#ifndef TIMER_DEFAULT_CONFIG_FREQUENCY

#define TIMER_DEFAULT_CONFIG_FREQUENCY 4

#endif

// <o> TIMER_DEFAULT_CONFIG_MODE  - Timer mode or operation

// <0=> Timer 

// <1=> Counter 

#ifndef TIMER_DEFAULT_CONFIG_MODE

#define TIMER_DEFAULT_CONFIG_MODE 0

#endif

// <o> TIMER_DEFAULT_CONFIG_BIT_WIDTH  - Timer counter bit width

// <0=> 16 bit 

// <1=> 8 bit 

// <2=> 24 bit 

// <3=> 32 bit 

#ifndef TIMER_DEFAULT_CONFIG_BIT_WIDTH

#define TIMER_DEFAULT_CONFIG_BIT_WIDTH 3

#endif

// <o> TIMER_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority

// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice

// <0=> 0 (highest) 

// <1=> 1 

// <2=> 2 

// <3=> 3 

// <4=> 4 

// <5=> 5 

// <6=> 6 

// <7=> 7 

#ifndef TIMER_DEFAULT_CONFIG_IRQ_PRIORITY

#define TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6

#endif

#ifndef TIMER0_ENABLED

#define TIMER0_ENABLED 1

#endif

// <q> TIMER1_ENABLED  - Enable TIMER1 instance

#ifndef TIMER1_ENABLED

#define TIMER1_ENABLED 1

#endif

// <q> TIMER2_ENABLED  - Enable TIMER2 instance

#ifndef TIMER2_ENABLED

#define TIMER2_ENABLED 1

#endif

// <q> TIMER3_ENABLED  - Enable TIMER3 instance

#ifndef TIMER3_ENABLED

#define TIMER3_ENABLED 0

#endif

// <q> 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);

    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();

    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();