adc_internal.c

Go to the documentation of this file.
00001 
00046 #include <includes.h>
00047 
00048 //Variables
00049 static volatile ADCI_FILTER adci_filters[4]; 
00050 static ADCI_CHAN adci_sched[4]; 
00051 static ADCI_STATUS adci_status = ADCI_DONE; 
00052 static int adci_sched_index = -1; 
00053 static int adci_sched_size = 0; 
00065 void adci_init(int coeff0, int coeff1, int coeff2, int coeff3){
00066   adci_init_channel(ADCI_CH_0, coeff0);
00067   adci_init_channel(ADCI_CH_1, coeff1);
00068   adci_init_channel(ADCI_CH_2, coeff2);
00069   adci_init_channel(ADCI_CH_3, coeff3);
00070 }
00071 
00077 void adci_convert_all(void) //This will be called from the scheduler
00078 {
00079   if (adci_status == ADCI_NOT_DONE) { //previous conversion did not finish
00080     error_occurred(ERROR_ADCI_DNF);
00081   }
00082   adci_convert_next();
00083 }
00084 
00088 void adci_convert_next(void)
00089 {
00090   adci_sched_index++;
00091 //  printf("%i %i\n",adci_sched_index, adci_sched_size);
00092   if (adci_sched_index < adci_sched_size){
00093     adci_status = ADCI_NOT_DONE;
00094     ADCR &= (~0xFF);// & (~ADCR_RB); //clear chan select bits
00095     ADCR |= (1<<(adci_sched[adci_sched_index])) & (~ADCR_RB);
00096 //    ADCR |= (1<<adci_next) & ~ADCR_RB; 
00097 //    ADCR &= ~(~(1<<adci_next) & 0x0F)& ~ADCR_RB;
00098     ADCR |= (1<<24) & (~ADCR_RB); //bit24 is the START conversion bit
00099   } else {
00100     adci_sched_index = -1;
00101     adci_status = ADCI_DONE;
00102   }
00103 }
00104 
00109 void adci_isr(void) __irq
00110 {
00111   int reg = ADGDR;
00112   int chan = (reg>>24) & (0x7);
00113   int result = (reg>>6) & (0x3FF);
00114 
00115   adci_efilter_add(&(adci_filters[chan]), result); 
00116   
00117 
00118 //  printf("chan: %i\n",chan);
00119   VICVectAddr = 0; //Interrupt Acknowledged
00120   adci_convert_next(); 
00121 
00122 }
00123 
00129 int adci_get_result(ADCI_CHAN channel)
00130 {
00131   return adci_filters[channel].average>>14;
00132 }
00133 
00141 int adci_get_raw(ADCI_CHAN chan)
00142 {
00143   return adci_filters[chan].average;
00144 }
00145 
00151 static void adci_init_channel(ADCI_CHAN ch, int coeff)
00152 {
00153   if (coeff >= 0){
00154     adci_filters[ch].average = 0; //no initial value
00155     adci_filters[ch].count = 0;   //no values yet
00156     adci_filters[ch].coeff = coeff;   //filter coeff
00157     adci_sched[adci_sched_size] = ch;
00158     adci_sched_size++;
00159   }
00160 }
00161 
00167 volatile ADCI_FILTER* adci_get_filter(ADCI_CHAN channel)
00168 {
00169   return &adci_filters[channel];
00170 }
00171 
00180 void adci_efilter_add(volatile ADCI_FILTER* f, short num){
00181   if (num > (1<<16)){
00182     error_occurred(ERROR_ADCI_FILT_OOB);
00183   }
00184   if (f->average == 0)
00185     f->average = num << 14;
00186   else
00187     f->average = f->average - (f->average >> f->coeff) + (((int)num) << (14 - f->coeff));
00188   f->count = f->count + 1;
00189 } 
00190 
00191 
Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3