/*!
* COPYRIGHT NOTICE
* Copyright (c) 2016,CTGU-GB
* All rights reserved.
* @file main.c
* @brief MSP430F5529 platform main program
* @author CTGU-GB
#include "include.h"
double adcDataTest[20];
/****************************************************************************
* Function name: ADC_Filter(u32 num,double *adcDataStorage)
* Function description: ADC filter function
* Author: klaus Email: xcf2016a@outlook.com
* Parameter description: uint8_t num: number of input filter data
* double *adcDataStorage: filter array
* Function returns: filter result
******************************************************************************/
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(); //Disable general interrupts
LED_Init(LED_ALL); //LED light initializationOLED_Init
();
ADC_Init(ADC_CH2,ADC_VREF_3_3V,ADC_10bit); //Initialize channel, 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); //在指定位置打印
}
}
|