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

Parents
  • 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;		
    }
    
Reply
  • 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;		
    }
    
Children
Related