0
\$\begingroup\$

I am reading code in C to convert analog to digital and didn't understand this part. I didn't post the whole code but this is enough to understand the point of my questions since my questions are a bit general.

void ad_isr (void) interrupt 10     /*I didnt understand when does the program calls the interrupt and why do we need an interrupt?*/
{
    lowbytevoltage = ADC0L;     highbytevoltage = ADC0H;
    voltage = highbytevoltage*256 + lowbytevoltage;
    array [b] =voltage; 

        if (b == 31)
        {
            for (c=0; c!=32; c++)
            {
                Voltage = Voltage + array[c]; 
            }
                voltage = voltage/32;

Why do we need to sum 32 consecutive voltages and then divide by 32? Also I saw in an example code from the internet

for(i=0;i<16;i++)
{
    ConvertADC();
    while(BusyADC());   
    ADCResult += (unsigned int) ReadADC();
}
ADCResult /= 16;

So I thought they may have something with each other since 32 is the double of 16

\$\endgroup\$
1
\$\begingroup\$

It's simple digital filtering. The average of 32 conversions (usually the last 32 conversions) is taken to reduce the effects of ADC input noise on the final converted result.

If electrical noise or other interference can affect the voltage going to your ADC, the converted value will also be affected and you won't get the true value you wanted. By taking multiple readings and averaging, the effect of temporary noise or of white noise can be greatly diminished or removed.

Digital filters are a subject in themselves and the actual parameters of the averaging, such as the sampling rate, interval between samples and the algorithm for combining them, against the ADC input frequency range and type of signal are deep and involved.

But for basic, low-frequency and slowly-changing signals like pot' or photo-detector sensor readings, a simple filter like this can be easy to implement to avoid action being taken on false conversions.

\$\endgroup\$
0
\$\begingroup\$

Since we don't know what the functions ConvertADC and BusyADC do we cannot say anything about why you would (or would not) need an interrupt. I would guess that ConvertADC starts the acquisition by enabling the ADC. Your main function then waits until a flag is set in the interrupt (which is read and waited for by BusyADC).

As for 16 and 32: You don't really need them. This is just for creating the mean value over 32 or 16 samples (basically a digital low pass filter). It's up to your application to decide if and how many mean samples you want (you can easily get away with 1 as well).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.