本帖最后由 fish001 于 2019-9-10 19:05 编辑
采样实例
/*!
* COPYRIGHT NOTICE
* Copyright (c) 2016,CTGU-GB
* All rights reserved.
*
*
* @file main.c
* MSP430F5529 平台主程序
* @author CTGU-GB
*/
#include "include.h"
double adcDataTest[20];
/*******************************************************************************
* 函数名称:ADC_Filter(u32 num,double *adcDataStorage)
* 功能说明:ADC滤波函数
* 作者:klaus 邮箱:xcf2016a@outlook.com
* 参数说明:uint8_t num 输入滤波数据个数
* double *adcDataStorage:滤波数组
* 函数返回:滤波结果
********************************************************************************/
double ADC_Filter(uint8_t num,double *adcDataStorage)
{
uint8_t i,j,k;
uint8_t noswap=1;
double adc_sum_tmp=0,adc_ave_tmp=0;
for(i=0;i<num-1;++i){
for(j=0;j<num-i-1;++j)
{
if(adcDataStorage[j]>adcDataStorage[j+1]){
adcDataStorage[j]=adcDataStorage[j]+adcDataStorage[j+1];
adcDataStorage[j+1]=adcDataStorage[j]-adcDataStorage[j+1];
adcDataStorage[j]=adcDataStorage[j]-adcDataStorage[j+1];
noswap=0;
}
}
if(noswap) break;
}
for(k=2;k<num-2;k++)adc_sum_tmp += adcDataStorage[k];
//adc_sum_tmp -= (adcDataStorage[0]+adcDataStorage[1]+adcDataStorage[num-2]+adcDataStorage[num-1]);
adc_ave_tmp=adc_sum_tmp/(num-4);
adc_sum_tmp=0;
return adc_ave_tmp;
}
void main()
{
DisableInterrupts(); //禁止总中断
LED_Init(LED_ALL); //LED灯初始化
OLED_Init();
ADC_Init(ADC_CH2,ADC_VREF_3_3V,ADC_10bit); //初始化通道,P6.1
while(1)
{
int i;
for(i=0;i<20;i++)
{
adcDataTest=ADC_ReadChanelOnce(ADC_CH2)*3.3/1023;
DELAY_MS(10);
}
double ad = ADC_Filter(20,adcDataTest);
OLED_PrintfAt(FONT_ASCII_8X16,0,0,"ADValue:\n%.3f V",ad); //在指定位置打印
}
}
|