1
1
/*
2
2
* This is an example to read analog data at high frequency using the I2S peripheral
3
3
* Run a wire between pins 27 & 32
4
- * The readings from the device will be 12bit (0-4096)
4
+ * The readings from the device will be 12bit (0-4096)
5
5
*/
6
6
#include < driver/i2s.h>
7
7
8
8
#define I2S_SAMPLE_RATE 78125
9
9
#define ADC_INPUT ADC1_CHANNEL_4 // pin 32
10
10
#define OUTPUT_PIN 27
11
11
#define OUTPUT_VALUE 3800
12
- #define READ_DELAY 10000 // microseconds
12
+ #define READ_DELAY 9000 // microseconds
13
+
14
+ uint16_t adc_reading;
13
15
14
16
void i2sInit ()
15
17
{
@@ -34,23 +36,29 @@ void i2sInit()
34
36
void reader (void *pvParameters) {
35
37
uint32_t read_counter = 0 ;
36
38
uint64_t read_sum = 0 ;
39
+ // The 4 high bits are the channel, and the data is inverted
40
+ uint16_t offset = (int )ADC_INPUT * 0x1000 + 0xFFF ;
41
+ size_t bytes_read;
37
42
while (1 ){
38
- size_t bytes_read = 0 ;
39
- uint16_t buffer = 0 ;
40
- i2s_read (I2S_NUM_0, &buffer, sizeof (buffer), &bytes_read, portMAX_DELAY);
41
- buffer = ~buffer; // The data is inverted
42
- // Serial.println(buffer % 0x1000);
43
- read_sum += buffer % 0x1000 ; // The 4 high bits are the channel
44
- read_counter++;
45
- if (bytes_read != sizeof (buffer)) Serial.println (" buffer empty!" );
43
+ uint16_t buffer[2 ] = {0 };
44
+ i2s_read (I2S_NUM_0, &buffer, sizeof (buffer), &bytes_read, 15 );
45
+ // Serial.printf("%d %d\n", offset - buffer[0], offset - buffer[1]);
46
+ if (bytes_read == sizeof (buffer)) {
47
+ read_sum += offset - buffer[0 ];
48
+ read_sum += offset - buffer[1 ];
49
+ read_counter++;
50
+ } else {
51
+ Serial.println (" buffer empty" );
52
+ }
46
53
if (read_counter == I2S_SAMPLE_RATE) {
47
- Serial.printf (" avg: %d\n " , read_sum/I2S_SAMPLE_RATE);
54
+ adc_reading = read_sum / I2S_SAMPLE_RATE / 2 ;
55
+ // Serial.printf("avg: %d millis: ", adc_reading);
56
+ // Serial.println(millis());
48
57
read_counter = 0 ;
49
58
read_sum = 0 ;
50
59
i2s_adc_disable (I2S_NUM_0);
51
60
delay (READ_DELAY);
52
61
i2s_adc_enable (I2S_NUM_0);
53
-
54
62
}
55
63
}
56
64
}
@@ -68,4 +76,8 @@ void setup() {
68
76
xTaskCreatePinnedToCore (reader, " ADC_reader" , 2048 , NULL , 1 , NULL , 1 );
69
77
}
70
78
71
- void loop () {}
79
+ void loop () {
80
+ delay (1020 );
81
+ Serial.printf (" ADC reading: %d\n " , adc_reading);
82
+ delay (READ_DELAY);
83
+ }
0 commit comments