| ??? 03/13/08 14:52 Read: times |
#152235 - Comments Responding to: ???'s previous message |
Personally I would declare sum as a global and then you don't have to do 8 additions every time, this is how I implement moving average filters when I use them. You subtract the oldest value from the sum and add the newest value to the sum then calculate the average. I would also suggest using a size of 8 allowing the compiler the opportunity to do a shift instead of divide(as mentioned in other posts). Using your variable names and general format:
#define ADC_BUFFER_SIZE 8
static volatile unsigned char __adc_buffer[ADC_BUFFER_SIZE];
static volatile unsigned char __adc_buffer_ptr;
static volatile unsigned int __adc_sum;
extern void InitAdcReader() {
for ( __adc_buffer_ptr = 0;
__adc_buffer_ptr < ADC_BUFFER_SIZE;
__adc_buffer_ptr++ ) {
__adc_buffer[__adc_buffer_ptr] = 0;
}
__adc_buffer_ptr = 0;
}
extern unsigned char ReadAdc(unsigned char adc_value) {
__adc_sum -= __adc_sum[__adc_buffer_ptr]; // Subtract the oldest value
__adc_buffer[__adc_buffer_ptr] = adc_value; // Insert the newest value
__adc_sum += __adc_buffer[__adc_buffer_ptr]; // Add the newest value
__adc_buffer_ptr++; // Advance the pointer
if (__adc_buffer_ptr >= ADC_BUFFER_SIZE ) {
__adc_buffer_ptr = 0;
}
adc_value = __adc_sum / ADC_BUFFER_SIZE; // Calculate the average
return (adc_value);
}
|
| Topic | Author | Date |
| Math not functioning with proper headers? | 01/01/70 00:00 | |
| horrible method | 01/01/70 00:00 | |
| Lots of issues | 01/01/70 00:00 | |
| Code Op at level 8 | 01/01/70 00:00 | |
| Try multiple steps | 01/01/70 00:00 | |
| ah hah moment arrived......omg | 01/01/70 00:00 | |
| 01/01/70 00:00 | ||
| Here is the block which now works 100% | 01/01/70 00:00 | |
| Operation question in C | 01/01/70 00:00 | |
| Because | 01/01/70 00:00 | |
| Simplify | 01/01/70 00:00 | |
| what's the point ? | 01/01/70 00:00 | |
| What's the point | 01/01/70 00:00 | |
| simpler, but | 01/01/70 00:00 | |
| Not right | 01/01/70 00:00 | |
| use shift right instead of divide | 01/01/70 00:00 | |
| If you're lucky... | 01/01/70 00:00 | |
| Use a rount trip buffer | 01/01/70 00:00 | |
| Bad names | 01/01/70 00:00 | |
| System use names ???? | 01/01/70 00:00 | |
| Common naming convention ? | 01/01/70 00:00 | |
| ISO/IEC 9899:1990... | 01/01/70 00:00 | |
| Comments | 01/01/70 00:00 | |
| same effect as a "circular array" | 01/01/70 00:00 | |
| think I like this the best | 01/01/70 00:00 | |
Use a loop with deglitching and averaging | 01/01/70 00:00 |



