/* Function for configuring ADC. 
This function powers on ADC and configures it. ADC is in DISABLE state after configuration, so it should be enabled before using it. */
void nrf_adc_configure(nrf_adc_config_t *config)
/* Function for configuring ADC. 
This function powers on ADC and configures it. ADC is in DISABLE state after configuration, so it should be enabled before using it. */
void nrf_adc_configure(nrf_adc_config_t *config)
{
    uint32_t config_reg = 0;
    config_reg |= ((uint32_t)config->resolution << ADC_CONFIG_RES_Pos) & ADC_CONFIG_RES_Msk;
    config_reg |= ((uint32_t)config->scaling << ADC_CONFIG_INPSEL_Pos) & ADC_CONFIG_INPSEL_Msk;
    config_reg |= ((uint32_t)config->reference << ADC_CONFIG_REFSEL_Pos) & ADC_CONFIG_REFSEL_Msk;
    if (config->reference & ADC_CONFIG_EXTREFSEL_Msk)
    {
        config_reg |= config->reference & ADC_CONFIG_EXTREFSEL_Msk;
    }
    nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_DISABLED);
    NRF_ADC->CONFIG = config_reg | (NRF_ADC->CONFIG & ADC_CONFIG_PSEL_Msk);
}