上面这段GetADCResult()是本来的AD收集,没什么改变,看看就行了
GetResult()这个是软件滤波的,10位AD的之前弄滤波总犯错,今日刚弄好。
关键在于(unsigned long)(),不加这个就总犯错!核算过程中溢出,感觉好像是核算时主动降位了
ulong GetADCResult(uchar ch){ulong AD_result;ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;_nop_(); //Must wait before inquiry_nop_();_nop_();_nop_();while (!(ADC_CONTR & ADC_FLAG));//Wait complete flagADC_CONTR &= ~ADC_FLAG; //Close ADCAD_result=ADC_RES*4+ADC_RESL; return AD_result; //Return ADC result 10bit}ulong GetResult(uchar ch) //滤波段{ulong AD_result=0,AD_result1=0;ulong AD_max,AD_min,AD_temp;char AD_i,AD_o;for(AD_i=0;AD_i<4;AD_i++){AD_min=AD_max=GetADCResult(ch);AD_result=0;for(AD_o=0;AD_o<8;AD_o++){AD_temp=GetADCResult(ch);if(AD_tempAD_max){AD_result =(unsigned long)(AD_result + (unsigned long)AD_max); AD_max=AD_temp;}else AD_result =(unsigned long)(AD_result + (unsigned long)AD_temp);}AD_result=(unsigned long)(AD_result/8);AD_result1=AD_result1+ (unsigned long)(AD_result); }AD_result1=(unsigned long)(AD_result1/4);return AD_result; //Return ADC result 10bit}
办法是根据:去除最大最小值后均匀。共采样了33次去掉9个后的成果均匀值,我自己做电压表,速度满足!假如嫌慢的话能够把AD_i<4中的这个4改小点或许把这个循环去掉。