Connect with us

ADC program template

Discussion in 'Microcontrollers, Programming and IoT' started by electronicsLearner77, Feb 22, 2017.

Scroll to continue with content
  1. electronicsLearner77

    electronicsLearner77

    95
    1
    Jul 2, 2015
    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;
    }
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,353
    2,255
    Nov 17, 2011
    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?
     
  3. electronicsLearner77

    electronicsLearner77

    95
    1
    Jul 2, 2015
    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.
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,353
    2,255
    Nov 17, 2011
    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.
    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?

    You now have the need to understand the interrupt operation and implement it correctly. Choose the lesser evil...
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-