我有一个 ADC 中断,我想对 channel (ADCBUF0) 进行 8 次采样,然后取采样的平均值。我的代码利用标志跳出 if 语句。代码编译并且我的变量在别处初始化。有人可以告诉我为什么我没有收到 SpeedADC 的值吗???
///////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
最佳答案
您需要获得正确的降档量(以避免除法),例如 8 -> 3、16 -> 4 等。对于 8 个样本,您只需要降档 3(3 位)。
并且您需要将所有值相加到一个值中,而不是将它们放在单独的数组条目中。
SpeedADCarray += ADCBUF0; /* accumulate in a single integer, not an array */
关于c - 在 ADC 中断中使用移位和数组求平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23687553/