Code for built-in 10-bit AD conversion in STC microcontroller + [software filtering]

Publisher:朱颜素韵Latest update time:2015-07-20 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The above GetADCResult() is the original AD acquisition, there is no change, just take a look

GetResult() is software filtering. I always got errors when filtering 10-bit AD before. I just got it right today.
The key is (unsigned long)(). If you don't add this, you will always get errors! Overflow during calculation, it seems that the bit is automatically reduced during calculation
 

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 flag
    ADC_CONTR &= ~ADC_FLAG; //Close ADC
AD_result=ADC_RES*4+ADC_RESL;

    return AD_result; //Return ADC result 10bit
}
ulong GetResult(uchar ch) //Filter segment
{
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
}

The method is based on: removing the maximum and minimum values ​​and averaging. A total of 33 samples were taken and the average value of the results after removing 9 samples was obtained. I made the voltmeter myself and the speed is sufficient! If it is too slow, you can reduce the 4 in AD_i<4 or remove this loop.

Reference address:Code for built-in 10-bit AD conversion in STC microcontroller + [software filtering]

Previous article:Design of intelligent water-saving control system for solar water heater based on STC single chip microcomputer
Next article:Colorful lights comprehensive C program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号