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

ADC_IRQHandler not called

Hi, for some reason ADC_IRQHandler is never called for me.

It's fairly simple and a direct copy of the battery sensor example, so not sure what happened here. is there any board config magic i need maybe to enable ADCs?

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "nrf_soc.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "simple_uart.h"

#define SOILSENS_READ  17
#define SOILSENS_POWER 18
#define GPIO_SENSOR_DATA  19

void ADC_IRQHandler(void)
{
    simple_uart_putstring((const uint8_t *)"ADC_IRQHandler\r\n");
    uint16_t adc_result = 0;
    if (NRF_ADC->EVENTS_END != 0) {
        adc_result              = NRF_ADC->RESULT;
        NRF_ADC->TASKS_STOP     = 1;

        char  buffer[12];
        sprintf(buffer, "%i\r\n", adc_result);
        simple_uart_putstring((const uint8_t *)buffer);
    }
}


void adc_start(int analogInputPin)
{
    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)  |
        (analogInputPin                             << ADC_CONFIG_PSEL_Pos) |
        (ADC_CONFIG_EXTREFSEL_None                  << ADC_CONFIG_EXTREFSEL_Pos);

    NRF_ADC->EVENTS_END = 0;
    NRF_ADC->ENABLE     = ADC_ENABLE_ENABLE_Enabled;

    sd_nvic_ClearPendingIRQ(ADC_IRQn);
    sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW);
    sd_nvic_EnableIRQ(ADC_IRQn);

    NRF_ADC->EVENTS_END  = 0;
    NRF_ADC->TASKS_START = 1;
}

int main(void)
{
    simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, 0);

    nrf_gpio_cfg_output(SOILSENS_POWER);
    nrf_gpio_pin_set(SOILSENS_POWER);

    nrf_gpio_cfg_input(SOILSENS_READ, NRF_GPIO_PIN_PULLDOWN);
    while (true)
    {
        simple_uart_putstring((const uint8_t *)"Start:\r\n");
        adc_start(SOILSENS_READ);
        nrf_delay_ms(500);
        continue;
    }
}
Parents
  •  (analogInputPin                             << ADC_CONFIG_PSEL_Pos) |
    

    ... and in your case analogInputPin is 17. That's not how the PSEL part of that register works, it's only 8 bits wide and uses the ADC_CONFIG_PSEL_AnalogInput0 constants which I assume to be mutually exclusive (i.e. only one pin can be set). You're ORing in 0x11 << 8 which sets two bits in the mask, AIN0 and AIN4. I would think that probably upsets it.

    I think you want

    #define SOILSENS_READ ADC_CONFIG_PSEL_AnalogInput[0-7]
    

    depending on which pin you're using (and I'm not entirely sure pin 17 is a valid analog input pin, at least not on nrf51822)

  • But why will ADC_IRQHandler not be called? I mean if the config is wrong it should just result a wrong value. I will also not get into the ADC_IRQHandler. I dont know if my ADC config is wrong. Here the set up:

    NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos)| (ADC_CONFIG_PSEL_AnalogInput3 << ADC_CONFIG_PSEL_Pos)| (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos)| (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)| (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos);

Reply
  • But why will ADC_IRQHandler not be called? I mean if the config is wrong it should just result a wrong value. I will also not get into the ADC_IRQHandler. I dont know if my ADC config is wrong. Here the set up:

    NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos)| (ADC_CONFIG_PSEL_AnalogInput3 << ADC_CONFIG_PSEL_Pos)| (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos)| (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)| (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos);

Children
No Data
Related