//读取一个坐标值
//连续读取READ_TIMES次数据,对这些数据升序排列,
//然后去掉最低和最高LOST_VAL个数,取平均值
#define READ_TIMES 15 //读取次数
#define LOST_VAL 5 //丢弃值
// 优化前:
u16 ADS_Read_XY(u8 xy)
{
u16 i, j;
u16 buf[READ_TIMES];
u16 sum=0;
u16 temp;
for(i=0;i<READ_TIMES;i++)
{
buf=ADS_Read_AD(xy);
}
for(i=0;i<READ_TIMES-1; i++)//排序
{
for(j=i+1;j<READ_TIMES;j++)
{
if(buf>buf[j])//升序排列
{
temp=buf;
buf=buf[j];
buf[j]=temp;
}
}
}
sum=0;
for(i=LOST_VAL;i<READ_TIMES-LOST_VAL;i++)sum+=buf;
temp=sum/(READ_TIMES-2*LOST_VAL);
return temp;
}
//优化后:
u16 ADS_Read_XY(u8 xy)
{
u8 i;
u16 buf[READ_TIMES];
u32 sum=0;
u16 T_max = 0;
u16 T_min = 0xFFFF;
for(i=0;i<READ_TIMES;i++)
{
buf=ADS_Read_AD(xy);
T_max = (buf > T_max)?buf:T_max;
T_min = (buf < T_min)?buf:T_min;
sum += buf;
}
return (u16)((sum-T_max-T_min)/(READ_TIMES-2));
} |