Average value of the sensor ADC Algorithm

Source: Internet
Author: User

# Define adc_buffer_num 6

Int adc_value_buf [adc_buffer_num];
Int adc_index_count;

Bool adc_buf_initialized;

 

Static int get_adcvalue (struct ics_proximity_data * ICS)
{
Int I = 0;
Int J = 0;
Unsigned int adc_total = 0;
Int adc_avr_value = 0;
Unsigned int adc_index = 0;
Unsigned int adc_max = 0;
Unsigned int adc_min = 0;
Int value = 0;

Value = get_adc_value ();

Adc_index = (adc_index_count ++) % adc_buffer_num;

If (! ICS-> adc_buf_initialized ){
ICS-> adc_buf_initialized = true;
For (j = 0; j <adc_buffer_num; j ++)
Adc_value_buf [J] = value;
} Else
ICS-> adc_value_buf [adc_index] = value;

Adc_max = adc_value_buf [0];
Adc_min = adc_value_buf [0];

For (I = 0; I <adc_buffer_num; I ++ ){
Adc_total + = adc_value_buf [I];

If (adc_max <adc_value_buf [I])
Adc_max = adc_value_buf [I];

If (adc_min> adc_value_buf [I])
Adc_min = adc_value_buf [I];
}
Adc_avr_value = (adc_total-(adc_max + adc_min)/(ADC_BUFFER_NUM-2 );

If (adc_index_count = ADC_BUFFER_NUM-1)
Adc_index_count = 0;

Printk (kern_info "proximity ADC = % d", adc_avr_value );

Return adc_avr_value;
}

 

MainAlgorithm:

1. Fill all buckets with the first ADC value

2. obtain the maximum and minimum values.

3. Remove the maximum and minimum values, and calculate the average value.

4. The Buf subscript uses the remainder to fill in the corresponding lower mark Value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.