This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ADC Interrupt and UART (delay issues)

Hi, 

I am very new to using microcontroller. I have been assigned to read an ADC Value using a timer interrupt, then UART-ing the results. The problem I am encountering is when a UART statement is present in the timer interrupt, the result is a delayed timer interrupt. For example, I am sampling a 50Hz sine wave in 12800Hz to produce 256 sample points in one period. I am able to produce the result correctly without UART, but once UART  (even a simple \n\f printf) is added, the sample points span across 3 periods. I suspect this is due to delay in sampling.

I hope i make myself clear. Is there any workaround for this? Perhaps i shouldn't place UART in a timer interrupt of 12800Hz.

 

void Timer0IntHandler(void)
{
	TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
	ADCIntClear(ADC0_BASE, 0);
		ADCProcessorTrigger(ADC0_BASE, 1);
		while(!ADCIntStatus(ADC0_BASE, 1, false))
		{}

		ADCSequenceDataGet(ADC0_BASE, 1, data);
		dataavg=(data[0]+data[1]+data[2]+data[3])/4;

		if (j==256)
		{
			return;
		}

		else
		{
			if (dataavg>2048)
					{
						ACVoltage[j]= (dataavg-2048)*3.4/2048.00;
//						UARTprintf("%d\n\r", dataavg); 
						j++;
					}

					else
					{
						ACVoltage[j]= -((2048-dataavg)*3.4/2048.00);
//						UARTprintf("%d\n\r", dataavg);

						j++;
					}

		}

}

* The UARTprintf produces delay.

  • Hello Jeunn,

    That is correct. With a sampling rate of 12800Hz, the time between two samples is ~78us. If the UARTprint is done with 115200 bps, then for 10 bits (1 Start, 8 bit data and 1 Stop bit) the time evaluates as 86us. So by the time the print is done then it would have missed the next interrupt. With 2 data on UART, there would be a 3rd sample lost as well.

    You would need to do two things
    1. Increase the baud rate
    2. Move the UART print out of the ISR and instead on sample acquisition set a flag so that UART can print.

    Regards
    Amit
  • In support of Amit's well advised guidance - you may also consider storing a suitable number of samples w/in MCU SRAM - and only then transferring those sample results as a "batch process"  - via UART.

    Should that batch method not be accepted (it really is most standard - avoids complexity) you must insure that each/every ADC conversion is made - and at the proper time.  For example - even by moving the UART Print from the ISR - you may still encounter "synchronization-like" transfer irregularities!

    You "escape" such (strict) MCU timing/peripheral coordination demands - by employing "after the fact" batch process data transfers...

    And - your system and demands upon it are likely to grow (not shrink) and this will further erode your timing margins.  Best to switch to batch - only later try to succeed with, "ADC Capture & UART Print LIVE!"