Main Page | File List | Globals

main.c

Go to the documentation of this file.
00001 
00025 #include <iom16.h>                    
00026 #include <inavr.h>                    
00027 
00028 // Global variables
00029 double        accumulator       = 0;  
00030 double        Vin               = 0;  
00031 short         temp              = 0;  
00032 short         samples           = 0;  
00033         
00034 // Function prototypes
00035 void init_adc(void);                  
00036 void init_counter(void);              
00037 void oversampled(void);  
00038              
00040 #pragma vector=ADC_vect
00041 __interrupt void ADCinterrupt(void)
00042 {
00043   accumulator += ADC;
00044   samples++;
00045 }
00046 
00047 /! Enables ADC Interrupt enabled clk/64 AREF=VREF Single ended input
00048    on ADC0 Right adjusted answer */
00049 void init_adc(void)
00050 {
00051   ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1);                                                                                                           
00052   ADMUX = (0<<REFS1)|(0<<REFS0);                                 
00053   __enable_interrupt();
00054 }
00055 
00057 void init_counter(void)
00058 {
00059   DDRD  = (1<<PD7);                    // PD7 output for PWM-signal
00060   TCCR2 = (1<<WGM20)|(1<<WGM21)|(1<<COM21)|(1<<CS21);                                                
00061   OCR2  = 128; 
00062 }
00063 
00066 void oversampled(void)
00067 {
00068   __disable_interrupt();
00069   accumulator += 5150;                 // Offset error compensation 
00070   accumulator *= 0.9993;               // Gain error compensation 
00071   temp=(int)accumulator%64;
00072   accumulator/=64;                     // Scaling the answer 
00073   if(temp>=32)
00074     {
00075       accumulator += 1;                // Round up 
00076     }
00077   Vin = (accumulator/65536)*4.910;     // Calculating 16-bit result
00078   samples     = 0;                      
00079   accumulator = 0;                      
00080   __enable_interrupt(); 
00081 } 
00082                               
00083 void main( void )
00084 {
00085   init_adc();                          // Initiate and starts the ADC
00086   init_counter();                      // Initiate and starts the Counter
00087 
00088   while(1)                             // Eternal loop
00089   {
00090     if(samples>4095)
00091       {
00092         oversampled();              
00093       }
00094   }
00095 }                                     
00096 

Generated on Wed Aug 17 13:45:22 2005 for Oversampling ADC by  doxygen 1.4.3