控制/MCU
假定从8位AD中读取数据(如果是更高位的AD可定义数据类型为int),子程序为get_ad();
/* N值可根据实际情况调整
排序采用冒泡法*/
#define N 11
char filter()
{
char value_buf[N];
char count,i,j,temp;
for ( count=0;count《N;count++)
{
value_buf[count] = get_ad();
delay();
}
for (j=0;j《N-1;j++)
{
for (i=0;i《N-j;i++)
{
if ( value_buf[i]》value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
return value_buf[(N-1)/2];
}
/*
*/
#define N 12
char filter()
{
char count,i,j;
char value_buf[N];
int sum=0;
for (count=0;count《N;count++)
{
value_buf[count] = get_ad();
delay();
}
for (j=0;j《N-1;j++)
{
for (i=0;i《N-j;i++)
{
if ( value_buf[i]》value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
for(count=1;count《N-1;count++)
sum += value[count];
return (char)(sum/(N-2));
}
/*
*/
略 参考子程序1、3
/* 为加快程序处理速度假定基数为100,a=0~100 */
#define a 50
char value;
char filter()
{
char new_value;
new_value = get_ad();
return (100-a)*value + a*new_value;
}
/* coe数组为加权系数表,存在程序存储区。*/
#define N 12
char code coe[N] = {1,2,3,4,5,6,7,8,9,10,11,12};
char code sum_coe = 1+2+3+4+5+6+7+8+9+10+11+12;
char filter()
{
char count;
char value_buf[N];
int sum=0;
for (count=0,count《N;count++)
{
value_buf[count] = get_ad();
delay();
}
for (count=0,count《N;count++)
sum += value_buf[count]*coe[count];
return (char)(sum/sum_coe);
}
#define N 12
char filter()
{
char count=0;
char new_value;
new_value = get_ad();
while (value !=new_value);
{
count++;
if (count》=N) return new_value;
delay();
new_value = get_ad();
}
return value;
}
全部0条评论
快来发表一下你的评论吧 !