|
@@ -18,9 +18,35 @@
|
18
|
18
|
|
19
|
19
|
#include <avr/io.h>
|
20
|
20
|
#include <avr/interrupt.h>
|
|
21
|
+#include <util/atomic.h>
|
21
|
22
|
|
22
|
23
|
#include "adc.h"
|
23
|
24
|
|
|
25
|
+static uint32_t adc_value = 0;
|
|
26
|
+
|
24
|
27
|
void adcInit(void) {
|
|
28
|
+ ADMUX = (1 << MUX0); // Vcc reference, ADC1 / PB2 in
|
|
29
|
+ ADCSRA = (1 << ADEN) | (1 << ADIE) // adc and interrupt enabled
|
|
30
|
+ | (1 << ADPS2) | (1 << ADPS1) // prescaler 64
|
|
31
|
+ | (1 << ADSC); // start first conversion
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+static inline void adcFilter(uint16_t val) {
|
|
35
|
+ adc_value = val; // TODO
|
|
36
|
+}
|
|
37
|
+
|
|
38
|
+ISR(ADC_vect) {
|
|
39
|
+ // add this value to our filtered average
|
|
40
|
+ adcFilter(ADC);
|
|
41
|
+
|
|
42
|
+ // start next conversion
|
|
43
|
+ ADCSRA |= (1 << ADSC);
|
|
44
|
+}
|
25
|
45
|
|
|
46
|
+uint32_t adcGet(void) {
|
|
47
|
+ uint32_t val;
|
|
48
|
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
|
49
|
+ val = adc_value;
|
|
50
|
+ }
|
|
51
|
+ return val;
|
26
|
52
|
}
|