void saadc_init(void)
{
    ret_code_t err_code;
    nrf_drv_saadc_config_t saadc_config;
    nrf_saadc_channel_config_t channel_config;


	
    //Configure SAADC
    saadc_config.low_power_mode = true;                                                   //Enable low power mode.
    saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;                                 //Set SAADC resolution to 12-bit. This will make the SAADC output values from 0 (when input voltage is 0V) to 2^12=2048 (when input voltage is 3.6V for channel gain setting of 1/6).
    saadc_config.oversample = SAADC_OVERSAMPLE;                                           //Set oversample to 4x. This will make the SAADC output a single averaged value when the SAMPLE task is triggered 4 times.
    saadc_config.interrupt_priority = APP_IRQ_PRIORITY_LOW;                               //Set SAADC interrupt to low priority.
	
    //Initialize SAADC
    err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);                         //Initialize the SAADC with configuration and callback function. The application must then implement the saadc_callback function, which will be called when SAADC interrupt is triggered
    APP_ERROR_CHECK(err_code);
		
    //Configure SAADC channel
    channel_config.reference = NRF_SAADC_REFERENCE_INTERNAL;                              //Set internal reference of fixed 0.6 volts
    channel_config.gain = NRF_SAADC_GAIN1_6;                                              //Set input gain to 1/6. The maximum SAADC input voltage is then 0.6V/(1/6)=3.6V. The single ended input range is then 0V-3.6V
    channel_config.acq_time = NRF_SAADC_ACQTIME_10US;                                     //Set acquisition time. Set low acquisition time to enable maximum sampling frequency of 200kHz. Set high acquisition time to allow maximum source resistance up to 800 kohm, see the SAADC electrical specification in the PS. 
    channel_config.mode = NRF_SAADC_MODE_SINGLE_ENDED;                                    //Set SAADC as single ended. This means it will only have the positive pin as input, and the negative pin is shorted to ground (0V) internally.
    channel_config.pin_p = NRF_SAADC_INPUT_AIN0;                                          //Select the input pin for the channel. AIN0 pin maps to physical pin P0.02.
    channel_config.pin_n = NRF_SAADC_INPUT_DISABLED;                                      //Since the SAADC is single ended, the negative pin is disabled. The negative pin is shorted to ground internally.
    channel_config.resistor_p = NRF_SAADC_RESISTOR_DISABLED;                              //Disable pullup resistor on the input pin
    channel_config.resistor_n = NRF_SAADC_RESISTOR_DISABLED;                              //Disable pulldown resistor on the input pin

	
    //Initialize SAADC channel
    err_code = nrf_drv_saadc_channel_init(0, &channel_config);                            //Initialize SAADC channel 0 with the channel configuration
    APP_ERROR_CHECK(err_code);
		
    if(SAADC_BURST_MODE)
    {
        NRF_SAADC->CH[0].CONFIG |= 0x01000000;                                            //Configure burst mode for channel 0. Burst is useful together with oversampling. When triggering the SAMPLE task in burst mode, the SAADC will sample "Oversample" number of times as fast as it can and then output a single averaged value to the RAM buffer. If burst mode is not enabled, the SAMPLE task needs to be triggered "Oversample" number of times to output a single averaged value to the RAM buffer.		
    }

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0],SAADC_SAMPLES_IN_BUFFER);    //Set SAADC buffer 1. The SAADC will start to write to this buffer
    APP_ERROR_CHECK(err_code);

    
    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1],SAADC_SAMPLES_IN_BUFFER);    //Set SAADC buffer 2. The SAADC will write to this buffer when buffer 1 is full. This will give the applicaiton time to process data in buffer 1.
    APP_ERROR_CHECK(err_code);

}
示例#2
0
文件: main.c 项目: JulianYG/WNR
/**@brief Function for configuring ADC to do battery level conversion.
 */
static void adc_configure(void)
{    
    #ifdef NRF51
    nrf_adc_config_t adc_config = NRF_ADC_CONFIG_DEFAULT;
    adc_config.scaling = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD;
    nrf_adc_configure(&adc_config); 
    nrf_adc_int_enable(ADC_INTENSET_END_Msk);
    NVIC_EnableIRQ(ADC_IRQn);
    NVIC_SetPriority(ADC_IRQn, 3);
    nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_DISABLED);
    #else //  NRF52
    ret_code_t err_code = nrf_drv_saadc_init(NULL, saadc_event_handler);
    APP_ERROR_CHECK(err_code);

    nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
    err_code = nrf_drv_saadc_channel_init(0,&config);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(&adc_buf[0],1);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(&adc_buf[1],1);
    APP_ERROR_CHECK(err_code);
    #endif //NRF51
}
示例#3
0
/**
 * @brief Function for initializing SAADC.
 */
