Advertisement
RuiViana

ADC_I2S.ino

Jul 26th, 2020
1,927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. /*
  2.    This is an example to read analog data at high frequency using the I2S peripheral
  3.    Run a wire between pins 27 & 32
  4.    The readings from the device will be 12bit (0-4096)
  5. */
  6. #include <driver/i2s.h>
  7.  
  8. #define I2S_SAMPLE_RATE 5000
  9. #define ADC_INPUT ADC1_CHANNEL_4                //pin 32
  10. #define READ_DELAY 1000
  11.  
  12. uint16_t adc_reading;
  13. //-------------------------------------------------------------------------------------------------
  14. void i2sInit()
  15. {
  16.   i2s_config_t i2s_config = {
  17.     .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
  18.     .sample_rate =  I2S_SAMPLE_RATE,
  19.     .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  20.     .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  21.     .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  22.     .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  23.     .dma_buf_count = 4,
  24.     .dma_buf_len = 8,
  25.     .use_apll = false,
  26.     .tx_desc_auto_clear = false,
  27.     .fixed_mclk = 0
  28.   };
  29.   i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  30.   i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT);
  31.   i2s_adc_enable(I2S_NUM_0);
  32. }
  33. //-------------------------------------------------------------------------------------------------
  34. void reader(void *pvParameters) {
  35.   uint32_t read_counter = 0;
  36.   uint64_t read_sum = 0;                                      
  37.   uint16_t offset = (int)ADC_INPUT * 0x1000 + 0xFFF;
  38.   size_t bytes_read;
  39.   while (1)
  40.   {
  41.     uint16_t buffer[2] = {0};
  42.     i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, 15);
  43.     if (bytes_read == sizeof(buffer)) {
  44.       read_sum += offset - buffer[0];
  45.       read_sum += offset - buffer[1];
  46.       read_counter++;
  47.     }
  48.     else
  49.     {
  50.       Serial.println("buffer empty");
  51.     }
  52.     if (read_counter == I2S_SAMPLE_RATE) {
  53.       adc_reading = read_sum / I2S_SAMPLE_RATE / 2;
  54.       read_counter = 0;
  55.       read_sum = 0;
  56.       i2s_adc_disable(I2S_NUM_0);
  57.       delay(READ_DELAY);
  58.       i2s_adc_enable(I2S_NUM_0);
  59.     }
  60.   }
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. void setup() {
  64.   Serial.begin(115200);
  65.   i2sInit();
  66.   // Create a task that will read the data
  67.   xTaskCreatePinnedToCore(reader, "ADC_reader", 2048, NULL, 1, NULL, 1);
  68. }
  69. //-------------------------------------------------------------------------------------------------
  70. void loop() {
  71.   Serial.printf("ADC reading: %d\n", adc_reading);
  72.   delay(READ_DELAY);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement