Page 212 - Programming Microcontrollers in C
P. 212

Analog-to-Digital Converter Operation   197

                   #include “hc05b6.h”


                   unsigned adc_data[8];
                   unsigned read_adc(int);


                   main()
                   {
                       unsigned int j;

                       AD_CTST=0;
                       AD_CTST.ADON=1;


                       for(j=0;j<8;j++)
                       {
                          adc_data[j]=(read_adc(j)+adc_data[j])/2;
                          adc_data[j] >>= 1;
                          adc_data[j] += read_adc(j)>>1;

                       #asm
                          ldx j
                          lda j
                          jsr read_adc
                          add adc_data,x
                          rora
                          sta adc_data,x
                       #endasm


                       }

                   }

                              Listing 4-5: Three Different ADC Averaging Routines
                              The first attempt to read and average the data approaches the
                          problem as simple as practical. The data are read in from the ADC,
                          added to the corresponding stored data in the array adc_data, the
                          result is divided by two, and the final average is put back into the
                          proper location in the array. The following line of code is all that is
                          necessary to accomplish this task:
   207   208   209   210   211   212   213   214   215   216   217