{"id":2965,"date":"2024-11-14T22:15:13","date_gmt":"2024-11-14T14:15:13","guid":{"rendered":"https:\/\/saludpcb.com\/zh\/?p=2965"},"modified":"2025-09-26T06:16:06","modified_gmt":"2025-09-25T22:16:06","slug":"adc-collection-esp32-dma-i2s-complete-guide","status":"publish","type":"post","link":"https:\/\/saludpcb.com\/zh\/adc-collection-esp32-dma-i2s-complete-guide\/","title":{"rendered":"\u4f7f\u7528 ESP32 DMA \u548c I2S \u9ad8\u6548\u9032\u884c ADC \u6578\u64da\u6536\u96c6 | \u5b8c\u6574\u6307\u5357"},"content":{"rendered":"\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u5728&nbsp;<strong>ESP32<\/strong>&nbsp;\u4e0a\u4f7f\u7528&nbsp;<strong>DMA<\/strong>\uff08\u76f4\u63a5\u5b58\u5132\u5668\u5b58\u53d6\uff09\u548c&nbsp;<strong>I2S<\/strong>\uff08\u4e92\u806f\u97f3\u983b\u4e32\u884c\uff09\u6280\u8853\u4f86\u9ad8\u6548\u5730\u6536\u96c6&nbsp;<strong>ADC<\/strong>\uff08\u6a21\u6578\u8f49\u63db\u5668\uff09\u6578\u64da\u3002\u901a\u904e&nbsp;<strong>ESP32 DMA<\/strong>&nbsp;\u7684\u9ad8\u6548\u6578\u64da\u50b3\u8f38\uff0c\u6211\u5011\u53ef\u4ee5\u5927\u5e45\u6e1b\u5c11&nbsp;<strong>CPU<\/strong>&nbsp;\u8ca0\u8f09\u4e26\u63d0\u5347\u6578\u64da\u541e\u5410\u91cf\uff0c\u4f7f\u5176\u66f4\u9069\u5408\u9ad8\u901f\u5ea6\u61c9\u7528\u9700\u6c42\u3002\u9019\u7a2e\u65b9\u6cd5\u986f\u8457\u589e\u5f37\u4e86 ESP32 \u5728\u9023\u7e8c\u6578\u64da\u6d41\u61c9\u7528\u4e2d\u7684\u8868\u73fe\uff0c\u5982\u97f3\u983b\u8655\u7406\u3001\u5be6\u6642\u6578\u64da\u76e3\u63a7\u4ee5\u53ca\u9ad8\u983b\u6578\u64da\u63a1\u6a23\u7b49\u61c9\u7528\u5834\u666f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Master-Efficient-ADC-Data-Collection-with-ESP32-DMA-and-I2S-The-Complete-Guide-1.jpg\" alt=\"ESP32 DMA\u00a0\" class=\"wp-image-2971\" title=\"\" srcset=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Master-Efficient-ADC-Data-Collection-with-ESP32-DMA-and-I2S-The-Complete-Guide-1.jpg 1024w, https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Master-Efficient-ADC-Data-Collection-with-ESP32-DMA-and-I2S-The-Complete-Guide-1-768x768.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>\u5167\u5bb9<\/h2><nav><ul><li class=\"\"><a href=\"#introduction\">\u7c21\u4ecb<\/a><\/li><li class=\"\"><a href=\"#why-use-dma-and-i-2-s-with-adc-on-esp-32\">ESP32 DMA \u548c I2S<\/a><\/li><li class=\"\"><a href=\"#setting-up-i-2-s-and-dma-for-adc-data-collection\">\u8a2d\u7f6e I2S \u548c DMA \u9032\u884c ADC \u6578\u64da\u6536\u96c6<\/a><\/li><li class=\"\"><a href=\"#data-collection-and-processing\">\u6578\u64da\u6536\u96c6\u8207\u8655\u7406<\/a><\/li><li class=\"\"><a href=\"#m\">\u5b8c\u6574\u7a0b\u5f0f\u78bc<\/a><\/li><li class=\"\"><a href=\"#explanation-of-the-code\">\u7a0b\u5f0f\u78bc\u89e3\u91cb<\/a><\/li><li class=\"\"><a href=\"#example-output\">\u57f7\u884c\u7d50\u679c<\/a><\/li><li class=\"\"><a href=\"#advantages-of-this-approach\">DMA \u512a\u52e2<\/a><\/li><li class=\"\"><a href=\"#real-world-applications\">\u5be6\u969b\u61c9\u7528\u5834\u666f<\/a><\/li><li class=\"\"><a href=\"#conclusion\">\u7d50\u8ad6<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction\">\u7c21\u4ecb<\/h2>\n\n\n\n<p>\u5728\u5d4c\u5165\u5f0f\u7cfb\u7d71\u4e2d\uff0c\u6578\u64da\u6536\u96c6\u7684\u6548\u7387\u5c0d\u65bc\u6027\u80fd\u81f3\u95dc\u91cd\u8981\uff0c\u7279\u5225\u662f\u7576\u4f60\u4f7f\u7528\u50cf&nbsp;<strong>ESP32<\/strong>&nbsp;\u9019\u6a23\u7684\u5fae\u63a7\u5236\u5668\u6642\u3002\u5982\u679c\u60a8\u6b63\u5728\u8655\u7406\u985e\u6bd4\u4fe1\u865f\uff0c<strong>ADC<\/strong>\uff08\u6a21\u6578\u8f49\u63db\u5668\uff09\u662f\u5c07\u9019\u4e9b\u4fe1\u865f\u8f49\u63db\u70ba\u6578\u5b57\u6578\u64da\u7684\u5fc5\u5099\u5de5\u5177\u3002\u7136\u800c\uff0c\u7576\u9700\u8981\u6536\u96c6\u5927\u91cf\u6578\u64da\u6642\uff0c<strong>ESP32<\/strong>&nbsp;\u7684&nbsp;<strong>DMA<\/strong>\uff08\u76f4\u63a5\u5b58\u5132\u5668\u8a2a\u554f\uff09\u548c&nbsp;<strong>I2S<\/strong>\uff08\u4e92\u806f\u97f3\u983b\u4e32\u884c\uff09\u5916\u8a2d\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u9019\u4e00\u904e\u7a0b\u7684\u6548\u7387\uff0c\u901a\u904e\u6e1b\u5c11&nbsp;<strong>CPU<\/strong>&nbsp;\u7684\u8ca0\u64d4\u4f86\u512a\u5316\u6578\u64da\u541e\u5410\u91cf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-use-dma-and-i-2-s-with-adc-on-esp-32\">ESP32 DMA \u548c I2S<\/h2>\n\n\n\n<p><strong>\u76f4\u63a5\u5b58\u5132\u5668\u8a2a\u554f\uff08DMA\uff09<\/strong><\/p>\n\n\n\n<p><strong>ESP32 DMA&nbsp;<\/strong>\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u5141\u8a31\u5916\u8a2d\uff08\u5982&nbsp;<strong>ADC<\/strong>\uff09\u5c07\u6578\u64da\u76f4\u63a5\u50b3\u8f38\u5230\u5167\u5b58\uff0c\u7e5e\u904e&nbsp;<strong>CPU<\/strong>\u3002\u9019\u5c0d\u65bc\u9700\u8981\u9023\u7e8c\u6216\u5927\u91cf\u6578\u64da\u6536\u96c6\u7684\u5834\u666f\u4f86\u8aaa\u975e\u5e38\u7406\u60f3\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u6a23\u672c\u50b3\u8f38\u6642\u90fd\u6253\u65b7&nbsp;<strong>CPU<\/strong>&nbsp;\u7684\u904b\u884c\u3002\u5c0d\u65bc&nbsp;<strong>ADC<\/strong>&nbsp;\u64cd\u4f5c\uff0c\u4f7f\u7528&nbsp;<strong>ESP32 DMA<\/strong>&nbsp;\u53ef\u4ee5\u78ba\u4fdd\u4f60\u53ef\u4ee5\u5c07&nbsp;<strong>ADC<\/strong>&nbsp;\u7684\u6a23\u672c\u76f4\u63a5\u50b3\u8f38\u5230\u5167\u5b58\u7de9\u885d\u5340\uff0c\u800c\u7121\u9700\u6bcf\u500b\u6a23\u672c\u90fd\u6253\u65b7&nbsp;<strong>CPU<\/strong>\u3002<\/p>\n\n\n\n<p><strong>I2S \u63a5\u53e3<\/strong><\/p>\n\n\n\n<p>\u96d6\u7136&nbsp;<strong>I2S<\/strong>&nbsp;\u50b3\u7d71\u4e0a\u662f\u7528\u4f86\u9032\u884c\u97f3\u983b\u6578\u64da\u50b3\u8f38\u7684\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u65bc\u5f9e&nbsp;<strong>ADC<\/strong>&nbsp;\u9ad8\u901f\u50b3\u8f38\u6578\u64da\u3002<strong>ESP32<\/strong>&nbsp;\u914d\u5099\u4e86\u5167\u5efa\u7684&nbsp;<strong>I2S<\/strong>\u63a5\u53e3\uff0c\u53ef\u4ee5\u914d\u7f6e\u70ba\u5404\u7a2e\u4efb\u52d9\uff0c\u5305\u62ec\u9ad8\u901f\u5ea6\u7684&nbsp;<strong>ADC<\/strong>&nbsp;\u6578\u64da\u6536\u96c6\u3002\u901a\u904e\u4f7f\u7528&nbsp;<strong>I2S<\/strong>\uff0c\u6211\u5011\u53ef\u4ee5\u5229\u7528\u5b83\u8655\u7406\u9023\u7e8c\u6578\u64da\u6d41\u7684\u80fd\u529b\uff0c\u9019\u4f7f\u5f97&nbsp;<strong>I2S<\/strong>&nbsp;\u6210\u70ba\u8207&nbsp;<strong>DMA<\/strong>&nbsp;\u914d\u5408\u7684\u7406\u60f3\u9078\u64c7\uff0c\u4ee5\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u50b3\u8f38\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"setting-up-i-2-s-and-dma-for-adc-data-collection\">\u8a2d\u7f6e I2S \u548c DMA \u9032\u884c ADC \u6578\u64da\u6536\u96c6<\/h2>\n\n\n\n<p><strong>\u6b65\u9a5f 1\uff1a\u914d\u7f6e ADC<\/strong><\/p>\n\n\n\n<p>\u5728\u8a2d\u7f6e&nbsp;<strong>DMA<\/strong>&nbsp;\u548c&nbsp;<strong>I2S<\/strong>&nbsp;\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u914d\u7f6e&nbsp;<strong>ESP32<\/strong>&nbsp;\u4e0a\u7684&nbsp;<strong>ADC<\/strong>\u3002<strong>ESP32<\/strong>&nbsp;\u64c1\u6709\u591a\u500b\u5167\u5efa\u7684&nbsp;<strong>ADC<\/strong>&nbsp;\u901a\u9053\uff0c\u53ef\u4ee5\u7528\u4f86\u5c0d\u985e\u6bd4\u4fe1\u865f\u9032\u884c\u53d6\u6a23\u3002\u4e0b\u9762\u662f\u5982\u4f55\u914d\u7f6e&nbsp;<strong>ADC<\/strong>&nbsp;\u7684\u793a\u7bc4\u4ee3\u78bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">#include \"driver\/adc.h\"\n#include \"esp_log.h\"\n\n#define ADC_WIDTH ADC_WIDTH_BIT_12\n#define ADC_SAMPLE_RATE 5000  \/\/ 5k samples per second\n\nvoid configure_adc() {\n    adc1_config_width(ADC_WIDTH);\n    adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);\n}<\/code><\/pre>\n\n\n\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07&nbsp;<strong>ADC1_CHANNEL_0<\/strong>&nbsp;\u914d\u7f6e\u70ba\u4ee5&nbsp;<strong>12 \u4f4d\u89e3\u6790\u5ea6<\/strong>&nbsp;\u9032\u884c\u53d6\u6a23\uff0c\u4e26\u8a2d\u7f6e\u8870\u6e1b\u70ba&nbsp;<strong>0dB<\/strong>\uff08\u9ed8\u8a8d\u503c\uff09\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9a5f 2\uff1a\u8a2d\u7f6e I2S \u9032\u884c\u6578\u64da\u50b3\u8f38<\/strong><\/p>\n\n\n\n<p>\u73fe\u5728&nbsp;<strong>ADC<\/strong>&nbsp;\u914d\u7f6e\u5b8c\u6210\uff0c\u6211\u5011\u5c07\u8a2d\u7f6e&nbsp;<strong>I2S<\/strong>&nbsp;\u9032\u884c\u9ad8\u901f\u6578\u64da\u50b3\u8f38\u3002<strong>I2S<\/strong>&nbsp;\u9700\u8981\u914d\u7f6e\u70ba\u5f9e&nbsp;<strong>ADC<\/strong>&nbsp;\u6536\u96c6\u6578\u64da\u4e26\u5c07\u5176\u5b58\u5132\u5230\u5167\u5b58\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">#include \"driver\/i2s.h\"\n\n#define I2S_NUM I2S_NUM_0\n#define BUFFER_SIZE 1024\n\nvoid configure_i2s() {\n    i2s_config_t i2s_config = {\n        .mode = I2S_MODE_MASTER | I2S_MODE_RX, \/\/ Set I2S as a receiver\n        .sample_rate = ADC_SAMPLE_RATE,        \/\/ Sample rate, should match ADC sample rate\n        .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,\n        .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,  \/\/ We use only one channel for ADC data\n        .communication_format = I2S_COMM_FORMAT_I2S,\n        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,\n        .dma_buf_count = 8,\n        .dma_buf_len = BUFFER_SIZE\n    };\n    i2s_driver_install(I2S_NUM, &amp;i2s_config, 0, NULL);\n\n    \/\/ Connect the I2S interface to the ADC\n    i2s_pin_config_t pin_config = {\n        .ws_io_num = I2S_PIN_NO_CHANGE,\n        .bck_io_num = I2S_PIN_NO_CHANGE,\n        .data_in_num = ADC1_CHANNEL_0\n    };\n    i2s_set_pin(I2S_NUM, &amp;pin_config);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6b65\u9a5f 3\uff1a\u914d\u7f6e DMA<\/strong><\/p>\n\n\n\n<p>\u4e00\u65e6&nbsp;<strong>I2S<\/strong>&nbsp;\u8a2d\u7f6e\u5b8c\u6210\u5f8c\uff0c\u9700\u8981\u914d\u7f6e&nbsp;<strong>DMA<\/strong>&nbsp;\u4f86\u5c07\u6578\u64da\u5f9e&nbsp;<strong>ADC<\/strong>&nbsp;\u50b3\u8f38\u5230\u5167\u5b58\uff0c\u4e26\u4e14\u4e0d\u9700\u8981&nbsp;<strong>CPU<\/strong>&nbsp;\u7684\u5e72\u9810\u3002\u60a8\u9700\u8981\u5c07&nbsp;<strong>I2S<\/strong>&nbsp;\u9a45\u52d5\u7a0b\u5e8f\u8a2d\u7f6e\u70ba\u4f7f\u7528&nbsp;<strong>DMA<\/strong>&nbsp;\u7de9\u885d\u5340\uff0c\u4e26\u78ba\u4fdd\u6578\u64da\u80fd\u5920\u76f4\u63a5\u5beb\u5165\u5167\u5b58\u3002<\/p>\n\n\n\n<p><strong>ESP32<\/strong>&nbsp;\u7684&nbsp;<strong>I2S<\/strong>&nbsp;\u9a45\u52d5\u7a0b\u5e8f\u6703\u81ea\u52d5\u7ba1\u7406&nbsp;<strong>DMA<\/strong>&nbsp;\u7de9\u885d\u5340\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u55ae\u7368\u9032\u884c&nbsp;<strong>DMA<\/strong>&nbsp;\u914d\u7f6e\uff0c\u4f46\u60a8\u4ecd\u7136\u9700\u8981\u78ba\u4fdd\u5206\u914d\u7d66&nbsp;<strong>DMA<\/strong>&nbsp;\u7de9\u885d\u5340\u7684\u5167\u5b58\u8db3\u5920\u5927\uff0c\u4ee5\u8655\u7406\u60a8\u7684\u6578\u64da\u6d41\uff0c\u907f\u514d\u7de9\u885d\u5340\u6ea2\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">#define DMA_BUFFER_SIZE 1024\nint16_t dma_buffer[DMA_BUFFER_SIZE];\n\nvoid start_adc_reading() {\n    size_t bytes_read;\n    while (1) {\n        \/\/ Read data into DMA buffer\n        i2s_read(I2S_NUM, dma_buffer, DMA_BUFFER_SIZE * sizeof(int16_t), &amp;bytes_read, portMAX_DELAY);\n\n        \/\/ Process data in dma_buffer...\n        ESP_LOGI(\"ADC\", \"Data read: %d\", dma_buffer[0]);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c<strong>i2s_read()<\/strong>&nbsp;\u51fd\u6578\u5f9e&nbsp;<strong>I2S<\/strong>&nbsp;\u63a5\u53e3\u4e2d\u8b80\u53d6\u6578\u64da\u4e26\u5c07\u5176\u5b58\u5132\u5230&nbsp;<strong>DMA<\/strong>&nbsp;\u7de9\u885d\u5340\u4e2d\u3002\u9019\u4e9b\u6578\u64da\u96a8\u5f8c\u53ef\u4ee5\u9032\u884c\u8655\u7406\uff0c\u800c\u4e0d\u9700\u8981&nbsp;<strong>CPU<\/strong>&nbsp;\u7684\u5e72\u9810\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-collection-and-processing\">\u6578\u64da\u6536\u96c6\u8207\u8655\u7406<\/h2>\n\n\n\n<p>\u4e00\u65e6\u60a8\u8a2d\u7f6e\u4e86&nbsp;<strong>DMA<\/strong>&nbsp;\u548c&nbsp;<strong>I2S<\/strong>\uff0c\u6578\u64da\u6536\u96c6\u5c31\u8b8a\u5f97\u975e\u5e38\u7c21\u55ae\u3002\u5f9e&nbsp;<strong>ADC<\/strong>&nbsp;\u6536\u96c6\u7684\u6578\u64da\u5c07\u88ab\u5b58\u5132\u5728&nbsp;<strong>DMA<\/strong>&nbsp;\u7de9\u885d\u5340\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5373\u6642\u8655\u7406\u9019\u4e9b\u6578\u64da\u6216\u5c07\u5176\u5b58\u5132\u4ee5\u4fbf\u7a0d\u5f8c\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u5faa\u74b0\uff0c\u7528\u4f86\u5f9e\u7de9\u885d\u5340\u8b80\u53d6\u4e26\u8655\u7406&nbsp;<strong>ADC<\/strong>&nbsp;\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">void process_adc_data() {\n    while (1) {\n        size_t bytes_read;\n        \/\/ Read data from DMA buffer\n        i2s_read(I2S_NUM, dma_buffer, DMA_BUFFER_SIZE * sizeof(int16_t), &amp;bytes_read, portMAX_DELAY);\n\n        \/\/ Process the data (e.g., store or analyze)\n        ESP_LOGI(\"ADC\", \"ADC Value: %d\", dma_buffer[0]);\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"m\">\u5b8c\u6574\u7a0b\u5f0f\u78bc<\/h2>\n\n\n\n<p>\u5b8c\u6574&nbsp;<a href=\"https:\/\/saludpcb.com\/zh\"><strong>ESP32<\/strong>&nbsp;\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/a>\uff0c\u5b83\u5305\u62ec&nbsp;<strong>ADC<\/strong>\u3001<strong>I2S<\/strong>\u3001<strong>DMA<\/strong>&nbsp;\u7684\u8a2d\u7f6e\u53ca\u6578\u64da\u6536\u96c6\uff0c\u4e26\u4f7f\u7528&nbsp;<strong>ESP_LOGI<\/strong>&nbsp;\u6253\u5370&nbsp;<strong>ADC<\/strong>&nbsp;\u6578\u64da\u81f3\u4e32\u5217\u76e3\u8996\u5668\uff0c\u4ee5\u4fbf\u5373\u6642\u67e5\u770b\u8b80\u53d6\u7684\u6578\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\"><code>#include &lt;stdio.h&gt;\n#include \"driver\/adc.h\"\n#include \"driver\/i2s.h\"\n#include \"esp_log.h\"\n\n#define ADC_WIDTH            ADC_WIDTH_BIT_12         \/\/ ADC resolution: 12 bits\n#define ADC_SAMPLE_RATE      5000                     \/\/ ADC sample rate: 5000 samples per second\n#define I2S_NUM              I2S_NUM_0                \/\/ I2S peripheral number\n#define BUFFER_SIZE          1024                     \/\/ DMA buffer size\n#define DMA_BUFFER_SIZE      1024                     \/\/ Length of DMA buffer\n\nstatic const char *TAG = \"ADC_I2S_DMA\";              \/\/ Log tag\n\n\/\/ DMA buffer for ADC data\nint16_t dma_buffer[DMA_BUFFER_SIZE];\n\n\/\/ Configure the ADC\nvoid configure_adc() {\n    \/\/ Set ADC width to 12 bits\n    adc1_config_width(ADC_WIDTH);\n    \n    \/\/ Set channel attenuation\n    adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_0);\n    \n    ESP_LOGI(TAG, \"ADC configured\");\n}\n\n\/\/ Configure I2S\nvoid configure_i2s() {\n    i2s_config_t i2s_config = {\n        .mode = I2S_MODE_MASTER | I2S_MODE_RX,      \/\/ I2S as master, in receive mode\n        .sample_rate = ADC_SAMPLE_RATE,             \/\/ Sample rate\n        .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \/\/ 16-bit samples\n        .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, \/\/ Single channel\n        .communication_format = I2S_COMM_FORMAT_I2S, \/\/ I2S communication format\n        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,    \/\/ Interrupt allocation\n        .dma_buf_count = 8,                          \/\/ Number of DMA buffers\n        .dma_buf_len = BUFFER_SIZE                   \/\/ DMA buffer size\n    };\n    \n    \/\/ Initialize I2S driver\n    i2s_driver_install(I2S_NUM, &amp;i2s_config, 0, NULL);\n    \n    \/\/ Configure I2S pin mapping to the ADC\n    i2s_pin_config_t pin_config = {\n        .ws_io_num = I2S_PIN_NO_CHANGE,\n        .bck_io_num = I2S_PIN_NO_CHANGE,\n        .data_in_num = ADC1_CHANNEL_0                \/\/ Connect I2S data input to ADC channel\n    };\n    \n    i2s_set_pin(I2S_NUM, &amp;pin_config);\n    \n    ESP_LOGI(TAG, \"I2S configured\");\n}\n\n\/\/ Start ADC data reading\nvoid start_adc_reading() {\n    size_t bytes_read;\n    \n    \/\/ Continuously read and log ADC data\n    while (1) {\n        \/\/ Use I2S to read data into the DMA buffer\n        i2s_read(I2S_NUM, dma_buffer, DMA_BUFFER_SIZE * sizeof(int16_t), &amp;bytes_read, portMAX_DELAY);\n\n        \/\/ Log the first sample in the buffer (or process it further if needed)\n        ESP_LOGI(TAG, \"ADC Data read: %d\", dma_buffer[0]);\n    }\n}\n\nvoid app_main() {\n    \/\/ Configure ADC and I2S\n    configure_adc();\n    configure_i2s();\n\n    \/\/ Start reading data\n    start_adc_reading();\n}<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"explanation-of-the-code\">\u7a0b\u5f0f\u78bc\u89e3\u91cb<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>configure_adc()<\/code><\/strong>\uff1a\u8a2d\u7f6e ADC \u89e3\u6790\u5ea6\u548c\u8870\u6e1b\u53c3\u6578\uff0c\u4f7f\u5176\u4ee5 12 \u4f4d\u89e3\u6790\u5ea6\u8b80\u53d6\u6578\u64da\u3002<\/li>\n\n\n\n<li><strong><code>configure_i2s()<\/code><\/strong>\uff1a\u914d\u7f6e I2S \u4f5c\u70ba\u6578\u64da\u63a5\u6536\u5668\uff0c\u8a2d\u7f6e\u53d6\u6a23\u7387\u3001\u4f4d\u6df1\u5ea6\u548c\u55ae\u901a\u9053\u683c\u5f0f\u3002\u9019\u88e1&nbsp;<code>i2s_set_pin()<\/code>&nbsp;\u5c07 I2S \u63a5\u6536\u6578\u64da\u7ba1\u8173\u8a2d\u7f6e\u70ba ADC \u901a\u9053\uff0c\u4ee5\u4fbf\u5f9e ADC \u7372\u53d6\u6578\u64da\u3002<\/li>\n\n\n\n<li><strong><code>start_adc_reading()<\/code><\/strong>\uff1a\u5229\u7528 I2S \u7684 DMA \u7de9\u885d\u5340\u5f9e ADC \u8b80\u53d6\u6578\u64da\u3002<code>i2s_read()<\/code>&nbsp;\u6703\u81ea\u52d5\u586b\u5145&nbsp;<code>dma_buffer<\/code>\uff0c\u4e14\u4e0d\u9700\u8981 CPU \u7684\u5e72\u9810\u3002\u96a8\u5f8c\u7528&nbsp;<code>ESP_LOGI()<\/code>&nbsp;\u5c07\u7b2c\u4e00\u7b46\u8b80\u53d6\u7684\u6578\u64da\u6253\u5370\u5230\u4e32\u53e3\u3002<\/li>\n\n\n\n<li><strong><code>app_main()<\/code><\/strong>\uff1a\u4e3b\u7a0b\u5e8f\uff0c\u9996\u5148\u8abf\u7528&nbsp;<code>configure_adc()<\/code>&nbsp;\u548c&nbsp;<code>configure_i2s()<\/code>&nbsp;\u4f86\u521d\u59cb\u5316 ADC \u548c I2S \u914d\u7f6e\uff0c\u7136\u5f8c\u9032\u5165&nbsp;<code>start_adc_reading()<\/code>&nbsp;\u4f86\u958b\u59cb\u6578\u64da\u6536\u96c6\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-output\">\u57f7\u884c\u7d50\u679c<\/h2>\n\n\n\n<p>\u6b64\u7a0b\u5e8f\u6703\u6301\u7e8c\u5728\u4e32\u53e3\u6253\u5370\u7531 <strong>ADC<\/strong> \u8b80\u53d6\u7684\u6578\u64da :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"><code>I (500) ADC_I2S_DMA: ADC configured\nI (500) ADC_I2S_DMA: I2S configured\nI (1000) ADC_I2S_DMA: ADC Data read: 1234\nI (1500) ADC_I2S_DMA: ADC Data read: 1250\n...<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-this-approach\">DMA \u512a\u52e2<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6e1b\u5c11 CPU \u8ca0\u8f09<\/strong>\uff1a\u5229\u7528 DMA \u5c07\u6578\u64da\u76f4\u63a5\u50b3\u8f38\u81f3\u5167\u5b58\uff0c\u4e0d\u5fc5\u4f54\u7528 CPU\uff0c\u8b93 CPU \u80fd\u5920\u8655\u7406\u5176\u4ed6\u4efb\u52d9\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u541e\u5410\u91cf<\/strong>\uff1aDMA \u548c I2S \u63d0\u4f9b\u5feb\u901f\u3001\u53ef\u9760\u7684\u6578\u64da\u50b3\u8f38\uff0c\u9069\u7528\u65bc\u9ad8\u901f\u5ea6\u6578\u64da\u6536\u96c6\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u6548\u6578\u64da\u6536\u96c6<\/strong>\uff1a\u5728\u6700\u5c0f\u5ef6\u9072\u7684\u60c5\u6cc1\u4e0b\u53ef\u5927\u91cf\u6536\u96c6 ADC \u6578\u64da\uff0c\u9069\u5408\u5373\u6642\u61c9\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"real-world-applications\">\u5be6\u969b\u61c9\u7528\u5834\u666f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4fe1\u865f\u8655\u7406<\/strong>\uff1a\u5728\u5be6\u6642\u5206\u6790\u985e\u6bd4\u4fe1\u865f\uff08\u5982\u97f3\u983b\u4fe1\u865f\uff09\u7684\u61c9\u7528\u4e2d\uff0c\u53ef\u6301\u7e8c\u9032\u884c\u4f4e\u5ef6\u9072\u7684\u53d6\u6a23\u8207\u8655\u7406\u3002<\/li>\n\n\n\n<li><strong>\u50b3\u611f\u5668\u6578\u64da\u8a18\u9304<\/strong>\uff1a\u5728\u74b0\u5883\u76e3\u63a7\u6216\u5de5\u696d\u6578\u64da\u8a18\u9304\u7684\u9577\u6642\u9593\u4efb\u52d9\u4e2d\uff0c\u9019\u7a2e\u8a2d\u7f6e\u78ba\u4fdd ADC \u8b80\u6578\u9ad8\u6548\u6301\u7e8c\u6536\u96c6\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u901f\u53d6\u6a23<\/strong>\uff1a\u6b64\u6280\u8853\u9069\u5408\u9700\u8981\u9ad8\u901f\u53d6\u6a23\u7684\u61c9\u7528\u5834\u666f\uff0c\u5982\u91ab\u7642\u8a2d\u5099\u6216\u96fb\u5b50\u6e2c\u8a66\u5de5\u5177\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">\u7d50\u8ad6<\/h2>\n\n\n\n<p>\u6211\u5011\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u00a0<a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/api-reference\/kconfig-reference.html?highlight=dma\" target=\"_blank\" rel=\"noopener\"><strong>ESP32<\/strong>\u00a0<strong>DMA<\/strong><\/a>\u00a0\u548c\u00a0<strong>I2S<\/strong>\u00a0\u9ad8\u6548\u5730\u6536\u96c6\u00a0<strong>ADC<\/strong>\u00a0\u6578\u64da\u3002\u901a\u904e\u5229\u7528\u9019\u4e9b\u5f37\u5927\u7684\u5916\u8a2d\uff0c\u60a8\u53ef\u4ee5\u5c07\u6578\u64da\u50b3\u8f38\u7684\u5de5\u4f5c\u5f9e\u00a0<strong>CPU<\/strong>\u00a0\u4e2d\u5378\u8f09\uff0c\u5be6\u73fe\u9ad8\u901f\u3001\u4f4e\u5ef6\u9072\u7684\u6578\u64da\u6536\u96c6\u3002\u9019\u7a2e\u65b9\u6cd5\u975e\u5e38\u9069\u5408\u9700\u8981\u5be6\u6642\u6578\u64da\u6536\u96c6\u548c\u8655\u7406\u7684\u9805\u76ee\uff0c\u4e26\u4e14\u4f7f\u5f97\u00a0<strong>ESP32<\/strong>\u00a0\u6210\u70ba\u5d4c\u5165\u5f0f\u61c9\u7528\u7684\u5f37\u5927\u5e73\u53f0\u3002\u7121\u8ad6\u60a8\u662f\u69cb\u5efa\u50b3\u611f\u5668\u9663\u5217\u3001\u97f3\u983b\u7cfb\u7d71\uff0c\u9084\u662f\u53ea\u9700\u8981\u5feb\u901f\u7684\u00a0<strong><a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/migration-guides\/release-5.x\/5.0\/peripherals.html#peripherals\" target=\"_blank\" rel=\"noopener\">ADC<\/a><\/strong>\u00a0\u53d6\u6a23\uff0c\u4f7f\u7528\u00a0<strong>ESP32<\/strong>\u00a0<strong>DMA<\/strong>\u00a0\u548c\u00a0<strong><a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/api-reference\/peripherals\/i2s.html\" target=\"_blank\" rel=\"noopener\">I2S<\/a><\/strong>\u00a0\u90fd\u662f\u63d0\u9ad8\u9805\u76ee\u6027\u80fd\u548c\u6548\u7387\u7684\u7d55\u4f73\u65b9\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u5728&nbsp;ESP32&nbsp;\u4e0a\u4f7f\u7528&nbsp;DMA\uff08\u76f4\u63a5\u5b58\u5132\u5668\u5b58\u53d6\uff09\u548c&#038;nb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2970,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,53,83],"tags":[61,27,63,20,60,10,64,82,9,11,62],"class_list":["post-2965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-module","category-esp32","category-featured","tag-bluetooth","tag-c","tag-firmware","tag-hardware","tag-iot","tag-mcu","tag-module","tag-mqtt","tag-python","tag-tutorial","tag-wifi"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/comments?post=2965"}],"version-history":[{"count":14,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2965\/revisions"}],"predecessor-version":[{"id":2983,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2965\/revisions\/2983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media\/2970"}],"wp:attachment":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media?parent=2965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/categories?post=2965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/tags?post=2965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}