Przeglądaj źródła

read values from adc (unfiltered)

Thomas B 3 miesięcy temu
rodzic
commit
b87f95b050
3 zmienionych plików z 31 dodań i 1 usunięć
  1. 3
    0
      sensor/include/adc.h
  2. 26
    0
      sensor/src/adc.c
  3. 2
    1
      sensor/src/usb.c

+ 3
- 0
sensor/include/adc.h Wyświetl plik

@@ -19,6 +19,9 @@
19 19
 #ifndef __ADC_H__
20 20
 #define __ADC_H__
21 21
 
22
+#include <stdint.h>
23
+
22 24
 void adcInit(void);
25
+uint32_t adcGet(void);
23 26
 
24 27
 #endif // __ADC_H__

+ 26
- 0
sensor/src/adc.c Wyświetl plik

@@ -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
 }

+ 2
- 1
sensor/src/usb.c Wyświetl plik

@@ -21,6 +21,7 @@
21 21
 #include "usbdrv.h"
22 22
 #include "osccal.c" // missing usbdrv include, so include here
23 23
 
24
+#include "adc.h"
24 25
 #include "main.h"
25 26
 
26 27
 #define CUSTOM_RQ_ECHO 0 // send back wValue and wIndex, for testing comms reliability
@@ -52,7 +53,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
52 53
         }
53 54
         return 1;
54 55
     } else if (rq->bRequest == CUSTOM_RQ_GET) {
55
-        uint32_t ldr_value = 23; // TODO
56
+        uint32_t ldr_value = adcGet();
56 57
         dataBuffer[0] = (ldr_value & 0x000000FF) >> 0;
57 58
         dataBuffer[1] = (ldr_value & 0x0000FF00) >> 8;
58 59
         dataBuffer[2] = (ldr_value & 0x00FF0000) >> 16;

Ładowanie…
Anuluj
Zapisz