diff options
| author | Selene ToyKeeper | 2020-01-30 01:18:56 -0700 |
|---|---|---|
| committer | Selene ToyKeeper | 2020-01-30 01:18:56 -0700 |
| commit | f61beefe38f5118dc816d5ba28d7e41001cfe703 (patch) | |
| tree | d3de61107b29f49f4071effc89a2484de0454eec /spaghetti-monster/fsm-adc.c | |
| parent | increased effective ADC resolution by switching from continuous-lowpass to su... (diff) | |
| download | anduril-f61beefe38f5118dc816d5ba28d7e41001cfe703.tar.gz anduril-f61beefe38f5118dc816d5ba28d7e41001cfe703.tar.bz2 anduril-f61beefe38f5118dc816d5ba28d7e41001cfe703.zip | |
switched to a pseudo-rolling-average method to reduce noise, set prescaler back to 64
Diffstat (limited to 'spaghetti-monster/fsm-adc.c')
| -rw-r--r-- | spaghetti-monster/fsm-adc.c | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/spaghetti-monster/fsm-adc.c b/spaghetti-monster/fsm-adc.c index c686694..f10114f 100644 --- a/spaghetti-monster/fsm-adc.c +++ b/spaghetti-monster/fsm-adc.c @@ -87,7 +87,7 @@ inline void ADC_on() //DIDR0 |= (1 << ADC_DIDR); // FIXME: unsure how to handle for VCC pin #endif #if (ATTINY == 1634) - ACSRA |= (1 << ACD); // turn off analog comparator to save power + //ACSRA |= (1 << ACD); // turn off analog comparator to save power //ADCSRB |= (1 << ADLAR); // left-adjust flag is here instead of ADMUX #endif // enable, start, auto-retrigger, prescale @@ -134,11 +134,11 @@ static inline uint8_t calc_voltage_divider(uint16_t value) { #endif // happens every time the ADC sampler finishes a measurement -// collects an average of 64 samples, which increases effective number of -// bits from 10 to about 16 (ish, probably more like 14 really) -// (64 was chosen because it's the largest sample size which allows the -// sum to still fit into a 16-bit integer, and for speed and size reasons, -// we want to avoid doing 32-bit math) +// collects a rolling average of 64+ samples, which increases effective number +// of bits from 10 to about 16 (ish, probably more like 14 really) (64 was +// chosen because it's the largest sample size which allows the sum to still +// fit into a 16-bit integer, and for speed and size reasons, we want to avoid +// doing 32-bit math) ISR(ADC_vect) { static uint16_t adc_sum; @@ -153,8 +153,15 @@ ISR(ADC_vect) { return; } // 64 samples collected, save the result + // (actually triggers at 64 and every 32 afterward) else if (66 == adc_sample_count) { + // save the latest result adc_smooth[adc_channel] = adc_sum; + // cut sum in half and set up another half-window of samples + // (for sort of a continuous average) + // (this seems to significantly reduce noise) + adc_sum >>= 1; + adc_sample_count = 33; } // add the latest measurement to the pile else { @@ -164,8 +171,6 @@ ISR(ADC_vect) { // update the latest value adc_raw[adc_channel] = m; } - // don't worry about the running total overflowing after sample 64... - // it doesn't matter // track what woke us up, and enable deferred logic irq_adc = 1; |
