• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

adc.c

Go to the documentation of this file.
00001 /*@{*//*@}*/
00014 
00015 /******************************************************************************/
00016 /* Includes (#include)                                                        */
00017 #include <avr/io.h>
00018 #include <util/delay.h>
00019 
00020 /* Own header files */
00021 #include "xtypes.h"
00022 #include "global.h"
00023 
00024 #define EXPORT
00025   #include "adc.h"
00026 #undef EXPORT
00027 
00028 /******************************************************************************/
00029 /* Constants (#define)                *//*@{*/
00030 
00032 #define AD_REF_EXT      0x00U
00033 
00036 #define AD_READIN_COUNT 8   /* must be <= 64 (2^10 * AD_READIN_COUNT <= 2^16) */
00037 
00038 /* End: Constants (#define)                                             *//*@}*/
00039 /******************************************************************************/
00040 
00041 /******************************************************************************/
00042 /* Macro definitions (#define)           *//*@{*/
00043 /* End: Macro definitions (#define)                                     *//*@}*/
00044 /******************************************************************************/
00045 
00046 /******************************************************************************/
00047 /* Basic types (typedef)                 *//*@{*/
00048 /* End: Basic types (typedef)                                           *//*@}*/
00049 /******************************************************************************/
00050 
00051 /******************************************************************************/
00052 /* Local constants (const)      *//*@{*/
00053 /* End: Local constants (const)                                         *//*@}*/
00054 /******************************************************************************/
00055 
00056 /******************************************************************************/
00057 /* Local variables              *//*@{*/
00058 /* End: Local variables                                                 *//*@}*/
00059 /******************************************************************************/
00060 
00061 /******************************************************************************/
00062 /* Global variables           *//*@{*/
00063 /* End: Global variables                                                *//*@}*/
00064 /******************************************************************************/
00065 
00066 /******************************************************************************/
00067 /* Prototype declarations                                                     */
00068 /* End: Prototype declarations                                                */
00069 /******************************************************************************/
00070 
00071 /******************************************************************************/
00072 /* Local functions             *//*@{*/
00073 /* End: Local functions                                                 *//*@}*/
00074 /******************************************************************************/
00075 
00076 /******************************************************************************/
00077 /* Global functions          *//*@{*/
00078 
00079 /* ****************************************************************************/
00090 /* ****************************************************************************/
00091 void adc_Init (void)
00092 {
00093   SWITCH_DDR  &= ~(1U << SWITCH_BIT);
00094   VTH_DDR     &= ~(1U << VTH_BIT);
00095   R3_DDR      &= ~(1U << R3_BIT);
00096   CSBASE_DDR  &= ~(1U << CSBASE_BIT);
00097   VREF_DDR    &= ~(1U << VREF_BIT);
00098 
00099   ADMUX  = AD_REF_EXT;
00100   ADCSRA =  (0<<ADEN)   /* AD Enable */
00101           | (0<<ADSC)   /* start conversion */
00102        // | (0<<ADFR)   /* free run */
00103           | (0<<ADIE)   /* interrupt enable */
00104           | (1<<ADPS2)
00105           | (1<<ADPS1)
00106           | (1<<ADPS0);
00107 } /* End: adc_Init() */
00108 
00109 
00110 /* ****************************************************************************/
00118 /* ****************************************************************************/
00119 UINT16 adc_Read (UINT8 u8Channel)
00120 {
00121   UINT8 i;
00122   UINT16 retval;
00123   
00124   ADMUX   = AD_REF_EXT | u8Channel; /* select channel */
00125   ADCSRA |= (1<<ADEN);              /* activate ADC */
00126   
00127   /* dummy read */
00128   ADCSRA |= (1<<ADSC);              /* read ADC */
00129   while (ADCSRA & (1<<ADSC))
00130   {
00131     asm("nop");                     /* wait for conversion end */
00132   }
00133   retval = ADCW;
00134   
00135   /* actual meassure (averaged) */
00136   retval = 0U; 
00137   for(i=0U; i<AD_READIN_COUNT; i++)
00138   {
00139     ADCSRA |= (1<<ADSC);            /* read ADC */
00140     while (ADCSRA & (1<<ADSC))
00141     {
00142       asm("nop");                   /* wait for conversion end */
00143     }
00144     retval += ADCW;                 /* add results */
00145   }
00146   
00147   ADCSRA &= ~(1<<ADEN);             /* deactivate ADC */
00148   
00149   retval /= AD_READIN_COUNT;        /* calculate average value */
00150  
00151   return retval;
00152 } /* End: adc_Read() */
00153 
00154 
00155 /* End: Global functions                                                *//*@}*/
00156 /******************************************************************************/
00157 
00158 /**** Last line of code                                                    ****/
00159 
00160 

Generated on Sun Jan 23 2011 14:05:40 for ULA replacement firmware by  doxygen 1.7.2