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

NRF51822 Print Issue

Hi, Here is the piece of code that is bugging me. The code algo is at every 500ms timer2 interrupts, and the adc captures 1500 samples and stores it in an array .I always have to stop the timer2 for printing and calculations and then start when it is done. Here newsamples is array; datatype is float and "ts" is integer defined as 1500 and IDE used is KEIL uVision 5.0.

KINDLY VERIFY WHAT IS THE PROBLEM AND HAVE I ENABLED/DISABLED TIMER2 INTERRUPTS CORRECTLY?

The following is the timer2 configuration

void start_timer(void)
{		
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;  // Set the timer in Counter Mode
  NRF_TIMER2->TASKS_CLEAR = 1;               // clear the task first to be usable for later
	NRF_TIMER2->PRESCALER = 8;                             //Set prescaler. 
	NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;		 //Set counter to 16 bit resolution
	NRF_TIMER2->CC[0] = 31250;                             //Set value for TIMER2 compare register 0
		
  // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events
	NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos)	;
	//| (TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos);
  NVIC_EnableIRQ(TIMER2_IRQn);
	NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos) & TIMER_SHORTS_COMPARE0_CLEAR_Msk;	
  NRF_TIMER2->TASKS_START = 1;               // Start TIMER2
}

Following is timer2 IRQ

void TIMER2_IRQHandler(void)
{
	if(NRF_TIMER2->EVENTS_COMPARE[0])
    {
			NRF_TIMER2->EVENTS_COMPARE[0] = 0;           //Clear compare register 0 event	
				  
			nrf_adc_conversion_event_clean();
			
			i=0;
			if(send==0)
			{		
				while(i < ts)
				{
					adc_sample = nrf_adc_result_get();
					nrf_delay_us(10);
					newsamples[i]=adc_sample;
					i++;
				}
				if(i >= ts)
				{
					send=1; 	//States samples are captured
				}	
			}		
			// trigger next ADC conversion
			nrf_adc_start();
			
	}
}

Following is the Main loop

while (true)
{
			if (send==1)
			{
				//Stop Timer2 to do calculations
				 NRF_TIMER2->TASKS_STOP = 1;	  
				//PASSING ARRAY OF DATA TO 	LPF FILTER	
				filter(newsamples,ts);      
				//NVIC_DisableIRQ(ADC_IRQn);
				
				printf("Going in While\n\r");
				while(j <ts)
				{ 	
					//printf("S: %06.0f , %d\r\n",newsamples[j],j);
					newsamples[j]*=newsamples[j];      //Squaring each datapoint
					accval+=newsamples[j];             //Adding squared values 
					j++;
					}      printf("Going out of while\n\r"); send=0;j=0; NRF_TIMER2->TASKS_START = 1;   
			 }

It does not print all the newsamples values and instead prints garbage after 15th value and doesnt show the statement "Going out of while". Here is the output while1.png

But when I don't print newsamples it shows both the statements.

while2.png

Parents
  • @Curiousmech: I see 2 issue in your code. First you only start adc nrf_adc_start(); at the end of the timer timeout handler, after you already read the result out. You should start it before you read the result.

    Secondly, as mentioned in the nRF51 Reference Manual: "The ADC itself only supports one-shot operation, this means every single conversion has to be explicitly started using the START task." (chapter31 ). You need to start the ADC conversion (call nrf_adc_start() ) every time you want the ADC to sample, not just once for 1500 samples.

    Please follow the adc_simple example we provided in the SDK.

  • @Hung Bui: Thanks for your support. The issue is now resolved. Also, I would like to share that the minimum delay to be added is of 10 ms , greater than that didn't gave proper result

Reply Children
No Data
Related