c - 在 ADC 中断中使用移位和数组求平均值

标签 c average interrupt microcontroller mplab

我有一个 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/

相关文章:

c - C 中的链表堆栈

C——传递一个二维数组作为函数参数?

python - 语料库中每个文本的平均句子长度(python3 和 nltk)

arduino - 关注 Arduino 程序中 micros() 值的漂移

c - 编辑 etc\hosts 文件

C 生产者/消费者应用程序使用 getenv() 获取环境变量不起作用

mysql - 获取行尾一列的平均值

java - 二维数组平均值 (Java)

java - 使用 Apache 发出 http 请求时如何捕获 InterruptedException?

java - 我想随时停止线程