由于各种干扰,MSP430在进行ADC转化时总会出现波动,为了提高数据的可靠性,可以进行软件滤波,其中均值滤波操作简单,效果良好:
//******************************************************************************
// MSP-FET430P140 Demo - ADC12, Using the Internal Reference
//******************************************************************************
#include <msp430x14x.h>
#include"lcd.h"
#include"adc.h"
int meanValueFilter(void);
void DelayXms(unsigned int i); //delay about x ms
void int2charsLcdshow1(int value); //LCD show a value from 0 to 9999
void int2charsLcdshow2(int value); //LCD show a value from 0 to 9999
void main(void)
{
int adcValue1,adcValue2;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
LcdReset(); //initialize the lac1602
while (1)
{
adcValue1 = meanValueFilter();
adcValue2 = GetAdcValue2();
int2charsLcdshow1(adcValue1);
int2charsLcdshow2(adcValue2);
DelayXms(500);
}
}
void DelayXms(unsigned int i){
unsigned int j;
for( ; i>0; i--){
for(j=0;j<200;j++);
}
}
/****************************************************************************
*函数名: int2charsLcdshow1(void) /
*作用 :将AD转化的结果,在LCD1602上进行显示 /
*返回值:无 /
*参数 :int /
/
*******************************************************************************/
void int2charsLcdshow1(int value){
int bits=0,ten = 0,hundred = 0,thousand = 0;
thousand = value/1000;
hundred = value%1000/100;
ten = value%100/10;
bits = value%10;
Disp1Char(0,0,thousand+'0');
Disp1Char(1,0,hundred+'0');
Disp1Char(2,0,ten+'0');
Disp1Char(3,0,bits+'0');
}
void int2charsLcdshow2(int value){
int bits=0,ten = 0,hundred = 0,thousand = 0;
thousand = value/1000;
hundred = value%1000/100;
ten = value%100/10;
bits = value%10;
Disp1Char(0,1,thousand+'0');
Disp1Char(1,1,hundred+'0');
Disp1Char(2,1,ten+'0');
Disp1Char(3,1,bits+'0');
}
/****************************************************************************
*函数名: meanValueFilter(void) /
*作用 :启动ADC转化之后,将采集到的10次AD数据进行求和平均,均值滤波 /
*返回值:int型 /
*参数 :无 /
/
*****************************************************************************/
int meanValueFilter(void){
unsigned int i;
float sum=0;
int meanvalue=0;
for(i=0;i<10;i++){
sum+=GetAdcValue1();
}
meanvalue = sum/10.0; //隐式类型转化,flozt->int
return meanvalue;
}