Commonly used digital filter functions for single chip microcomputers

Publisher:konglingdeyuanLatest update time:2016-09-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/// ...

#define A 10       //A value can be adjusted according to actual conditions
char value;        //value is a valid value
char filter()
{
char new_value;    //new value is the current sampling value
new value="get"_ad();
if ((new_value-value>A)‖(value-new_value>A)
return value;
return new_value;
}

///////////////////////////Median value filtering/////////////////////////

#define N 11       //N value can be adjusted according to actual situation
char filter()
{
char value_buf[N];
char count,i,j,temp;
for (count=0;count {
value_buf[count]=get_ad();   //Get sampling value
delay();
}
for (j=0;j  //The sampling values ​​are arranged from small to large, and the sorting is done by bubbling method
{
for (i=0;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];    //Take the middle value
}

////////////////////////////Arithmetic average filtering//////////////////////

#define N 12
char filter()
{
int sum="0";
for(count=0;count {
sum+=get_ad();
delay();
}
return (char)(sum/N);
}


/////////////////////Extremum average filtering   ///////////////////////

#define N 11       //N value can be adjusted according to actual situation
int sum="0";

char filter()
{
char value_buf[N];
char count,i,j,temp;
for (count=0;count {
value_buf[count]=get_ad();     //Get the sample value
delay();
}
for (j=0;j //The sample values ​​are arranged from small to large, and the sorting is done by bubbling method
{
for (i=0;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++)     //Remove the first and last number
{
sum+=value_buf[count];
delay();
}
return (char)(sum/(N-2));
}

////////////////////Moving average filter (recursive average filter)////////////

#define N 12
char value_buf[N];
char i="0";
char filter()
{
char count;
int sum="0";
value_buf[i++]=get_ad();
if(i=N) i=0 ;
for (count=0;count sum+=value_buf[count];
return (char)(sum/N)

}


///////////////////////Weighted average filtering//////////////////////
#define N 12
char code jq[N]={1,2,3,4,5,6,7,8,9,10,11,12};//Weighting coefficient table
char code sum_jq=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 {value_buf[count]=get_ad();     //Get sampling value
delay();
}
for (count=0;count sum+=value_buf[count]*jq[count];
return (char)(sum/sum_jq);
}


///////////////////////////Low-pass filter////////////////////////

#define a   0.25
char value;        //value is the existing value
char filter()
{
char new_value;    //new value is the current sampling value
new_value=get_ad();
return (a*new_value+(1-a)*value);
}

Reference address:Commonly used digital filter functions for single chip microcomputers

Previous article:How to use the SST MCU P4 port
Next article:Common functions of single-chip microcomputers (filtering, character processing, etc.)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号