第五种:加权递推平均滤波法
A、方法:
是对递推平均滤波法的改进,即不同时刻的数据加以不同的权
通常是,越接近现时刻的数据,权取得越大。
给予新采样值的权系数越大,则灵敏度越高,但信号平滑度越低
B、优点:
适用于有较大纯滞后时间常数的对象
和采样周期较短的系统
C、缺点:
对于纯滞后时间常数较小,采样周期较长,变化缓慢的信号
不能迅速反应系统当前所受干扰的严重程度,滤波效果差
- #include <msp430g2452.h>
#define uchar unsigned char #define uint unsigned int const int coefficient[10]= {1,2,3,4,5,6,7,8,9,10}; const int coesum = 55;
unsigned char i,j; long resultsum,resultend,resultsum1; unsigned int result[11]; uchar ge,shi,bai,qian;
#define SER ( 1 << 1 ) //也是所谓的DS #define SRCLK ( 1 << 0 ) //也是所谓的SHCP #define RCLK ( 1 << 2 ) //也是所谓的STCP
void delay_1ms(void) { unsigned int i; for (i=0;i<1000;i++); } void delay_nms(unsigned int n) { unsigned int i=0; for (i=0;i<n;i++) delay_1ms(); }
uchar table[]={ 0xC0,0xF9,0xA4,0xB0, 0x99,0x92,0x82,0xF8, 0x80,0x90,0x88,0x83, 0xC6,0xA1,0x86,0x8E }; void Write595(uchar data) {
P2OUT &= ~RCLK; for( i=0;i<8;i++ ) { if(data&0x80) { P2OUT |= SER; } else { P2OUT &= ~SER; } P2OUT &= ~SRCLK; P2OUT |= SRCLK; data <<= 1; } P2OUT |= RCLK; }
void ADC_init(void) { P1SEL |= 0x01; ADC10CTL0 = SREF_1 + REFON + ADC10ON; ADC10AE0 |= 0x01; // P1.0 ADC option select ADC10CTL1 = INCH_0+ADC10SSEL_2; }
void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT P1DIR |= (BIT2|BIT3|BIT4|BIT5); P2DIR |= 0xff; ADC_init(); for(j=0;j<11;j++) { ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start while (ADC10CTL1 & ADC10BUSY); // ADC10BUSY? result[j] = ADC10MEM; } for(j=0;j<10;j++) { result[j] = result[j+1]; resultsum += result[j]*coefficient[j]; } resultsum /= coesum;
while(1) { //resultend = resultsum/30; resultend = resultsum*1500/1024; qian = resultend/1000; bai = resultend/100%10; shi = resultend/10%10; ge = resultend%10; P1OUT &= ~(( 1 << 2 )|(1<<3)|(1<<4)|(1<<5)); Write595(table[ge]); P1OUT |= ( 1 << 2 ); delay_nms(1);
P1OUT &= ~(( 1 << 2 )|(1<<3)|(1<<4)|(1<<5)); Write595(table[shi]); P1OUT |= ( 1 << 3 ); delay_nms(1);
P1OUT &= ~(( 1 << 2 )|(1<<3)|(1<<4)|(1<<5)); Write595(table[bai]); P1OUT |= ( 1 << 4 ); delay_nms(1);
P1OUT &= ~(( 1 << 2 )|(1<<3)|(1<<4)|(1<<5)); Write595(table[qian]); P1OUT |= ( 1 << 5 ); delay_nms(1); } }
复制代码 |