2014-05-15 72 views
0

我有一個ADC中斷,我想對通道(ADCBUF0)進行8次採樣,然後取樣本的平均值。我的代碼利用標誌跳出if語句。代碼編譯和我的變量在別處被初始化。有人能告訴我爲什麼我沒有收到SpeedADC的價值?在ADC中斷中使用位移和陣列的平均值

///////Global//////////// 
int SpeedADCcount=0; 
///////////////////////// 

SpeedADCflag=1; 
    if(SpeedADCflag==1)  //The following is meant to take a an average of the incoming ADC voltages 
    { 
     SpeedADCcount++; 
     for(i = SpeedADCcount; i < 16; i++) 
     { 
      while(!ADCON1bits.SAMP); //Sample Done?   
      ADCON1bits.SAMP=0;   //Start Converting 
      while(!ADCON1bits.DONE); //Conversion Done? Should be on next Tcy cycle 
      SpeedADCarray[i] = ADCBUF0; 
      SpeedADCflag=0; 
     } 
    } 
    if(SpeedADCcount==15) 
    { 

     SpeedADC=SpeedADCarray[i]>>4; 
     SpeedADCcount=0; 
     // Re-enable the motor if it was turned off previous 
     if((SpeedADC>246) && Flags.RunMotor==0){RunMotor();} 

     /*Go through another stage of "filtering" for any analog input voltage below 1.25volts 
+0

你在哪裏計算平均值? –

+0

我假設平均值將被定義爲SpeedADC = SpeedADCarray [i] >> SpeedADCcount – Stumpyhuck29

+0

這是將* 1 *的樣本除以2^SpeedADCcount(其中^爲「of the power of」)。 –

回答

3

你需要得到正確的降檔量(以避免分割),使得8 - > 3,16 - >如圖4所示,等。對於8個樣品,你只需要降檔3(3位)。

而且您需要將所有值彙總到一個值中,而不是將它們放在單獨的數組條目中。

SpeedADCarray += ADCBUF0; /* accumulate in a single integer, not an array */