{"id":6285,"date":"2019-11-04T09:12:40","date_gmt":"2019-11-04T01:12:40","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=6285"},"modified":"2023-10-02T11:20:27","modified_gmt":"2023-10-02T03:20:27","slug":"nanopi-gpio-%e6%8e%a7%e5%88%b6%e5%a4%96%e6%8e%a5-led","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=6285","title":{"rendered":"NanoPi - GPIO \u63a7\u5236\u5916\u63a5 LED"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone wp-image-6270 size-medium\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio-300x123.png\" alt=\"\" width=\"300\" height=\"123\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio-300x123.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio-768x315.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio-1024x420.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio-500x205.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/nanopi_gpio.png 1205w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u5be6\u9ad4\u6e2c\u8a66\u88dd\u7f6e\u9577\u9019\u6a23<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone wp-image-6290 size-medium\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED-300x209.png\" alt=\"\" width=\"300\" height=\"209\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED-300x209.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED-768x534.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED-1024x712.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED-431x300.png 431w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2019\/11\/NanoPi_GPIO_LED.png 1280w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<h2>\u4f7f\u7528 \/sys\/class\/gpio - shell command<\/h2>\n<ul>\n<li><a class=\"\" href=\"https:\/\/www.twblogs.net\/a\/5bb00eff2b7177781a0facf7\" target=\"_blank\" rel=\"noopener\"><span class=\"original\">\u539f\u521b<\/span> linux\u64cd\u4f5cGPIO\u901a\u7528sysfs\u65b9\u6cd5 <\/a><\/li>\n<li><a href=\"https:\/\/my.oschina.net\/dingdayu\/blog\/1983075\" target=\"_blank\" rel=\"noopener\">GPIO\u64cd\u4f5c\u539f\u7406(Linux\u4e00\u5207\u7686\u6587\u4ef6)<\/a><\/li>\n<li><a href=\"https:\/\/zh.wikipedia.org\/wiki\/Sysfs\" target=\"_blank\" rel=\"noopener\">Sysfs<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\"># echo 12 &gt; \/sys\/class\/gpio\/export\r\n# cd \/sys\/class\/gpio\/gpio12\r\n# echo out &gt; direction\r\n# echo 1 &gt; value<\/pre>\n<p>--<\/p>\n<h2>\u4f7f\u7528 \/sys\/class\/gpio - Python 3<\/h2>\n<pre class=\"lang:python decode:true\">import gpio\r\nimport time\r\n\r\nledPin = 12\r\ngpio.setup(ledPin, 'out')\r\n\r\ngpio.set(ledPin, 1)\r\ntime.sleep(1)\r\ngpio.set(ledPin, 0)<\/pre>\n<p>--<\/p>\n<h2>\u6548\u80fd\u5be6\u9a57<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u7684 python \u7684\u7a0b\u5f0f\u6e2c\u8a66\uff0c\u8ff4\u5708\u8dd1 1000 \u6b21 led \u958b\u95dc<\/p>\n<pre class=\"lang:python decode:true\">import gpio\r\nimport time\r\n\r\nledPin = 12\r\ngpio.setup(ledPin, 'out')\r\ni = 0\r\ntStart = time.time()\r\n\r\nwhile 1:\r\n    if i &gt;= 1000 :\r\n        tEnd = time.time()\r\n        break\r\n    gpio.set(ledPin, 1)\r\n    # time.sleep(1)\r\n    gpio.set(ledPin, 0)\r\n    # time.sleep(1)\r\n    i = i+1\r\n    print(i)\r\n\r\nprint(tEnd - tStart)<\/pre>\n<p>\u76ee\u524d\u7684\u7a0b\u5f0f\u9700\u8981 1.16 \u79d2<\/p>\n<pre class=\"lang:default decode:true\">DEBUG:gpio:Write 12: 1\r\nDEBUG:gpio:writing: &lt;_io.TextIOWrapper name='\/sys\/class\/gpio\/gpio12\/value' mode='w+' encoding='UTF-8'&gt;: 1\r\nDEBUG:gpio:Write 12: 0\r\nDEBUG:gpio:writing: &lt;_io.TextIOWrapper name='\/sys\/class\/gpio\/gpio12\/value' mode='w+' encoding='UTF-8'&gt;: 0\r\n1000\r\n1.1629619598388672<\/pre>\n<p>\u56e0\u70ba\u89ba\u5f97\u90a3\u500b DEBUG.gpio \u7684\u8f38\u51fa\u5f88\u7919\u773c\uff0c\u6240\u4ee5\u8907\u88fd\u4e86 gpio.py \u7136\u5f8c\u5c07\u6240\u6709 logging \u8f38\u51fa\u522a\u9664\uff0c\u53ef\u4ee5\u5f97\u5230 0.10 \u79d2<\/p>\n<pre class=\"lang:default decode:true \">998\r\n999\r\n1000\r\n0.10581040382385254<\/pre>\n<p>... \u6240\u4ee5\u5df2\u7d93\u77e5\u9053\u8a0a\u606f\u8f38\u51fa\u6703\u5f71\u97ff\u6548\u80fd\uff0c\u90a3\u628a print(i) \u4e5f\u79fb\u9664\u5427<\/p>\n<pre class=\"lang:default decode:true \">0.07267355918884277<\/pre>\n<p>\u597d\u5427\uff0c\u6211\u77e5\u9053 GPIO \u539f\u672c\u6548\u80fd\u4e0d\u5f70\u7684\u539f\u56e0\u4e86<\/p>\n<p>--<\/p>\n<h2>\u901f\u7387 &amp; \u6642\u9593<\/h2>\n<ul>\n<li>2400 = 0.000417<\/li>\n<li>9600 = 0.0001<\/li>\n<li>115200 = 0.0000087<\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"6285\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,533&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u5be6\u9ad4\u6e2c\u8a66\u88dd\u7f6e\u9577\u9019...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"6285\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,533&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[308],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6285"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6285"}],"version-history":[{"count":8,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6285\/revisions"}],"predecessor-version":[{"id":6360,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6285\/revisions\/6360"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}