ADC program template

electronicsLearner77

Jul 2, 2015
306
Joined
Jul 2, 2015
Messages
306
I have configured the adc program in the following manner automatic conversion and interrupt based and i find that the interrupt is happening very frequently and i do not want at that rate. so i planned to use the following method only when i want the data i will enable the interrupts and in the rest of the program the interrupts will be OFF. Is it correct?

unsigned int localadc,adcdataavailable, global_adc;
int main(void)
{
adcinit();
while(1)
{
enableadcinterrupts();
if(adcdataavailable == TRUE)
{
localadc = global_adc;
adcdataavailable = FALSE;
}
disableadcinterrupts();

/*
remaining part of the program;
*/
}
}

interrupt adcinterrupt(void)
{
adcdataavailable = TRUE;
global_adc = ADCBUFF;
}
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
This will likely not work as expected. ADC Interrupts are allowed only during the few code lines between enableadcinterrupts() and disableadcinterrupts(). If the interrupt happens outside of this part of the code, it will not activate the interrupt srvice routine.
If you require a new ADC value only at the beginning of the loop, why not simply poll the ADC without using any interrupt routine at all?
 

electronicsLearner77

Jul 2, 2015
306
Joined
Jul 2, 2015
Messages
306
what you said is correct. Is it possible that i slightly modify the code as

if(adcdataavailable == TRUE)
{
localadc = global_adc;
adcdataavailable = FALSE;
disableadcinterrupts();
}
moving the disable interrupts inside the if loop. Initially we started with interrupts enabling and then tested completely. so, to go to polling mode it may take some time for understanding the initialization and testing again. so to avoid the rework of initialization planned the above method. Also in embedded people say you should never be in a loop. So, just tried the above method.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
Is it possible that i slightly modify the code as
Possible? Yes.
Good? Questionable.

You still call enableadcinterrupts() at the beginning of each loop which is not necessary if the ADC interrupts are already enabled. This will cause a slight overhead at runtime. Also, this program structure is not easy to understand and tehrefore prone to errors for the uninitiated.
Also in embedded people say you should never be in a loop.
Sorry, but that's nonsense. You already are within a loop: while (1) {}. For polling you simply replace this code sequence:
Code:
enableadcinterrupts();
if(adcdataavailable == TRUE)
{
localadc = global_adc;
adcdataavailable = FALSE;
}
disableadcinterrupts();
by a read call to the ADC, then evaluate any "flags" that come with the read and if the ADC data is valid assign them to localadc. If the ADC data is not valid (e.g. "ready" flag not set or any similar indication your ADC may offer) just skip the assignment of the read data to localadc to continue the loop with the old adc data.

Consider whether it is necessary at all to continue the loop if no new valid ADC data is present. Maybe there is no penalty in waiting for new valid ADC data at the beginning of each loop pass?

to go to polling mode it may take some time for understanding the initialization and testing again. so to avoid the rework of initialization planned the above method.
You now have the need to understand the interrupt operation and implement it correctly. Choose the lesser evil...
 
Top