如上所述,如何在不将所有值添加到浮点数的情况下平均一组值,这旨在用于 8 位/16 位微,以减轻时间计算浮点数。我想出了一个方法,但我担心它比将所有内容添加到浮点数中并划分更麻烦。我将在下面附上代码。
平均值减少到 ADCsample 数组中的第一个元素
平均.c
#include <xc.h>
#include "Average.h"
#include "Initialize.h"
/*
(x+y)=((x&y)*2)+(x^y)
(x+y)/2=(x&y)+((x^y)>>1)
Does not overflow data type when done in pairs
* No need for floats
* array must have 2/4/8/16/32...values to be averaged
* Accurate rounding
*/
void Average(void)
{
x=0;
for (x=0; x<ADCsamplesize; ++x)
{
ADCsamples[x].value=ADCsampleraw[x];
ADCsamples[x].upflag=0;
}
x=0;
y=0;
z=ADCsamplesize/2;
while(z!=0)
{
if (((ADCsamples[y].value&0x01)^(ADCsamples[y+1].value&0x01))==1) //is rounding needed? (even/odd mismatch)
{
if((ADCsamples[y].upflag^ADCsamples[y+1].upflag)==1) //if ONE has been rounded before
{
if (ADCsamples[y].upflag==1) //which to round down?
{
ADCsamples[y].value--;
ADCsamples[y].upflag=0;
}
else
{
ADCsamples[y+1].value--;
ADCsamples[y+1].upflag=0;
}
}
else //round up
{
if (ADCsamples[y].value!=65535) //overflow protection
{
ADCsamples[y].value++;
}
else
{
ADCsamples[y+1].value++; //in the event of a mismatch y and y+1 cannot both be data type max
}
ADCsamples[x].upflag=1; //mark as being rounded up
}
}
else
{
ADCsamples[x].upflag=0; //as elements are reused, clear rounded flag
}
ADCsamples[x].value=(ADCsamples[y].value&ADCsamples[y+1].value)+((ADCsamples[y].value^ADCsamples[y+1].value)>>1); //bitwise average of 2 values, not possible to overflow
if (x+1!=z)
{
x++;
y=y+2;
}
else
{
z=z>>1;
x=0;
y=0;
}
}
}
平均.h
#ifndef AVERAGE_H
#define AVERAGE_H
#include <xc.h> // include processor files - each processor file is guarded.
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
unsigned char z;
unsigned char x;
unsigned char y;
unsigned char const ADCsamplesize=8;
unsigned int ADCsampleraw[]=
{
123,516,4569,3521,852,456,981,852
};
typedef struct sample{
unsigned int value;
unsigned int upflag;
}sample;
sample ADCsamples[8];
void Average(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */