示例#1
0
/**
 * @brief Function for main application entry.
 */
int main(void)
{
    LEDS_CONFIGURE(BSP_LED_0_MASK);
    LEDS_OFF(BSP_LED_0_MASK);

    adc_config();
    UNUSED_RETURN_VALUE(NRF_LOG_INIT());

    NRF_LOG_PRINTF("ADC example\r\n");

    while (true)
    {
        APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE));
        uint32_t i;
        for (i = 0; i < ADC_BUFFER_SIZE; i++)
        {
            // manually trigger ADC conversion
            nrf_drv_adc_sample();
            // enter into sleep mode
            __SEV();
            __WFE();
            __WFE();

            nrf_delay_ms(100);
            LEDS_INVERT(BSP_LED_0_MASK);
        }
    }
}
/**
 * @brief Function to trigger ADC sampling
 */
void adc_sample(void)
{
    ret_code_t ret_code;
    uint32_t p_is_running = 0;
	
    ret_code = nrf_drv_adc_buffer_convert(adc_buffer, ADC_BUFFER_SIZE);       // Allocate buffer for ADC
    APP_ERROR_CHECK(ret_code);
	
    //Request the external high frequency crystal for best ADC accuracy. For lowest current consumption, don't request the crystal.
    sd_clock_hfclk_request();
    while(! p_is_running) {          //wait for the hfclk to be available
        sd_clock_hfclk_is_running((&p_is_running));
    }  
	
    for (uint32_t i = 0; i < ADC_BUFFER_SIZE; i++)
    {
        while((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) == ADC_BUSY_BUSY_Busy) {}   //Wait until the ADC is finished sampling
        NRF_LOG_INFO("Start sampling ... \r\n");
        nrf_drv_adc_sample();        // Trigger ADC conversion
    }					
}
示例#3
0
/**
 * @brief ADC interrupt handler.
 * Prints ADC results on hardware UART and over BLE via the NUS service.
 */
static void adc_event_handler(nrf_drv_adc_evt_t const * p_event)
{
    uint8_t adc_result[ADC_BUFFER_SIZE*2];
	
    if (p_event->type == NRF_DRV_ADC_EVT_DONE)
    {
        adc_event_counter++;
        NRF_LOG_PRINTF("  adc event counter: %d\r\n", adc_event_counter);
        for (uint32_t i = 0; i < p_event->data.done.size; i++)
        {
            NRF_LOG_PRINTF("ADC value channel %d: %d\r\n", (i % number_of_adc_channels), p_event->data.done.p_buffer[i]);
            adc_result[(i*2)] = p_event->data.done.p_buffer[i] >> 8;
            adc_result[(i*2)+1] = p_event->data.done.p_buffer[i];
        }
        if(ADC_BUFFER_SIZE <= 10)
        {
            ble_nus_string_send(&m_nus, &adc_result[0], ADC_BUFFER_SIZE*2);
        }	
        APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE));
        LEDS_INVERT(BSP_LED_3_MASK);
    }