aboutsummaryrefslogtreecommitdiff
path: root/spaghetti-monster
diff options
context:
space:
mode:
authorSelene ToyKeeper2020-01-30 01:18:56 -0700
committerSelene ToyKeeper2020-01-30 01:18:56 -0700
commitf61beefe38f5118dc816d5ba28d7e41001cfe703 (patch)
treed3de61107b29f49f4071effc89a2484de0454eec /spaghetti-monster
parentincreased effective ADC resolution by switching from continuous-lowpass to su... (diff)
downloadanduril-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')
-rw-r--r--spaghetti-monster/fsm-adc.c21
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;
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.