This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to read the value from ADC

Hi,

i want to use the adc, i use the code from here devzone.nordicsemi.com/.../is-there-an-example-code-for-using-pwm-on-the-nrf51822-with-a-softdevice and it works fine. But i don#t know how to handle the ->Result when i got 5 ADC Channels to use. It would be great if some one can tell me how to use ->Result and how to confert it to some variables

best regards Nils

  • Here is an example of ADC reading value :

    /**@brief ADC interrupt handler.
     * @details  This function will fetch the conversion result from the ADC, convert the value into
     *           percentage and send it to peer.
     */
    void ADC_IRQHandler(void)
    {
        if (NRF_ADC->EVENTS_END != 0)
        {
            uint8_t     adc_result;
            uint16_t    batt_lvl_in_milli_volts;
            uint8_t     batt_lvl_formatted;			
            uint8_t     percentage_batt_lvl;
            uint32_t    err_code;
    
            NRF_ADC->EVENTS_END     = 0;
            adc_result              = NRF_ADC->RESULT;
            NRF_ADC->TASKS_STOP     = 1;
    
            batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) ;//+ DIODE_FWD_VOLT_DROP_MILLIVOLTS;
            percentage_batt_lvl     = battery_level_in_percent(batt_lvl_in_milli_volts);
    				
    			  batt_lvl_formatted = batt_lvl_in_milli_volts/100;
    			
    			  if (batt_lvl_formatted != m_bas.battery_level_last)
            {
    					uint16_t len = sizeof(uint8_t);
    					
    					// Save new battery value
    					m_bas.battery_level_last = batt_lvl_formatted;
    					
    					// Update database
    					err_code = sd_ble_gatts_value_set(m_bas.battery_level_handles.value_handle,
    																						0,
    																						&len,
    																						&batt_lvl_formatted);					
    				}
            err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl);
            if (
                (err_code != NRF_SUCCESS)
                &&
                (err_code != NRF_ERROR_INVALID_STATE)
                &&
                (err_code != BLE_ERROR_NO_TX_BUFFERS)
                &&
                (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
            )
            {
                APP_ERROR_HANDLER(err_code);
            }
        }
    }
    
    
    /**@brief Function to make the ADC start a battery level conversion.
     */
    static void adc_start(void)
    {
        uint32_t err_code;
    
        // Configure ADC
        NRF_ADC->INTENSET   = ADC_INTENSET_END_Msk;
        NRF_ADC->CONFIG     = (ADC_CONFIG_RES_8bit                        << ADC_CONFIG_RES_Pos)     |
                              (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)  |
                              (ADC_CONFIG_REFSEL_VBG                      << ADC_CONFIG_REFSEL_Pos)  |
                              (ADC_CONFIG_PSEL_Disabled                   << ADC_CONFIG_PSEL_Pos)    |
                              (ADC_CONFIG_EXTREFSEL_None                  << ADC_CONFIG_EXTREFSEL_Pos);
        NRF_ADC->EVENTS_END = 0;
        NRF_ADC->ENABLE     = ADC_ENABLE_ENABLE_Enabled;
    
        // Enable ADC interrupt
        err_code = sd_nvic_ClearPendingIRQ(ADC_IRQn);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_nvic_EnableIRQ(ADC_IRQn);
        APP_ERROR_CHECK(err_code);
    
        NRF_ADC->EVENTS_END  = 0;    // Stop any running conversions.
        NRF_ADC->TASKS_START = 1;		
    }
    
  • Hi, thanks for the answer.

    Mhh i don't get it with this code. You also handle only one adc mesurement (adc_result), but i need to handle 5 mesurements from the ADCs but i don#t understand how to use ->ADC_RESULT

    thank you, Nils

  • Can anyone explain to me how to handle the RESULT pls?

    thanks Nils

  • The ADC will measure one input at a time, so depending on which input was selected in PSEL when you started the measurement, the RESULT when you get the END event will be the result for this particular input. No special handling of this should be needed; if you need to measure 5 different inputs, you'll just have to cycle through them.

  • You won't normally get a quicker answer by posting several replies (that aren't actual replies, but just bumping) to your own question. We try to tend to all questions here as we have time, but some days are more busy than others. Posting a complete, clear question, with detailed description on what you need to do, what exactly is the problem, and what you've already tried is the best way to get a quick reply.

Related