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

Using ADC to measure voltage of battery

I've looked over all the previous posts but none of them seem to work but the most useful one I can find is this one:

devzone.nordicsemi.com/.../

The code compiles fine but seems to stop with the console saying "Starting target CPU..." at the loop in "static void lfclk_config(void)" so I changed it so that the function is now:

static void lfclk_config(void)
{
  NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
  NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_LFCLKSTART = 1;

	// Wait for the low frequency clock to start
  while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 1) {}
  NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}

and now it will run to the end so it seems to work but I was wondering where do I see the outputted value of the voltage reading assuming what I changed works?

  • Here, you do not ensure that lfclk is started. You wait in the while-loop as long as it is started, and since your code just runs past it you haven't actually started the clock. If it was started your program would be stuck in that while-loop. So, you need to investigate why the clock isn't started.

    What hardware are you using? Do you have an external crystal mounted? from nrf51_bitfields.h:

    #define CLOCK_LFCLKSRC_SRC_Pos (0UL) /*!< Position of SRC field. */

    #define CLOCK_LFCLKSRC_SRC_Msk (0x3UL << CLOCK_LFCLKSRC_SRC_Pos) /*! < Bit mask of SRC field. */

    #define CLOCK_LFCLKSRC_SRC_RC (0UL) /*!< Internal 32KiHz RC oscillator. */

    #define CLOCK_LFCLKSRC_SRC_Xtal (1UL) /*!< External 32KiHz crystal. */

    #define CLOCK_LFCLKSRC_SRC_Synth (2UL) /*!< Internal 32KiHz synthesizer from HFCLK system clock. */

    You see that you have to choose an available clock source. An external oscillator would acquire better accuracy, but for your test you can try CLOCK_LFCLKSRC_SRC_RC and change you code back to how it was from the example.

    To show the result you can use e.g UART, represent the value binary on LED's or use the hardware module overview in Keil debugger. Here you can see state of all hardware modules, so set a breakpoint after an ADC reading and see if you have some reasonable value in the ADC registers.

Related