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

Validate ADC sampling at 1 kHz

Hi,

I am a new programmer to the NRF51 and I was wondering if someone would look at my code. I am trying to program the NRF51 to take ADC samples at a frequency of 1 kHz. My code also doesn't seem to produce any values when I run it on putty and the Board leds do not light up as expected which makes me believe something in my settings is wrong.

Thanks.

#include "RTE_Components.h"             // Component selection

#include <stdbool.h>
#include <stdint.h>
#include "nrf_adc.h"
#include "nrf.h"
#include "nrf_gpio.h"
#include "nordic_common.h"
#include "nrf_delay.h"
#include "app_error.h"
#include "app_timer.h"
#include "app_util_platform.h"
#include "boards.h"
#include "bsp.h"
#include "nrf_drv_adc.h"
#include "nrf_drv_config.h"

#define ADC_BUFFER_SIZE 								10    //ADC Buffer - useful for sending large datapackets
#define APP_TIMER_PRESCALER 						0     //Prescalar Value: 2^24 / (1+Prescalar)
#define APP_TIMER_MAX_TIMERS 						2
#define APP_TIMER_OP_QUEUE_SIZE         4     
#define ADC_SAMPLING_INTERVAL     APP_TIMER_TICKS(1, APP_TIMER_PRESCALER) //ADC Sampling at 1 ms

static nrf_adc_value_t       adc_buffer[ADC_BUFFER_SIZE]; /**< ADC buffer. */
static nrf_drv_adc_channel_t m_channel_config = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_0);
	
static void adc_event_handler(nrf_drv_adc_evt_t const * p_event)  //Event Handler for ADC triggering
{
    if (p_event->type == NRF_DRV_ADC_EVT_DONE)
    {          
        uint32_t i;
        for (i = 0; i < p_event->data.done.size; i++)
        {
            NRF_LOG_PRINTF("Current sample value: %d\r\n", p_event->data.done.p_buffer[i]);
        }
    }
}

//Initialize ADC 
static void adc_init()
{
	ret_code_t ret_code;
	
	nrf_drv_adc_config_t config = NRF_DRV_ADC_DEFAULT_CONFIG;
	
	ret_code = nrf_drv_adc_init(&config, adc_event_handler);
	
	nrf_drv_adc_channel_enable(&m_channel_config);
}

//Initialize Timer
static void timer_init()
{
	
	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
	
}

//MAIN Function
int main(void)
{
		//Initialize the Application Timer Module
		timer_init();
		
		
		//Initialize ADC
		adc_init();
		
		//Sampling 
    while (true)
    {
			uint32_t i;
        for (i = 0; i < ADC_BUFFER_SIZE; i++)
        {
            // manually trigger ADC conversion
            nrf_drv_adc_sample();
            // enter into sleep mode
            __SEV();
            __WFE();
            __WFE();

            nrf_delay_ms(1);
            LEDS_INVERT(BSP_LED_0_MASK);
        }
    }
}
Parents
  • You have not initialized log module (i dunno if you want to use UART or RTT logging, but it is not initialized anyway), so your debug output is not working.

    I am not sure though you will be able to have 1k sampling with debug print at the same time... try some lower frequencies at the beginning maybe?

  • NRF_LOG_INIT() will initialize the log module.

    Around 28 characters at 115200 will take about 1/115200 * 10 bauds per byte * 28 byte = 2.4ms, so you would not be able to print out as fast as you sample, which will lead to your buffer being filled up and eventually junk will be printed. You can use RTT instead by changing NRF_LOG_USES_UART=1 to NRF_LOG_USES_RTT=1 in the precompiler symbols. RTT is much faster (uses the debug interface). You need RTT viewer to see what you print on the PC.

Reply
  • NRF_LOG_INIT() will initialize the log module.

    Around 28 characters at 115200 will take about 1/115200 * 10 bauds per byte * 28 byte = 2.4ms, so you would not be able to print out as fast as you sample, which will lead to your buffer being filled up and eventually junk will be printed. You can use RTT instead by changing NRF_LOG_USES_UART=1 to NRF_LOG_USES_RTT=1 in the precompiler symbols. RTT is much faster (uses the debug interface). You need RTT viewer to see what you print on the PC.

Children
No Data
Related