{"id":21219,"date":"2024-02-23T09:01:41","date_gmt":"2024-02-23T01:01:41","guid":{"rendered":"https:\/\/www.circuspi.com\/?p=21219"},"modified":"2024-02-23T09:01:43","modified_gmt":"2024-02-23T01:01:43","slug":"esp32-hardware-interrupt","status":"publish","type":"post","link":"https:\/\/www.circuspi.com\/index.php\/2024\/02\/23\/esp32-hardware-interrupt\/","title":{"rendered":"ESP32 \u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7"},"content":{"rendered":"\n<p><style>\n\/* ###\u5be6\u9a57\u4e0b\u4e00\u5217### *\/\n\na:link {color:#0695e4\n;}    \/* \u8a2d\u5b9a\u5c1a\u672a\u9ede\u95b1\u904e\u7684\u9023\u7d50\u6a23\u5f0f *\/\na:visited {color:#0695e4\n;} \/* \u8a2d\u5b9a\u904e\u53bb\u66fe\u7d93\u95b1\u904e\u7684\u9023\u7d50 *\/\na:hover {color:#0695e4\n;}   \/* \u8a2d\u5b9a\u6ed1\u9f20\u6e38\u6a19\u6307\u5728\u9023\u7d50\u4f4d\u7f6e\u4e0a\u7684\u6a23\u5f0f *\/\na:active {color:#0000BB;}  \/* \u8a2d\u5b9a\u4e0a\u9ede\u64ca\u904e\u7684\u9023\u7d50\u6a23\u5f0f *\/\n<\/style><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"526\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/02\/1000x526_2-2.jpg\" alt=\"ESP32 \u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21430\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u5e38\u898b\u7684\u5fae\u63a7\u5236\u5668\u7cfb\u5217\uff0c\u4f8b\u5982Arduino\u7684ATmega\u7cfb\u5217\uff08\u50cf\u662fATmega328P\uff09\u3001STM32\u7cfb\u5217\u3001PIC\u7cfb\u5217\uff0c\u6216\u662fESP32\uff0c\u901a\u5e38\u90fd\u652f\u63f4\u591a\u7a2e\u4e0d\u540c\u7684\u4e2d\u65b7\u985e\u578b\u3002ESP32\u53ef\u4ee5\u7528\u786c\u9ad4\u4e2d\u65b7\u4f86\u8655\u7406\u5916\u90e8\u786c\u9ad4\u6216\u6a21\u7d44\u5f15\u8d77\u7684\u4e2d\u65b7\uff0c\u50cf\u662fGPIO\u6216\u662f\u89f8\u78b0\uff08TOUGH\uff09\u4e2d\u65b7\uff1b\u7528\u8edf\u9ad4\u4e2d\u65b7\u8655\u7406\u5b9a\u6642\u5668\u4e2d\u65b7\uff0c\u672c\u7bc7\u4e3b\u8981\u8aaa\u660e\u7684\u662f\u786c\u9ad4\u4e2d\u65b7\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u9ebc\u662f\u4e2d\u65b7\uff1f<\/h2>\n\n\n\n<p>\u4e2d\u65b7\uff08Interrupt\uff09\u662f\u4e00\u7a2e\u5728\u5fae\u63a7\u5236\u5668\u5728\u57f7\u884c\u4e3b\u8981\u5de5\u4f5c\u6642\uff0c\u4ecd\u7136\u80fd\u5920\u7acb\u5373\u56de\u61c9\u5916\u90e8\u4e8b\u4ef6\u7684\u4e00\u7a2e\u6a5f\u5236\u3002\u4e00\u500b\u8a2d\u8a08\u5f97\u7576\u7684\u4e2d\u65b7\uff0c\u4e0d\u50c5\u53ef\u4ee5\u7c21\u5316\u7a0b\u5f0f\u908f\u8f2f\uff0c\u9084\u80fd\u63d0\u9ad8\u8655\u7406\u5668\u7684\u57f7\u884c\u6548\u7387\u3002\u6211\u5011\u53ef\u4ee5\u7528\u4e2d\u65b7\u4f86\u5075\u6e2c\u958b\u95dc\u3001\u65cb\u8f49\u7de8\u78bc\u5668\uff0c\u6216\u662f\u5149\u906e\u65b7\u5668\u7b49\u7684\u8b8a\u5316\u3002\u8981\u7406\u89e3\u4e2d\u65b7\uff0c\u8b93\u6211\u5011\u5148\u7528\u4e00\u500b\u4f8b\u5b50\u4f86\u8aaa\u660e\u3002<\/p>\n\n\n\n<p>\u5047\u5982\u8981\u5075\u6e2c\u4e00\u500b\u6309\u9215\u662f\u5426\u88ab\u6309\u4e0b\uff0c\u6211\u5011\u53ef\u80fd\u6703\u9019\u6a23\u5beb\uff1a<\/p>\n\n\n\n<div style=\"overflow: auto; height: 480px; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/6684cf58357af92bee2f5c21893e8020.js\"><\/script>\n<\/div>\n\n\n\n<p>\u50cf\u9019\u6a23\u4f9d\u7167\u7a0b\u5f0f\u6d41\u7a0b\u5b9a\u6642\u4e14\u53cd\u8986\u6aa2\u67e5\u6578\u503c\u7684\u65b9\u6cd5\u5c31\u7a31\u505a\u8f2a\u8a62\u3002\u5728\u4e0d\u4f7f\u7528\u4e2d\u65b7\u7684\u60c5\u6cc1\u4e0b\uff0c\u901a\u5e38\u6703\u4f7f\u7528\u8f2a\u8a62\u4f86\u6aa2\u67e5\u5916\u90e8\u4e8b\u4ef6\uff0c\u512a\u9ede\u662f\u7a0b\u5f0f\u5beb\u8d77\u4f86\u76f4\u89ba\u3001\u597d\u7406\u89e3\uff0c\u4f46\u662f\u9019\u5176\u5be6\u6703\u8b93ESP32\u7684CPU\u5728\u5f88\u4f4e\u7684\u6548\u7387\u4e0b\u904b\u4f5c\uff0c\u56e0\u70ba\u7cfb\u7d71\u9700\u8981\u5728\u4e3b\u7a0b\u5f0f\u8ff4\u5708\u4e2d\u4e0d\u65b7\u7684\u6aa2\u67e5GPIO\u7684\u72c0\u614b\u3002\u5982\u679c\u4e3b\u7a0b\u5f0f\u5340\u584a\u9084\u6709\u5f88\u591a\u9700\u8981\u8655\u7406\u7684\u4efb\u52d9\uff0c\u6aa2\u67e5\u7684\u901f\u5ea6\u4e5f\u6703\u8b8a\u6162\u3002<\/p>\n\n\n\n<p>\u800c\u4f7f\u7528\u4e2d\u65b7\u7684\u597d\u8655\u662f\uff0c\u53ea\u8981\u7279\u5b9a\u7684\u63a5\u8173\u5075\u6e2c\u5230\u72c0\u614b\u767c\u751f\u8b8a\u5316\uff0c\u5c31\u80fd\u7acb\u5373\u300c\u4e2d\u65b7\u300dCPU\u73fe\u5728\u6b63\u5728\u505a\u7684\u4e8b\u60c5\uff0c\u512a\u5148\u8655\u7406\u76ee\u524d\u7684\u4e8b\u4ef6\u4e0b\u8981\u5b8c\u6210\u7684\u4efb\u52d9\u3002<\/p>\n\n\n\n<p>\u63db\u53e5\u8a71\u8aaa\uff0c\u4e2d\u65b7\u6a5f\u5236\u5141\u8a31\u7cfb\u7d71\u53ea\u5728\u4e8b\u4ef6\u767c\u751f\u6642\u624d\u9032\u884c\u8655\u7406\uff0c\u9019\u9ebc\u4e00\u4f86\u5c31\u80fd\u7bc0\u7701\u4e0d\u5fc5\u8981\u7684\u8655\u7406\u5668\u904b\u7b97\u548c\u96fb\u529b\u6d88\u8017\uff0c\u800c\u6211\u5011\u4e5f\u80fd\u78ba\u4fdd\u7279\u5b9a\u4e8b\u4ef6\u767c\u751f\u6642\u53ef\u4ee5\u88ab\u5373\u6642\u7684\u8655\u7406\uff0c\u662f\u4e0d\u662f\u4e00\u8209\u591a\u5f97\u5462\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ISR\uff08Interrupt Survice Routine\uff09<\/h3>\n\n\n\n<p>\u5f9e\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u77e5\u9053\u4e2d\u65b7\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u300c\u4e8b\u4ef6\u9a45\u52d5\u300d\u3002\u7576\u6709\u66f4\u512a\u5148\u3001\u66f4\u9700\u8981\u5373\u6642\u8655\u7406\u7684\u4e8b\u4ef6\u767c\u751f\u6642\uff0cCPU\u5c31\u80fd\u5920\u5728\u6700\u77ed\u7684\u6642\u9593\u5167\u53cd\u61c9\u4e26\u4e14\u8655\u7406\u5b8c\u7562\uff0c\u4e0d\u9700\u8981\u7d93\u904e\u8f2a\u8a62\u7684\u7b49\u5f85\u6642\u9593\u3002<\/p>\n\n\n\n<p>\u7576\u4e2d\u65b7\u767c\u751f\u6642\uff0cCPU\u6703\u66ab\u6642\u505c\u6b62\u57f7\u884c\u4e3b\u7a0b\u5f0f\uff0c\u8f49\u800c\u57f7\u884c\u76f8\u61c9\u7684\u8655\u7406\u7a0b\u5e8f\u3002\u7528\u4e2d\u65b7\u7684\u8853\u8a9e\u4f86\u8aaa\uff0c\u9019\u500b\u8655\u7406\u7a0b\u5e8f\u53eb\u505a\u300c<strong>\u4e2d\u65b7\u670d\u52d9\u7a0b\u5e8f<\/strong> (<strong>I<\/strong>nterrupt <strong>S<\/strong>urvice <strong>R<\/strong>outine, ISR)\u300d\u3002 ISR\u662f\u5c08\u9580\u7528\u4f86\u8655\u7406\u4e2d\u65b7\u4e8b\u4ef6\u7684\u4e00\u6bb5\u7279\u6b8a\u51fd\u5f0f\uff0c\u8ca0\u8cac\u8655\u7406\u4e2d\u65b7\u767c\u751f\u5f8c\u8981\u57f7\u884c\u7684\u4efb\u52d9\u6216\u662f\u64cd\u4f5c\uff0c\u5b83\u4e0d\u80fd\u50b3\u5165\u4efb\u4f55\u53c3\u6578\uff0c\u4e5f\u4e0d\u6703\u56de\u50b3\u4efb\u4f55\u5167\u5bb9\u3002\u7576ISR\u5b8c\u6210\u4efb\u52d9\u5f8c\uff0cCPU\u6703\u8fd4\u56de\u4e3b\u7a0b\u5f0f\u7e7c\u7e8c\u57f7\u884c\u539f\u672c\u7684\u7a0b\u5f0f\u3002<\/p>\n\n\n\n<p>ISR\u5177\u6709\u4ee5\u4e0b\u7279\u9ede\uff1a<\/p>\n\n\n\n<ul>\n<li>CPU\u4e00\u6b21\u53ea\u80fd\u8655\u7406\u4e00\u500bISR<\/li>\n\n\n\n<li>\u767c\u751f\u591a\u500b\u4e2d\u65b7\u6642\uff0c\u6703\u4f9d\u7167ISR\u7684\u512a\u5148\u9806\u5e8f\u57f7\u884c<\/li>\n\n\n\n<li>\u6bcf\u500bISR\u90fd\u5c08\u6ce8\u65bc\u8655\u7406\u7279\u5b9a\u7684\u4e8b\u4ef6<\/li>\n\n\n\n<li>ISR\u8d8a\u7c21\u55ae\u8d8a\u597d\uff0c\u8981\u76e1\u53ef\u80fd\u4fdd\u6301\u5f88\u77ed\u7684\u57f7\u884c\u6642\u9593\uff0c\u624d\u80fd\u6e1b\u5c11\u5c0d\u7cfb\u7d71\u7684\u5e72\u64fe<\/li>\n\n\n\n<li>\u8655\u7406\u4e2d\u65b7\u6642\uff0c\u901a\u5e38\u6703\u4f7f\u7528\u5168\u57df\u8b8a\u6578\u5728ISR\u548c\u4e3b\u7a0b\u5f0f\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u70ba\u4e86\u78ba\u4fddISR\u8ddf\u4e3b\u7a0b\u5f0f\u5171\u4eab\u7684\u8b8a\u6578\u53ef\u4ee5\u6b63\u5e38\u66f4\u65b0\uff0c\u6703\u5c07\u8b8a\u6578\u5ba3\u544a\u6210volatile\u578b\u614b\u3002(\u8b93\u8b8a\u6578\u53ef\u4ee5\u88ab\u591a\u500b\u57f7\u884c\u7dd2\u8a2a\u554f)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32\u7684GPIO\u4e2d\u65b7<\/h2>\n\n\n\n<p>ESP32\u7684\u5167\u90e8\u4f7f\u7528\u4e2d\u65b7\u77e9\u9663\u4f86\u8655\u7406\u8a31\u591a\u4e0d\u540c\u7684\u4e2d\u65b7\u4f86\u6e90\u3002<\/p>\n\n\n\n<p>\u6240\u6709ESP32\u80fd\u4f7f\u7528\u7684GPIO\u90fd\u53ef\u4ee5\u62ff\u4f86\u7576\u4f5cGPIO\u4e2d\u65b7\u63a5\u8173\uff0c\u5176\u4e2d10\u96bb\u63a5\u8173\u652f\u63f4\u89f8\u78b0\u611f\u6e2c\u5668\uff08touch sensor\uff09\u4e2d\u65b7\uff08\u8acb\u53c3\u8003\u9032\u968e\u529f\u80fd\u7ae0\u7bc0\uff09\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"ESP32 \u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21260\" width=\"570\" height=\"548\"\/><\/figure><\/div>\n\n\n<p>\u63a5\u4e0b\u4f86\u8aaa\u660e\u4e2d\u65b7\u5e38\u7528\u7684\u8a9e\u6cd5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8a2d\u5b9a\u4e2d\u65b7<\/h3>\n\n\n\n<p>\u5728Arduino IDE\u4e2d\uff0c\u6211\u5011\u900f\u904e\u5c07GPIO\u63a5\u8173\u8207\u5c0d\u61c9\u7684ISR\u51fd\u5f0f\u7d81\u5b9a\u4f86\u555f\u7528\u4e2d\u65b7\uff0c\u8a2d\u5b9a\u4e2d\u65b7\u6642\u53ef\u4ee5\u4f7f\u7528 <code>attachInterrupt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">attachInterrupt<\/mark>(GPIO_pin, ISR, mode);<\/code><\/pre>\n\n\n\n<p>\u53c3\u6578\u8aaa\u660e\uff1a<\/p>\n\n\n\n<ul>\n<li>GPIO_pin: \u5c07\u5176\u8a2d\u5b9a\u70ba\u4e2d\u65b7\u63a5\u8173<\/li>\n\n\n\n<li>ISR: \u7576\u6b64\u4e2d\u65b7\u767c\u751f\u5f8c\u8981\u57f7\u884c\u7684ISR\u51fd\u5f0f<\/li>\n\n\n\n<li>mode: \u5b9a\u7fa9\u4e2d\u65b7\u89f8\u767c\u7684\u689d\u4ef6\u3002\u6709\u516b\u7a2e\u6a21\u5f0f\u53ef\u4ee5\u9078\u64c7\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Interrupt Modes\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">DISABLED  0x00<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">RISING    0x01<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">FALLING   0x02<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">CHANGE    0x03<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ONLOW     0x04<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ONHIGH    0x05<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ONLOW_WE  0x0C<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">#<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">define<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ONHIGH_WE 0x0D<\/mark><\/code><\/pre>\n\n\n\n<p>Tips: <\/p>\n\n\n\n<p>\u5728Arduino-esp32\u6838\u5fc3\u51fd\u5f0f\u5eab\u7684esp32-hal-gpio.h\u6a94\u6848\u4e2d\u53ef\u4ee5\u627e\u5230\u9019\u4e9b\u6a21\u5f0f\u7684\u5b9a\u7fa9\u3002<\/p>\n\n\n\n<p><strong>\u8aaa\u660e<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li>DISABLED: \u7981\u7528GPIO\u4e2d\u65b7<\/li>\n\n\n\n<li>RISING: \u4e0a\u5347\u908a\u6cbf\u89f8\u767c\uff08rising edge trigger\uff09<\/li>\n\n\n\n<li>FALLING: \u4e0b\u964d\u908a\u6cbf\u89f8\u767c\uff08falling edge trigger\uff09<\/li>\n\n\n\n<li>CHANGE: \u4efb\u610f\u908a\u6cbf\u89f8\u767c \uff08any edge trigger\uff09<\/li>\n\n\n\n<li>ONLOW: \u4f4e\u96fb\u4f4d\u89f8\u767c\uff08low level trigger\uff09<\/li>\n\n\n\n<li>ONHIGH: \u9ad8\u96fb\u4f4d\u89f8\u767c\uff08high level trigger\uff09<\/li>\n\n\n\n<li>ONLOW_WE\u548cONHIGH_WE\u662f\u7576ESP32\u5728 Light-sleep\u6a21\u5f0f\u4e0b\u7528\u4f86\u559a\u9192CPU\u7684\u4e2d\u65b7\uff0c\u4e00\u822c\u6bd4\u8f03\u5c11\u4f7f\u7528\u5230\u3002\u201dWE\u201d\u662fwakeup enable\u7684\u7e2e\u5beb\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u95dc\u9589\u4e2d\u65b7<\/h3>\n\n\n\n<p><code>detachInterrupt<\/code>\u8a9e\u6cd5\u53ef\u4ee5\u7528\u4f86\u95dc\u9589\u4e2d\u65b7\uff0c\u53d6\u6d88ESP32\u5c0d\u7279\u5b9a\u63a5\u8173\u7684\u76e3\u807d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>detachInterrupt(GPIO_pin);<\/code><\/pre>\n\n\n\n<p>\u7576\u6211\u5011\u95dc\u9589\u7279\u5b9a\u63a5\u8173\u7684\u4e2d\u65b7\u6642\uff0c\u9700\u8981\u518d\u6b21\u547c\u53eb<code>attachInterrupt<\/code> \u6216\u662f\u91cd\u65b0\u555f\u52d5\u7cfb\u7d71\u624d\u80fd\u91cd\u65b0\u555f\u7528\u4e2d\u65b7\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e2d\u65b7\u670d\u52d9\u7a0b\u5e8f(ISR)<\/strong><\/h3>\n\n\n\n<p>ISR\u51fd\u5f0f\u6703\u5728\u4e2d\u65b7\u88ab\u547c\u53eb\u6642\u57f7\u884c\uff0c\u5b83\u7684\u8a9e\u6cd5\u7d50\u69cb\u770b\u8d77\u4f86\u6703\u50cf\u9019\u6a23\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">void<\/mark> IRAM_ATTR <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ISR<\/mark>() {\n    \/\/ \u4e2d\u65b7\u51fd\u5f0f\u5167\u5bb9\n}<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d<strong>ISR<\/strong>\u662f\u81ea\u8a02\u7684\u51fd\u5f0f\u540d\u7a31\uff0c\u800c<strong>IRAM_ATTR<\/strong>\u7684\u610f\u601d\u662f\u5c07ISR\u51fd\u5f0f\u5b58\u653e\u5728IRAM\uff08Instruction RAM\uff09\uff0c\u800c\u4e0d\u662fflash\u3002\u5f9eIRAM\u8b80\u53d6ISR \u51fd\u5f0f\u53ef\u4ee5\u52a0\u5feb\u8f09\u5165\u901f\u5ea6\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u4f86\uff0c\u8b93\u6211\u5011\u7528\u5be6\u969b\u7684\u7bc4\u4f8b\u4f86\u4e86\u89e3\u5982\u4f55\u4f7f\u7528GPIO\u4e2d\u65b7\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b1\uff1a\u4f7f\u7528\u6309\u9215\u6539\u8b8aLED\u72c0\u614b<\/h2>\n\n\n\n<p>\u9019\u500b\u7bc4\u4f8b\u4f7f\u7528\u6309\u9215\u89f8\u767cESP32\u7684GPIO\u4e2d\u65b7\uff0c\u7576\u6309\u9215\u72c0\u614b\u6709\u8b8a\u5316\uff08\u4e0a\u5347\u6216\u4e0b\u964d\uff09\u6642\uff0cLED\u7684\u72c0\u614b\u4e5f\u6703\u8ddf\u8457\u6539\u8b8a\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"365\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/01\/IMG_1864.gif\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21221\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u6750\u6599\u6e05\u55ae<\/h3>\n\n\n\n<ul>\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/368030501866\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32\u958b\u767c\u677f<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/3680110000434\" target=\"_blank\" rel=\"noreferrer noopener\">TACK-SW<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/categories\/led\" target=\"_blank\" rel=\"noreferrer noopener\">LED<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/categories\/plug-in-resistor\" target=\"_blank\" rel=\"noreferrer noopener\">220\u03a9\u96fb\u963b<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/categories\/plug-in-resistor\" target=\"_blank\" rel=\"noreferrer noopener\">1k\u03a9\u96fb\u963b<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/368070100145\" target=\"_blank\" rel=\"noreferrer noopener\">micro USB \u50b3\u8f38\u7dda<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/368090200053\" target=\"_blank\" rel=\"noreferrer noopener\">\u9eb5\u5305\u677f<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/3681101000174\" target=\"_blank\" rel=\"noreferrer noopener\">\u675c\u90a6\u7dda\uff08\u516c-\u516c\uff09<\/a> x5<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9023\u63a5\u793a\u610f\u5716<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/01\/image-14.png\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21222\" width=\"744\" height=\"539\"\/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h4>\n\n\n\n<div style=\"overflow: auto; height: 500px; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/b5fc859ef7ef642b77e4903874d2dc80.js\"><\/script>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u7a0b\u5f0f\u78bc\u8aaa\u660e<\/h3>\n\n\n\n<ol>\n<li>\u4e00\u958b\u59cb\u6211\u5011\u5148\u5728setup()\u4e2d\uff0c\u7528<code>attachInterrupt<\/code>\u8a2d\u5b9a\u4e2d\u65b7\u689d\u4ef6\u3002 \u5c07btn_pin\u8a2d\u70ba\u4e2d\u65b7\u63a5\u8173\uff0c\u6307\u5b9a<code>toggleLED<\/code>\u70baISR\uff0c\u89f8\u767c\u689d\u4ef6\u70ba<code>CHANGE<\/code>\u3002 \u53ea\u8981\u63a5\u8173\u5075\u6e2c\u5230\u96fb\u4f4d\u72c0\u614b\u7684\u6539\u8b8a\uff0c\u5c31\u6703\u547c\u53eb<code>toggleLED<\/code>\u3002 <br><code>attachInterrupt(btn_pin, toggleLED, CHANGE);<\/code><\/li>\n\n\n\n<li>\u5728<code>toggleLED<\/code>\u4e2d\uff0c\u5982\u679c\u76f4\u63a5\u8b80\u53d6\u6309\u9375\u72c0\u614b\u6703\u7522\u751f\u958b\u95dc\u5f48\u8df3\u7684\u554f\u984c\u3002\u5728\u64b0\u5bebISR\u51fd\u5f0f\u6642\uff0c\u8981\u907f\u514d\u4f7f\u7528delay()\u5ef6\u6642\uff0c\u56e0\u6b64\u6211\u5011\u5728\u7bc4\u4f8b\u7a0b\u5f0f\u4e2d\u4f7f\u7528millis()\u8a08\u6642\uff0c\u907f\u514d\u8b80\u53d6\u5230\u6309\u9215\u4e0d\u7a69\u5b9a\u7684\u72c0\u614b\u3002<\/li>\n\n\n\n<li>\u70ba\u4e86\u78ba\u4fdd\u57f7\u884cISR\u6642\u80fd\u5920\u6b63\u5e38\u66f4\u65b0\u8b8a\u6578\u5167\u5bb9\uff0c\u8b8a\u6578btn_state\u3001previous_time\u3001current_time\u90fd\u5ba3\u544a\u70bavolatile\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u9032\u968e\u529f\u80fd\uff1a\u89f8\u78b0\u611f\u6e2c\u5668\u4e2d\u65b7<\/h2>\n\n\n\n<p>ESP32\u9664\u4e86\u53ef\u4ee5\u4f7f\u7528\u6309\u9215\u6216\u5916\u90e8\u611f\u6e2c\u5668\u89f8\u767cGPIO\u4e2d\u65b7\u4e4b\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528\u5167\u5efa\u7684\u89f8\u78b0\u611f\u6e2c\u5668\/\u96fb\u5bb9\u5f0f\u611f\u6e2c\u5668\uff08Touch sensor\/ Capacitive sensor\uff09\u529f\u80fd\u4f86\u89f8\u767c\u4e2d\u65b7\u3002<\/p>\n\n\n\n<p>\u4e0b\u5716\u4e2d\u6a19\u793a\u70ba\u7c89\u7d05\u8272\u7684\u90fd\u662f\u80fd\u5920\u8b80\u53d6\u89f8\u78b0\u611f\u6e2c\u5668\u7684\u63a5\u8173\uff0c\u5f9eTOUCH_0\uff5eTOUCH_9\uff0c\u4e00\u5171\u670910\u96bb\u63a5\u8173\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2000\" height=\"1001\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/01\/image-15.png\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21223\"\/><\/figure>\n\n\n\n<p>\u6709\u95dc\u89f8\u6478\u63a5\u8173\u7684\u539f\u7406\u6211\u5011\u4ee5\u5f8c\u627e\u6a5f\u6703\u518d\u8a73\u7d30\u4ecb\u7d39\uff0c\u6b64\u8655\u5148\u8aaa\u660e\u5982\u4f55\u8b80\u53d6\u89f8\u78b0\u611f\u6e2c\u5668\u6578\u503c\uff0c\u4e26\u4e14\u85c9\u7531\u624b\u6307\u89f8\u6478\u4f86\u89f8\u767c\u4e2d\u65b7\u8a0a\u865f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53d6\u5f97\u611f\u6e2c\u6578\u503c<\/h3>\n\n\n\n<p>\u5982\u679c\u8981\u53d6\u5f97\u76ee\u524d\u7684\u89f8\u78b0\u611f\u6e2c\u6578\u503c\uff0c\u53ef\u4ee5\u7528<code>touchRead<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touchRead(touch_pin);  \/\/ \u53d6\u5f97\u63a5\u8173\u7684\u611f\u6e2c\u6578\u503c\n<\/code><\/pre>\n\n\n\n<p>\u53c3\u6578\u8aaa\u660e\uff1a<\/p>\n\n\n\n<ul>\n<li>touch_pin: \u50b3\u5165T0~T9\u4e4b\u9593\u7684\u4efb\u610f\u63a5\u8173<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u8a2d\u5b9a\u4e2d\u65b7<\/h3>\n\n\n\n<p>\u4e00\u822c\u7684GPIO\u4e2d\u65b7\u4f7f\u7528<code>attachInterrupt<\/code>\u4f86\u8a2d\u5b9a\u4e2d\u65b7\u4e8b\u4ef6\uff0c\u800c\u89f8\u78b0\u63a5\u8173\u5247\u662f\u4f7f\u7528<code>touchAttachInterrupt<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touchAttachInterrupt(touch_pin, touch_ISR, threshold);\n<\/code><\/pre>\n\n\n\n<p>\u53c3\u6578\u8aaa\u660e\uff1a<\/p>\n\n\n\n<ul>\n<li>touch_pin: \u50b3\u5165T0~T9\u4e4b\u9593\u7684\u4efb\u610f\u63a5\u8173<\/li>\n\n\n\n<li>touch_ISR: \u89f8\u78b0\u4e8b\u4ef6\u7684ISR\u51fd\u5f0f<\/li>\n\n\n\n<li>threshold: \u547c\u53eb\u4e2d\u65b7\u7684\u81e8\u754c\u503c\uff0c\u5c0f\u65bc\u81e8\u754c\u503c\u8868\u793a\u5075\u6e2c\u5230\u89f8\u78b0\u3002\u53ef\u4ee5\u85c9\u7531<code>touchRead<\/code>\u7684\u7d50\u679c\u8a2d\u5b9a\u6b64\u6578\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b2\uff1a\u89f8\u78b0\u4e2d\u65b7<\/h2>\n\n\n\n<p>\u9019\u500b\u7bc4\u4f8b\u4f7f\u7528\u5c0e\u7dda\u4f86\u5075\u6e2c\u624b\u6307\u662f\u5426\u78b0\u89f8\u5230\u63a5\u8173\u3002\u7576\u624b\u6307\u89f8\u78b0\u5230\u5c0e\u7dda\u6642\uff0c\u5c31\u6703\u547c\u53eb\u4e2d\u65b7\u51fd\u5f0f\uff0c\u9ede\u4eae\u958b\u767c\u677f\u4e0a\u9762\u7684LED\u71c8\uff0c\u4e26\u4e14\u5728\u5e8f\u5217\u57e0\u76e3\u63a7\u8996\u7a97\u986f\u793a\u6578\u503c\u548c\u7d50\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6750\u6599\u6e05\u55ae<\/h3>\n\n\n\n<ul>\n<li>ESP32\u958b\u767c\u677f x1<\/li>\n\n\n\n<li>micro USB \u50b3\u8f38\u7dda x1<\/li>\n\n\n\n<li>\u9eb5\u5305\u677f x1<\/li>\n\n\n\n<li>\u675c\u90a6\u7dda\uff08\u516c-\u516c\uff09 x1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9023\u63a5\u793a\u610f\u5716<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/01\/image-16.png\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21224\" width=\"935\" height=\"756\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h3>\n\n\n\n<div style=\"overflow: auto; height: 600px; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/150fcd3a9df64890f4c8e61886cb7316.js\"><\/script>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u57f7\u884c\u7d50\u679c<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"391\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2024\/01\/IMG_1865.gif\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u5341\u4e8c)\uff1a\u786c\u9ad4\u4e2d\u65b7\" class=\"wp-image-21225\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading alignwide\">\u5c0f\u7d50<\/h2>\n\n\n\n<p>\u786c\u9ad4\u4e2d\u65b7\u53ef\u4ee5\u7528\u4f86\u5075\u6e2c\u63a5\u8173\u72c0\u614b\u7684\u8b8a\u5316\uff0c\u5b83\u8b93\u5fae\u63a7\u5236\u5668\u7684CPU\u4e0d\u7528\u4e00\u76f4\u5728\u4e3b\u7a0b\u5f0f\u5faa\u74b0\u4e2d\u53cd\u8986\u6aa2\u67e5GPIO\u7684\u72c0\u614b\uff0c\u4e5f\u80fd\u5920\u5728\u767c\u751f\u8b8a\u5316\u6642\u7acb\u5373\u505a\u51fa\u53cd\u61c9\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86ESP32\u7684\u5169\u7a2e\u5e38\u898b\u7684GPIO\u4e2d\u65b7\uff0c\u4e5f\u900f\u904e\u5be6\u969b\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\u8aaa\u660e\u4e2d\u65b7\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e38\u898b\u7684\u5fae\u63a7\u5236\u5668\u7cfb\u5217\uff0c\u4f8b\u5982Arduino\u7684ATmega\u7cfb\u5217\uff08\u50cf\u662fATmega328P\uff09\u3001STM32\u7cfb\u5217\u3001PIC\u7cfb\u5217\uff0c\u6216\u662fESP32\uff0c\u901a\u5e38\u90fd\u652f\u63f4\u591a\u7a2e\u4e0d\u540c\u7684\u4e2d\u65b7\u985e\u578b\u3002ESP32\u53ef\u4ee5\u7528\u786c\u9ad4\u4e2d\u65b7\u4f86\u8655\u7406\u5916\u90e8\u786c\u9ad4\u6216\u6a21\u7d44\u5f15\u8d77\u7684\u4e2d\u65b7\uff0c\u50cf\u662fGPIO\u6216\u662f\u89f8\u78b0\uff08TOUGH\uff09\u4e2d\u65b7\uff1b\u7528\u8edf\u9ad4\u4e2d\u65b7\u8655\u7406\u5b9a\u6642\u5668\u4e2d\u65b7\uff0c\u672c\u7bc7\u4e3b\u8981\u8aaa\u660e\u7684\u662f\u786c\u9ad4\u4e2d\u65b7\u3002<\/p>\n","protected":false},"author":1,"featured_media":21431,"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\/21219"}],"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=21219"}],"version-history":[{"count":10,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/21219\/revisions"}],"predecessor-version":[{"id":21434,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/21219\/revisions\/21434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/media\/21431"}],"wp:attachment":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=21219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=21219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=21219"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/table_tags?post=21219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}