请问一下,我用PIC单片机的AD转换来测直流电压(0~5V),然后用数码管来显示(三位)

请问一下,我用PIC单片机的AD转换来测直流电压(0~5V),然后用数码管来显示(三位),第1张

加个数字滤波程序,理论就是:采AD值N次(如20次),存在内存里,然后程序将这20个数从低到高排列,取中间一位作为算出的AD值,每0.5s(或其它延时时间)进行一次数字滤波,这样得出的AD值就稳定了,这是中值滤波法,当然还有限幅滤波(就是设电压上下限)、平均数(去最大值和最小值,再求平均值)等数字滤波法,单片机做检测电压值一般都要用数字滤波。

给你个C语言的中值滤波法:

#define N 10

typedef unsigned int u16

u16 voltage_filter(void)

{

char count,i,j

static u16 value_buf[N]

u16 sum=0,temp

for(count=0count<Ncount++)

{

value_buf[count] = GetADCValue(5)

}

for(j=0j<N-1j++)

{

for(i=0i<N-ji++)

{

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=1count<N-1count++)

sum+=value_buf[count]

return (sum/(N-2))

}

上面u(k)=kpe(k)-u1(k)应该是有问题的,应该是u(k)=kpe(k)+u1(k)

程序照着写就可以了。

double U,U1

double Um

double Kp,Ki

void Control(double e)

{

U1+=Ki*e

if(U1>Um)U=Um

else if(U1<-Um)U=-Um

else

{

U+=Kp*e+U1

if (U>Um)U=Um

if(u<-Um)U=-Um

}

}

short h[], short y[])

{

int i, j, sum for (j = 0j <100j++) {

sum = 0

for (i = 0i <32i++)

sum += x[i+j] * h[i]

y[j] = sum >>15

}

}

2

void fir(short x[], short h[], short y[])

{

int i, j, sum0, sum1

short x0,x1,h0,h1 for (j = 0j <100j+=2) {

sum0 = 0

sum1 = 0

x0 = x[j]

for (i = 0i <32i+=2){

x1 = x[j+i+1]

h0 = h[i]

sum0 += x0 * h0

sum1 += x1 * h0

x0 = x[j+i+2]

h1 = h[i+1]

sum0 += x1 * h1

sum1 += x0 * h1

}

y[j] = sum0 >>15

y[j+1] = sum1 >>15

}

}

3

void fir(short x[], short h[], short y[])

{

int i, j, sum0, sum1

short x0,x1,x2,x3,x4,x5,x6,x7,h0,h1,h2,h3,h4,h5,h6,h7 for (j = 0j <100j+=2) {

sum0 = 0

sum1 = 0

x0 = x[j]

for (i = 0i <32i+=8){

x1 = x[j+i+1]

h0 = h[i]

sum0 += x0 * h0

sum1 += x1 * h0

x2 = x[j+i+2]

h1 = h[i+1]

sum0 += x1 * h1

sum1 += x2 * h1

x3 = x[j+i+3]

h2 = h[i+2]

sum0 += x2 * h2

sum1 += x3 * h2

x4 = x[j+i+4]

h3 = h[i+3]

sum0 += x3 * h3

sum1 += x4 * h3

x5 = x[j+i+5]

h4 = h[i+4]

sum0 += x4 * h4

sum1 += x5 * h4

x6 = x[j+i+6]

h5 = h[i+5]

sum0 += x5 * h5

sum1 += x6 * h5

x7 = x[j+i+7]

h6 = h[i+6]

sum0 += x6 * h6

sum1 += x7 * h6

x0 = x[j+i+8]

h7 = h[i+7]

sum0 += x7 * h7

sum1 += x0 * h7

}

y[j] = sum0 >>15

y[j+1] = sum1 >>15

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11133029.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存