static ret_code_t saadc_init(void)
{
    ret_code_t err_code;
    static nrf_saadc_value_t saadc_value;

    saadc_channel.gain = NRF_SAADC_GAIN1_3;

   err_code = nrf_drv_saadc_init(NULL, saadc_handler);
   if (err_code != NRF_SUCCESS)
   {
       return NRF_ERROR_INTERNAL;
   }

    nrf_gpio_pin_set(m_csense.output_pin);

    err_code = nrf_drv_saadc_channel_init(0, &saadc_channel);
    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

    err_code = nrf_drv_saadc_buffer_convert(&saadc_value, 1);
    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

    nrf_saadc_disable();

    return NRF_SUCCESS;
}
示例#4
0
void adc_init(void)
{
    nrf_saadc_channel_config_t adc_channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(MBED_SHIELD_A0_POT_1);
    nrf_drv_saadc_config_t adc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
    adc_config.resolution = NRF_SAADC_RESOLUTION_8BIT;
    
    nrf_drv_saadc_channel_init(0, &adc_channel_config);
    nrf_drv_saadc_init(&adc_config, &adc_event);
}
示例#5
0
文件: main.c 项目: xiaoma2105/SH_Repo
void saadc_init(void)
{
    ret_code_t err_code;
    nrf_saadc_channel_config_t channel_config =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_channel_init(0, &channel_config);
    APP_ERROR_CHECK(err_code);
}
示例#6
0
void analogin_init(analogin_t *obj, PinName pin)
{
    ret_code_t ret_code;
    
    NVIC_SetVector(SAADC_IRQn, (uint32_t)SAADC_IRQHandler);
    
    ret_code = nrf_drv_saadc_init(&saadc_config, analog_in_event_handler);
    MBED_ASSERT(((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_INVALID_STATE))); //NRF_ERROR_INVALID_STATE expected for multiple channels used.
    
    uint8_t saadcIn = nrf_drv_saadc_gpio_to_ain(pin);
    MBED_ASSERT(saadcIn != NRF_SAADC_INPUT_DISABLED);
    
    obj->adc     = ADC0_0; // only one instance of ADC in nRF52 SoC
    obj->adc_pin = saadcIn  - 1;
    
    nrf_saadc_channel_config_t channel_config =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(saadcIn); //Single ended, negative input to ADC shorted to GND.
    
    ret_code = nrf_drv_saadc_channel_init(obj->adc_pin, &channel_config);
    MBED_ASSERT(ret_code == NRF_SUCCESS);
}
示例#7
0
文件: analogin_api.c 项目: pan-/mbed
/** Initialize the analogin peripheral
 *
 * Configures the pin used by analogin.
 * @param obj The analogin object to initialize
 * @param pin The analogin pin name
 */
void analogin_init(analogin_t *obj, PinName pin)
{    
    MBED_ASSERT(obj);

    /* Only initialize SAADC on first pin. */
    static bool first_init = true;

    if (first_init) {

        first_init = false;

        /* Use configuration from sdk_config.h.
         * Default is: 
         *  - 12 bit.
         *  - No oversampling.
         *  - Priority 7 (lowest).
         *  - No low power mode.
         */
        nrf_drv_saadc_config_t adc_config = {
            .resolution         = (nrf_saadc_resolution_t)SAADC_CONFIG_RESOLUTION,
            .oversample         = (nrf_saadc_oversample_t)SAADC_CONFIG_OVERSAMPLE,
            .interrupt_priority = SAADC_CONFIG_IRQ_PRIORITY,
            .low_power_mode     = SAADC_CONFIG_LP_MODE
        };

        ret_code_t result = nrf_drv_saadc_init(&adc_config, analog_in_event_handler);
        MBED_ASSERT(result == NRF_SUCCESS);

        /* Register interrupt handler in vector table. */
        NVIC_SetVector(SAADC_IRQn, (uint32_t)SAADC_IRQHandler);
    }

    /* Use pinmap function to get associated channel. */
    uint32_t channel = pinmap_function(pin, PinMap_ADC);
    MBED_ASSERT(channel != (uint32_t) NC);

    /* Account for an off-by-one in Channel definition and Input definition. */
    nrf_saadc_input_t input = channel + 1;

    /* Configure channel and pin:
     *  - the 1/4 gain and VDD/4 makes the reference voltage VDD.
     */
    nrf_saadc_channel_config_t channel_config = {
        .resistor_p = NRF_SAADC_RESISTOR_DISABLED,
        .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
        .gain       = NRF_SAADC_GAIN1_4,
        .reference  = NRF_SAADC_REFERENCE_VDD4,
        .acq_time   = NRF_SAADC_ACQTIME_10US,
        .mode       = NRF_SAADC_MODE_SINGLE_ENDED,
        .burst      = NRF_SAADC_BURST_DISABLED,
        .pin_p      = input,
        .pin_n      = NRF_SAADC_INPUT_DISABLED
    };

    ret_code_t result = nrf_drv_saadc_channel_init(channel, &channel_config);
    MBED_ASSERT(result == NRF_SUCCESS);

    /* Store channel in ADC object. */
    obj->channel = channel;
}


/** Read the input voltage, represented as a float in the range [0.0, 1.0]
 *
 * @param obj The analogin object
 * @return A floating value representing the current input voltage
 */
uint16_t analogin_read_u16(analogin_t *obj)
{    
    MBED_ASSERT(obj);

    /* Default return value is 0. */
    uint16_t retval = 0;
    
    /* Read single channel, blocking. */
    nrf_saadc_value_t value = { 0 };
    ret_code_t result = nrf_drv_saadc_sample_convert(obj->channel, &value);

    /* nrf_saadc_value_t is a signed integer. Only take the absolute value. */
    if ((result == NRF_SUCCESS) && (value > 0)) {

        /* Normalize 12 bit ADC value to 16 bit Mbed ADC range. */
        uint32_t normalized = value;
        retval = (normalized * ADC_16BIT_RANGE) / ADC_12BIT_RANGE;
    }

    return retval;
}