{"id":20807,"date":"2023-12-07T16:11:56","date_gmt":"2023-12-07T08:11:56","guid":{"rendered":"https:\/\/www.circuspi.com\/?p=20807"},"modified":"2023-12-07T16:11:58","modified_gmt":"2023-12-07T08:11:58","slug":"esp32-spi-introduction","status":"publish","type":"post","link":"https:\/\/www.circuspi.com\/index.php\/2023\/12\/07\/esp32-spi-introduction\/","title":{"rendered":"ESP32\u6559\u5b78\u7cfb\u5217(\u516b)\uff1a\u5e8f\u5217\u5468\u908a\u4ecb\u9762(SPI)"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"526\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/1000x526_1.jpg\" alt=\"\" class=\"wp-image-20826\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u9019\u6b21\u6211\u5011\u8981\u4ecb\u7d39\u7684\u4e3b\u984c\u662fSPI\u901a\u8a0a\u754c\u9762\u3002\u4e00\u822c\u5728\u4f7f\u7528SPI\u4ecb\u9762\u50b3\u8f38\u7684\u611f\u6e2c\u5668\u6216\u88dd\u7f6e\u6642\uff0c\u901a\u5e38\u9084\u6703\u518d\u53e6\u5916\u642d\u914d\u5176\u4ed6\u51fd\u5f0f\u5eab\uff0c\u8207ESP32\u539f\u751f\u7684SPI\u95dc\u9023\u4e0d\u5927\uff0c\u6709\u95dc\u5be6\u969b\u61c9\u7528\u7684\u5167\u5bb9\u6211\u5011\u6703\u7559\u5230\u4e0b\u4e00\u7bc7\u518d\u9032\u884c\u8aaa\u660e\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528ESP32\u7684SPI\uff0c\u4e26\u4e14\u8aaa\u660e\u5982\u4f55\u5728\u5169\u7247ESP32\u958b\u767c\u677f\u4e4b\u9593\u4f7f\u7528SPI\u901a\u8a0a\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u9ebc\u662fSPI\uff1f<\/h2>\n\n\n\n<p>\u5e8f\u5217\u5468\u908a\u4ecb\u9762\uff08Serial Peripheral Interface\uff0c SPI\uff09\uff0c\u662f\u5fae\u63a7\u5236\u5668\u5e38\u7528\u7684\u4e00\u7a2e\u5168\u96d9\u5de5\u4e32\u5217\u540c\u6b65\u901a\u8a0a\u754c\u9762\uff0c\u9069\u7528\u65bc\u8207\u5468\u908a\u8a2d\u5099\u9032\u884c\u77ed\u8ddd\u96e2\u4e14\u9ad8\u901f\u7684\u50b3\u8f38\u3002\u5b83\u63a1\u7528\u4e86\u4e3b\u5f9e\u5f0f\u67b6\u69cb\uff0c\u7531\u4e00\u500b\u4e3b\u8a2d\u5099\u548c\u4e00\u81f3\u591a\u500b\u5468\u908a\u8a2d\u5099\u7d44\u6210\uff0c\u6700\u65e9\u7531\u6469\u6258\u7f85\u62c9\uff08Motorola\uff09\u516c\u53f8\u57281980\u5e74\u4ee3\u4e2d\u671f\u958b\u767c\u3002<\/p>\n\n\n\n<p>\u5728Flash\uff08\u5feb\u9583\u8a18\u61b6\u9ad4\uff09\u3001EEPROM\u3001RTC\uff08\u5be6\u6642\u6642\u9418\uff09\u3001ADC\uff08\u985e\u6bd4\u6578\u4f4d\u8f49\u63db\u5668\uff09\u3001DAC\uff08\u6578\u4f4d\u985e\u6bd4\u8f49\u63db\u5668\uff09\u3001\u6db2\u6676\u986f\u793a\u5668\uff0c\u6216\u662f\u5169\u500b\u5fae\u63a7\u5236\u5668\u9593\u7684\u901a\u8a0a\u90fd\u53ef\u4ee5\u770b\u5230SPI\u7684\u61c9\u7528\u3002\u4f46\u7531\u65bcSPI\u4e0d\u662f\u4e00\u5957\u5177\u6709\u6a19\u6e96\u898f\u7bc4\u7684\u5354\u8b70\uff0c\u5b83\u53ea\u662f\u4e00\u7a2e\u696d\u754c\u6a19\u6e96\uff08de facto standard\uff09\uff0c\u6240\u4ee5\u4e0d\u662f\u6bcf\u500b\u4f7f\u7528SPI\u7684\u8a2d\u5099\u90fd\u7528\u76f8\u540c\u7684\u65b9\u5f0f\u5be6\u73fe\u3002<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>\ud83d\udca1<span data-token-index=\"0\" class=\"notion-enable-hover\" style=\"font-weight:600\">Tips:<\/span> \u904e\u53bb SPI \u5e38\u4ee5 Master \u8207 Slave \u7a31\u547c\u4e3b\u5f9e\u7aef\u7684\u8a2d\u5099\uff0c\u4f46\u662f\u958b\u6e90\u786c\u9ad4\u806f\u76df\uff08Open Source Hardware Association, OSHWA\uff09\u5728<a style=\"cursor:pointer;color:inherit;word-wrap:break-word;font-weight:600;text-decoration:inherit\" class=\"notion-link-token notion-focusable-token notion-enable-hover\" rel=\"noopener noreferrer\" data-token-index=\"2\" tabindex=\"0\" href=\"https:\/\/www.oshwa.org\/a-resolution-to-redefine-spi-signal-names\/\"><span class=\"link-annotation-unknown-block-id--206015494\" style=\"border-bottom:0.05em solid;border-color:rgba(55,53,47,.4);opacity:0.7\">A Resolution to Redefine SPI Signal Names<\/span><\/a>\u9019\u7bc7\u6587\u7ae0\u4e2d\u5efa\u8b70\u61c9\u8a72\u4f7f\u7528\u65b0\u7684\u547d\u540d\u65b9\u5f0f\uff0c\u4ee5 <span data-token-index=\"4\" class=\"notion-enable-hover\" style=\"font-weight:600\">Controller \u4ee3\u66ff Master<\/span>\uff0c<span data-token-index=\"6\" class=\"notion-enable-hover\" style=\"font-weight:600\">Peripheral \u4ee3\u66ffSlave<\/span>\uff0cSCK\uff08Serial Clock\uff09\u7684\u7a31\u547c\u7dad\u6301\u4e0d\u8b8a\u3002<\/p><\/blockquote><\/figure>\n\n\n\n<p>\u65b0\u820a\u63a5\u8173\u540d\u7a31\u5c0d\u7167\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u63a5\u8173\u540d\u7a31\uff08\u820a\uff09<\/th><th>\u63a5\u8173\u540d\u7a31\uff08\u65b0\uff09<\/th><\/tr><\/thead><tbody><tr><td>MISO (Master In Slave Out)<\/td><td>CIPO (Controller In Peripheral Out)<\/td><\/tr><tr><td>MOSI (Master Out Slave In)<\/td><td>COPI (Controller Out Peripheral In)<\/td><\/tr><tr><td>SS (Slave Select Pin)<\/td><td>CS (Chip Select Pin)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>SPI\u901a\u8a0a\u63a1\u7528\u4e3b\u5f9e\u5f0f\u67b6\u69cb\uff0c\u7531\u4e3b\u8a2d\u5099\u767c\u8d77\u50b3\u8f38\u8a0a\u865f\uff0c\u7d71\u4e00\u63a7\u5236\u6240\u6709\u88dd\u7f6e\u50b3\u8f38\uff0c\u4e26\u63a7\u5236\u8a2d\u5099\u8207\u8a2d\u5099\u4e4b\u9593\u767c\u9001\u548c\u8b80\u53d6\u8cc7\u6599\u7684\u6642\u6a5f\u3002\u6240\u6709\u8a2d\u5099\u90fd\u5171\u4eab\u540c\u4e00\u500b\u7531\u4e3b\u8a2d\u5099\u63d0\u4f9b\u7684\u6642\u8108(clock)\u8a0a\u865f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/1280px-SPI_three_slaves.svg.png\" alt=\"\" class=\"wp-image-20809\" width=\"546\" height=\"433\"\/><figcaption class=\"wp-element-caption\">SPI \u532f\u6d41\u6392\u3002\uff08\u5716\u7247\u53d6\u81ea<a href=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/e\/ed\/SPI_single_slave.svg\/525px-SPI_single_slave.svg.png\">\u7dad\u57fa\u767e\u79d1<\/a>\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u4e00\u822cSPI\u6703\u67094\u689d\u8cc7\u6599\u7dda\uff0c\u5206\u5225\u662f\uff1a<\/p>\n\n\n\n<ul>\n<li>CIPO\/MISO: \u5468\u908a\u88dd\u7f6e\u8f38\u51fa\u8cc7\u6599\u5230\u63a7\u5236\u5668\u3002<\/li>\n\n\n\n<li>COPI\/MOSI: \u63a7\u5236\u5668\u8f38\u51fa\u8cc7\u6599\u5230\u5468\u908a\u88dd\u7f6e\u3002<\/li>\n\n\n\n<li>SCK\/SCLK\uff08Serial Clock\uff09: \u4e32\u5217\u6642\u8108\uff0c\u7531\u4e3b\u8a2d\u5099\u767c\u51fa\u3002\u6642\u8108\u901f\u5ea6\u4e5f\u6c7a\u5b9a\u4e86\u50b3\u8f38\u901f\u5ea6\u3002<\/li>\n\n\n\n<li>CS\/SS: \u6676\u7247\u9078\u64c7\u63a5\u8173\uff0c\u4f4e\u96fb\u4f4d\u9a45\u52d5\u3002\u7576\u540c\u4e00\u689dSPI Bus\u4e0a\u6709\u591a\u500b\u8a2d\u5099\u6642\uff0c\u4e3b\u8a2d\u5099\u6703\u7528CS\u63a5\u8173\u9078\u64c7\u8981\u9032\u884c\u901a\u8a0a\u7684\u5468\u908a\u8a2d\u5099\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SPI\u6a21\u5f0f<\/h3>\n\n\n\n<p>\u7576\u8a2d\u5099\u50b3\u9001\u8cc7\u6599\u6642\uff0c\u5728\u4ec0\u9ebc\u6642\u9593\u9ede\u8b80\u53d6\u8a0a\u865f\uff0c\u6703\u76f4\u63a5\u7684\u5f71\u97ff\u8a2d\u5099\u4e4b\u9593\u7684\u901a\u8a0a\u5167\u5bb9\u3002\u901a\u5e38\u5728SPI\u4e2d\uff0c\u6703\u4ee5\u6a21\u5f0f\uff08Mode\uff09\u4f86\u5b9a\u7fa9\u3002\u6a21\u5f0f\u7531\u5169\u500b\u689d\u4ef6\u69cb\u6210\uff1a\u6642\u8108\u76f8\u4f4d\uff08clock phase\uff09\u8207\u6642\u8108\u6975\u6027\uff08clock polarity\uff09\u3002<\/p>\n\n\n\n<ul>\n<li>\u6642\u8108\u76f8\u4f4d\uff08CPHA\uff09\uff1a\u9078\u64c7\u8a0a\u865f\u8981\u5728SCLK\u7684\u4e0a\u5347\u7de3\u6216\u662f\u4e0b\u964d\u7de3\u9032\u884c\u63a1\u6a23\u3002 CPHA=0: \u4e0a\u5347\u7de3\uff1bCPHA = 1: \u4e0b\u964d\u7de3\u3002<\/li>\n\n\n\n<li>\u6642\u8108\u6975\u6027\uff08CPOL\uff09\uff1a\u7576SPI\u5728\u9592\u7f6e\u72c0\u614b\u6642\uff0cSCLK\u7684\u96fb\u4f4d\u72c0\u614b\u3002 \uff08\u8a3b\uff1a\u9592\u7f6e\u72c0\u614b\u6307\u7684\u662fCS\u63a5\u8173\u5728\u50b3\u8f38\u958b\u59cb\u8207\u50b3\u8f38\u7d50\u675f\u6642\u7684\u72c0\u614b\uff09 CPOL = 0: \u9592\u7f6e\u6642\u4fdd\u6301\u4f4e\u96fb\u4f4d\uff1bCPOL = 1: \u9592\u7f6e\u6642\u4fdd\u6301\u9ad8\u96fb\u4f4d\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6839\u64da\u76f8\u4f4d\u548c\u6975\u6027\u7684\u8b8a\u5316\uff0cSPI\u5c31\u6703\u7522\u751f4\u7a2e\u4e0d\u540c\u7684\u50b3\u8f38\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u6a21\u5f0f<\/th><th>Clock Polarity (CPOL)&nbsp;<\/th><th>Clock Phase (CPHA)<\/th><\/tr><\/thead><tbody><tr><td>MODE0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>MODE1<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>MODE2<\/td><td>1<\/td><td>0<\/td><\/tr><tr><td>MODE3<\/td><td>1<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32\u7684SPI\u4ecb\u9762<\/h2>\n\n\n\n<p>ESP32\u7e3d\u5171\u67094\u500bSPI\u4ecb\u9762\uff0c\u5206\u5225\u662fSPI0~SPI3\u3002SPI0 \u8207 SPI1 \u5df2\u7d93\u88ab\u7528\u4f86\u9023\u63a5\u5230\u6676\u7247\u4e0a\u7684Flash Memory\uff0c\u6211\u5011\u4e00\u822c\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u7684\u662fSPI2\u8207SPI3\u9019\u5169\u500bSPI\u63a7\u5236\u5668\uff0c\u5176\u4e2dSPI2\u53c8\u88ab\u7a31\u4f5c HSPI\uff08High-speed Serial Peripheral Interface\uff09\uff0cSPI3\u88ab\u7a31\u4f5cVSPI\uff08Very High-speed Serial Peripheral Interface\uff09\u3002\u9019\u5169\u500b SPI Bus \u5404\u81ea\u5177\u6709\u7368\u7acb\u7684\u8a0a\u865f\uff0c\u4ee3\u8868\u6211\u5011\u53ef\u4ee5\u96a8\u610f\u5c07\u4efb\u610f\u4e00\u9805\u4f5c\u70ba\u4e3b\u8a2d\u5099\u6216\u5468\u908a\u8a2d\u5099\uff0c\u800c\u4e0d\u5f71\u97ff\u5230\u53e6\u5916\u4e00\u9805\u3002\u5728\u628a\u5b83\u5011\u7576\u4f5c\u4e3b\u8a2d\u5099\u7684\u60c5\u6cc1\u4e0b\uff0c\u55ae\u4e00\u500bSPI Bus\u6700\u591a\u53ef\u4ee5\u9a45\u52d53\u500b\u5468\u908a\u8a2d\u5099\u3002<\/p>\n\n\n\n<p>HSPI \u548c VSPI Bus\u7684\u9810\u8a2d\u63a5\u8173\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2221\" height=\"1579\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/ESP32-PINOUT.jpg\" alt=\"\" class=\"wp-image-20824\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>\ud83d\udca1<span data-token-index=\"0\" class=\"notion-enable-hover\" style=\"font-weight:600\">Tips:<\/span> \u5728\u67d0\u4e9bSPI\u5468\u908a\u8a2d\u5099\u7684\u8173\u4f4d\u6a19\u793a\u4e2d\uff0cCOPI \/ MOSI \u53ef\u80fd\u88ab\u6a19\u8a18\u70ba  SDO \uff08Serial Data Out\uff09, CIPO \/ MISO \u53ef\u80fd\u88ab\u6a19\u8a18\u70ba SDI \uff08Serial Data In\uff09\u3002<\/p><\/blockquote><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528ESP32\u4e0a\u9762\u7684SPI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">VSPI\uff08\u9810\u8a2d\uff09<\/h3>\n\n\n\n<p>\u5728\u6c92\u6709\u7279\u5225\u8a2d\u5b9a\u7684\u60c5\u6cc1\u4e0b\uff0cESP32\u7684SPI\u9810\u8a2d\u4f7f\u7528VSPI\uff08SPI3\uff09\uff0c\u56e0\u6b64\u53ea\u8981\u7167\u8457\u9810\u8a2d\u63a5\u8173\u9023\u63a5\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u6211\u5011\u53ef\u4ee5\u57f7\u884c\u4e0b\u9762\u7684\u7bc4\u4f8b\u67e5\u770b\u958b\u767c\u677f\u7684\u9810\u8a2d\u63a5\u8173\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* \u986f\u793aESP32 SPI\u9810\u8a2d\u63a5\u8173 *\/\n\nvoid setup() {\n  Serial.begin(115200);\n}\n\nvoid loop() {\n  Serial.print(\"COPI \/ MOSI: \");\n  Serial.println(MOSI);\n  Serial.print(\"CIPO \/ MISO: \");\n  Serial.println(MISO);\n  Serial.print(\"SCK: \");\n  Serial.println(SCK);\n  Serial.print(\"CS \/ SS: \");\n  Serial.println(SS);\n  delay(1000);\n  Serial.println();\n}<\/code><\/pre>\n\n\n\n<p><span data-token-index=\"0\" class=\"notion-enable-hover\" style=\"font-weight:600\">\u57f7\u884c\u7d50\u679c<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/\u5716\u7247-2.png\" alt=\"\" class=\"wp-image-20814\" width=\"428\" height=\"192\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HSPI<\/h3>\n\n\n\n<p>\u5982\u679c\u8981\u4f7f\u7528HSPI\uff0c\u5c31\u8981\u5148\u5f15\u5165&lt;SPI.h&gt;\u9019\u500b\u6a19\u982d\u6a94\uff0c\u7136\u5f8c\u7528SPIClass\u51fd\u5f0f\u5be6\u4f8b\u5316\u7269\u4ef6\uff0c\u4e26\u6307\u5b9a\u4f7f\u7528HSPI\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># include &lt;SPI.h&gt;\n\nSPIClass MySPI2(HSPI);  \/\/ \u6307\u5b9aMySPI2\u4f7f\u7528HSPI\n\nvoid setup() {\n\n  MySPI2.begin();       \/\/ \u521d\u59cb\u5316MySPI2\uff0c\u4f7f\u7528HSPI\u9810\u8a2d\u63a5\u8173 (SCLK = 14, CIPO= 12, COPI =13, CS = 15)\n  \n\t\/\/ \u4ee5\u4e0b\u7701\u7565...\n}\n\nvoid loop() {\n  \n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u8a02\u7fa9SPI\u63a5\u8173<\/h3>\n\n\n\n<p>SPIClass\u7684begin\u51fd\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4f86\u6307\u5b9a\u5176\u4ed6GPIO\u63a5\u8173\uff1a<\/p>\n\n\n<p>MySPI2.begin(HSPI_SCLK, HSPI_CIPO, HSPI_COPI, HSPI_CS); \/\/SCLK, CIPO, COPI, CS Pin<\/p>\n\n\n\n<p>\u4f8b\u5982\u50cf\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># include &lt;SPI.h&gt;\n\n\/\/ \u63a5\u8173\u5b9a\u7fa9\n# define MySPI2_SCLK 14\n# define MySPI2_CIPO 12\n# define MySPI2_COPI 13\n# define MySPI2_CS 15\n\n# define SPI2CLK 1000000  \/\/ 1M Hz\n\nSPIClass MySPI2(HSPI);\n\nvoid setup() {\n  MySPI2.begin(MySPI2_SCLK, MySPI2_CIPO, MySPI2_COPI, MySPI2_CS);  \/\/ \u5728begin()\u81ea\u5b9a\u7fa9SPI2\u7684\u63a5\u8173\n}\n\nvoid loop() {\n  \n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>\ud83d\udca1Tips: \u7576SPI\u88dd\u7f6e\u9700\u8981\u9ad8\u901f\u901a\u8a0a\u6642\uff0c\u5efa\u8b70\u4f7f\u7528\u9810\u8a2d\u63a5\u8173\u3002<\/p><\/blockquote><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SPI\u50b3\u8f38\u6d41\u7a0b<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u8a2d\u5b9a\u597dSPI\u7684\u6642\u8108\u983b\u7387\u4ee5\u53ca\u50b3\u8f38\u6a21\u5f0f\uff0c\u7136\u5f8c\u4f7f\u7528beginTransaction\u521d\u59cb\u5316SPI\u4ecb\u9762\u3002<\/p>\n\n\n<p>SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0)); \/\/ \u6642\u81081MHz, MSBFIRST, MODE0<\/p>\n\n\n\n<p>\u63a5\u8457\u7528CS\u63a5\u8173\u9078\u64c7\u8981\u50b3\u9001\u7684\u5468\u908a\u8a2d\u5099\uff0c\u5c07\u96fb\u58d3\u6e96\u4f4d\u62c9\u4f4e\uff0c\u555f\u7528\u8a2d\u5099\u3002<\/p>\n\n\n<p>digitalWrite(CS_pin, LOW);<\/p>\n\n\n\n<p>\u7136\u5f8c\u547c\u53ebtransfer()\u65b9\u6cd5\u50b3\u8f38\u8cc7\u6599\u3002<\/p>\n\n\n<p>SPI.transfer(data);<\/p>\n\n\n\n<p>\u50b3\u8f38\u5b8c\u6210\u5f8c\uff0c\u518d\u5c07CS\u63a5\u8173\u7684\u96fb\u58d3\u6e96\u4f4d\u62c9\u56de\u9ad8\u96fb\u4f4d\u3002<\/p>\n\n\n<p>digitalWrite(CS_pin, HIGH);<\/p>\n\n\n\n<p>\u6700\u5f8c\uff0c\u547c\u53ebendTransaction()\uff0c\u91cb\u653e\u76ee\u524d\u4f7f\u7528\u7684SPI Bus\u3002<\/p>\n\n\n<p>SPI.endTransaction();<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SPI\u8a0a\u865f\u7684\u5be6\u969b\u8f38\u51fa<\/h2>\n\n\n\n<p>\u4e0b\u5716\u662f\u6307\u5b9aESP32\u7576\u4f5cSPI Controller\u8f38\u51fa<code> <strong><em>0b11001100<\/em><\/strong><\/code> \u7684\u8a0a\u865f\uff0c\u7528\u908f\u8f2f\u5206\u6790\u5100\u6355\u6349\u5230\u7684\u6ce2\u5f62\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"441\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/\u5716\u7247-3.png\" alt=\"\" class=\"wp-image-20817\"\/><\/figure>\n\n\n\n<p>\u9019\u6bb5\u901a\u8a0a\u7684\u50b3\u8f38\u6a21\u5f0f\u662fMODE 0\uff0c\u6240\u4ee5\u5f9e\u5716\u7247\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u6bcf\u500b\u6642\u8108\u7684\u4e0a\u5347\u7de3\u8b80\u53d6\uff0cMOSI\u63a5\u8173\u5c31\u6703\u63a5\u6536\u5230 <strong><em>0b11001100<\/em><\/strong> \u7684\u8cc7\u6599\u3002\u4f46\u662f\u5728\u8a66\u4e86\u5e7e\u6b21\u5f8c\u90fd\u6c92\u80fd\u6293\u5230CS\u63a5\u8173\u7684\u8b8a\u5316\u72c0\u614b\uff0c\u4e14CS\u51fa\u73fe\u7684\u5e7e\u500b\u5c16\u6ce2\uff0c\u4e5f\u8b93\u7b46\u8005\u76f8\u7576\u4e0d\u89e3\u3002\u5982\u679c\u6709\u7db2\u53cb\u77e5\u9053\u539f\u56e0\uff0c\u9084\u8acb\u4e0d\u541d\u544a\u77e5\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6e2c\u8a66\u7a0b\u5f0f\u78bc<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/FelixLinSY\/ff41917f54e01bd94ea78833fa332517.js\"><\/script>\n\n\n\n<p>\uff08\u8a3b: \u9019\u90e8\u5206\u53c3\u8003\u7684\u662farduino-esp32 core SPI example\u7684\u6e2c\u8a66\u7a0b\u5f0f\uff08<a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/SPI\/examples\/SPI_Multiple_Buses\/SPI_Multiple_Buses.ino\">Link<\/a>\uff09\uff09<\/p>\n\n\n\n<p>\u5f9e\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\u6211\u5011\u53ef\u4ee5\u770b\u5230\uff0c\u5728SPI\u8cc7\u6599\u50b3\u8f38\u968e\u6bb5\uff0cESP32\u4f7f\u7528\u7684\u7a0b\u5f0f\u8a9e\u6cd5\u8207\u539f\u751f\u7684Arduino\u76f8\u540c\u3002\u5728\u50b3\u8f38\u8cc7\u6599\u524d\uff0c\u6703\u5148\u5c07CS\u63a5\u8173\u63a5\u5230\u4f4e\u96fb\u4f4d\u518d\u9001\u51fa\u8cc7\u6599\u3002\u50b3\u8f38\u5b8c\u6210\u5f8c\u518d\u5c07CS\u63a5\u8173\u63a5\u56de\u9ad8\u96fb\u4f4d\uff0c\u8868\u793a\u50b3\u8f38\u7d50\u675f\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9023\u63a5\u591a\u500bSPI\u8a2d\u5099<\/h2>\n\n\n\n<p>\u5047\u5982ESP32\u540c\u6642\u8207\u5468\u908a\u8a2d\u50991\u4ee5\u53ca\u8a2d\u50992\u9023\u63a5\uff0c\u8981\u5411\u5468\u908a\u8a2d\u50992\u50b3\u9001\u8cc7\u6599\u6642\uff0c\u4e5f\u662f\u7528\u540c\u6a23\u7684\u65b9\u5f0f\u8a2d\u5b9a\u3002\u53ea\u662f\u8981\u5148\u5c07\u5468\u908a\u8a2d\u50991\u7684CS\u63a5\u8173\u8a2d\u70ba\u9ad8\u96fb\u4f4d\uff0c\u518d\u53bb\u8a2d\u5b9a\u8a2d\u50992\u7684CS\u63a5\u8173\u3002\u63a5\u7dda\u90e8\u5206\u9700\u8981\u6ce8\u610fController\u7528\u4e86\u5169\u6839\u4e0d\u540c\u7684CS\u63a5\u8173\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void loop() {\n\n  MySPI2.beginTransaction(SPISettings(SPI2CLK, MSBFIRST, SPI_MODE0)); \/\/ \u521d\u59cb\u5316SPI bus\u3002\u8207Arduino SPI\u7528\u6cd5\u76f8\u540c\n  \n  digitalWrite(MySPI2_CS1, HIGH); \/\/ \u5c07\u8a2d\u50991\u7684\u6676\u7247\u9078\u64c7\u8173(CS)\u62c9\u9ad8\uff0c\u7981\u7528\u8a2d\u50991\n  digitalWrite(MySPI2_CS2, LOW); \/\/ \u5c07\u8a2d\u50992\u7684\u6676\u7247\u9078\u64c7\u8173(CS)\u62c9\u4f4e\uff0c\u6e96\u5099\u50b3\u8f38\u8cc7\u6599\n\n  MySPI2.transfer(byte(0b11001100)); \/\/ \u50b3\u8f38\u4e8c\u9032\u4f4d\u8cc7\u6599\n  \n  \/\/ \u4ee5\u4e0b\u7701\u7565....\n}<\/code><\/pre>\n\n\n\n<p>\u8a3b\uff1a\u82e5\u5c07ESP32\u7576\u4f5c\u4e3b\u8a2d\u5099\u8b80\u53d6\u5177\u6709SPI\u4ecb\u9762\u7684\u611f\u6e2c\u5668\u8cc7\u6599\uff0c\u901a\u5e38\u9084\u6703\u53e6\u5916\u518d\u642d\u914d\u611f\u6e2c\u5668\u7684\u51fd\u5f0f\u5eab\u4f7f\u7528\uff0c\u4f46\u662f\u6b64\u90e8\u5206\u7684\u7a0b\u5f0f\u78bc\u8207\u539f\u751fSPI\u7684\u95dc\u806f\u6027\u8f03\u4f4e\uff0c\u672c\u7bc7\u5148\u4e0d\u8a0e\u8ad6\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9032\u968e\u7bc4\u4f8b: \u4f7f\u7528SPI\u5728ESP32\u4e4b\u9593\u50b3\u8a0a<\/h2>\n\n\n\n<p>\u63a5\u4e0b\u4f86\u7684\u7bc4\u4f8b\u6703\u8aaa\u660e\u5982\u4f55\u5728\u5169\u7247\u4e0d\u540c\u7684ESP32\u958b\u767c\u677f\u4e0a\u9762\u4f7f\u7528SPI\u901a\u8a0a\u3002\u6211\u5011\u628a\u53f3\u908a\u7684\u958b\u767c\u677f\u7576\u4f5c\u4e3b\u8a2d\u5099\uff0c\u5de6\u908a\u7684\u958b\u767c\u677f\u7576\u4f5c\u5468\u908a\u8a2d\u5099\uff0c\u5169\u908a\u4f7f\u7528HSPI\u7684\u9810\u8a2d\u63a5\u8173\u901a\u8a0a\uff0c\u7531\u4e3b\u8a2d\u5099\u767c\u9001\u4e8c\u9032\u4f4d\u8a0a\u606f\u5230\u5468\u908a\u8a2d\u5099\u3002\u7576\u9031\u908a\u8a2d\u5099\u63a5\u6536\u52300x01\u8a0a\u865f\u6642\uff0c\u5c31\u6703\u9ede\u4eae\u958b\u767c\u677f\u4e0a\u7684LED\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/IMG_2339.jpeg\" alt=\"\" class=\"wp-image-20819\" width=\"799\" height=\"600\"\/><\/figure>\n\n\n\n<p><h3>\u6750\u6599\u6e05\u55ae<li>ESP32\u958b\u767c\u677f x2micro USB \u50b3\u8f38\u7dda x1\u9eb5\u5305\u677f x1\u675c\u90a6\u7dda\uff08\u516c-\u516c\uff09 x6<\/li>\u9023\u63a5\u793a\u610f\u5716<\/h3> <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"862\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/\u5716\u7247-4.png\" alt=\"\" class=\"wp-image-20820\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8173\u4f4d\u5c0d\u7167\u8868<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>SPI Bus<\/strong><\/th><th><strong>\u4e3b\u8a2d\u5099<\/strong><\/th><th><strong>\u5468\u908a\u8a2d\u5099<\/strong><\/th><\/tr><\/thead><tbody><tr><td>COPI<\/td><td>GPIO 13<\/td><td>GPIO 13<\/td><\/tr><tr><td>CIPO<\/td><td>GPIO 12<\/td><td>GPIO 12<\/td><\/tr><tr><td>SCK<\/td><td>GPIO 14<\/td><td>GPIO 14<\/td><\/tr><tr><td>CS<\/td><td>GPIO 15<\/td><td>GPIO 15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3b\u63a7\u7aef<\/h3>\n\n\n\n<p>\u4e0b\u9762\u7684\u7a0b\u5f0f\u529f\u80fd\u662f\u8b93\u4e3b\u63a7\u7aef\u4f7f\u7528HSPI\u63a7\u5236\u5668\u56fa\u5b9a\u9593\u9694\u5927\u7d041\u79d2\u9418\u7684\u6642\u9593\uff0c\u50b3\u9001\u8a0a\u865f\u7d66\u5468\u908a\u8a2d\u5099\u3002<\/p>\n\n\n\n<p><strong>\u7a0b\u5f0f\u78bc<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/FelixLinSY\/2ce96aa6a6a1a4cb28670fb27160eaae.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5468\u908a\u8a2d\u5099\u7aef<\/h3>\n\n\n\n<p>\u7531\u65bc&lt;SPI.h&gt;\u51fd\u5f0f\u5eab\u6c92\u6709\u652f\u63f4\u5c07ESP32\u7576\u4f5c\u5468\u908a\u8a2d\u5099\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u6211\u5011\u9700\u8981\u5148\u5230\u51fd\u5f0f\u5eab\u7ba1\u7406\u54e1\u4e2d\u4e0b\u8f09ESP32SPISlave\u9019\u500b\u51fd\u5f0f\u5eab\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/\u5716\u7247-5.png\" alt=\"\" class=\"wp-image-20821\" width=\"387\" height=\"401\"\/><\/figure>\n\n\n\n<p>\u5c07\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u8907\u88fd\u5230IDE\u5f8c\u4e0a\u50b3\uff0c\u6253\u958b\u5e8f\u5217\u57e0\u76e3\u63a7\u8996\u7a97\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u76ee\u524d\u63a5\u6536\u5230\u7684\u8cc7\u6599\u3002<\/p>\n\n\n\n<p><strong>\u7a0b\u5f0f\u78bc<\/strong><\/p>\n\n\n\n<p>\uff08\u8a3b\uff1a\u7531\u65bc\u6b64\u51fd\u5f0f\u5eab\u4f7f\u7528Slave\u4ee3\u7a31\u5468\u908a\u8a2d\u5099\uff0c\u56e0\u6b64\u7a0b\u5f0f\u78bc\u4e2d\u5c31\u6cbf\u7528Slave\u7684\u7a31\u547c\uff09<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/FelixLinSY\/a338fd7f8c5df08faf85ab3bae8c011e.js\"><\/script>\n\n\n\n<p>\u57f7\u884c\u7d50\u679c<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"280\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/12\/esp32_spi.gif\" alt=\"\" class=\"wp-image-20822\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><h2>\u5c0f\u7d50<\/h2><p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528ESP32\u4e0a\u9762\u7684VSPI\u548cHSPI\u9019\u5169\u500b\u7368\u7acb\u7684SPI\u63a7\u5236\u5668\u9032\u884cSPI\u901a\u8a0a\uff0c\u55ae\u4e00\u500bSPI\u63a7\u5236\u5668\u53ef\u4ee5\u9a45\u52d53\u500b\u5468\u908a\u88dd\u7f6e\uff0c\u6240\u4ee5\u6700\u591a\u53ef\u4ee5\u63a7\u5236\u52306\u500b\u5468\u908a\u88dd\u7f6e\u3002\u5728\u9023\u63a5\u5230\u591a\u500b\u5468\u908a\u8a2d\u5099\u6642\uff0c\u9700\u8981\u6ce8\u610f\u6bcf\u500b\u8a2d\u5099\u90fd\u8981\u7531\u4e0d\u540c\u7684CS\u63a5\u8173\u63a7\u5236\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u9664\u4e86\u4ecb\u7d39\u9810\u8a2d\u7684SPI\u63a5\u8173\uff0c\u4e5f\u8aaa\u660e\u4e86\u5982\u4f55\u7528\u8edf\u9ad4\u91cd\u65b0\u5b9a\u7fa9\u63a5\u8173\uff0c\u4ee5\u53ca\u4f7f\u7528SPI\u50b3\u8a0a\u6642\u5be6\u969b\u7684\u8f38\u51fa\u8a0a\u865f\u3002\u6700\u5f8c\u900f\u904e\u7bc4\u4f8b\u8aaa\u660e\u5982\u4f55\u5728\u5169\u7247ESP32\u9593\u7528SPI\u901a\u8a0a\u3002\u4e0b\u4e00\u7bc7\uff0c\u6211\u5011\u5c07\u6703\u8aaa\u660e\u5982\u4f55\u5728ESP32\u4e0a\u9762\u9a45\u52d5SPI\u4ecb\u9762\u7684\u96fb\u5b50\u7d19\u3002<\/p> <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u9019\u6b21\u6211\u5011\u8981\u4ecb\u7d39\u7684\u4e3b\u984c\u662fSPI\u901a\u8a0a\u754c\u9762\u3002\u4e00\u822c\u5728\u4f7f\u7528SPI\u4ecb\u9762\u50b3\u8f38\u7684\u611f\u6e2c\u5668\u6216\u88dd\u7f6e\u6642\uff0c\u901a\u5e38\u9084\u6703\u518d\u53e6\u5916\u642d\u914d\u5176\u4ed6\u51fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20827,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[235,238],"tags":[],"table_tags":[],"_links":{"self":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/20807"}],"collection":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/comments?post=20807"}],"version-history":[{"count":9,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/20807\/revisions"}],"predecessor-version":[{"id":20828,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/20807\/revisions\/20828"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/media\/20827"}],"wp:attachment":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=20807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=20807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=20807"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/table_tags?post=20807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}