{"id":68666,"date":"2020-09-25T16:25:26","date_gmt":"2020-09-25T08:25:26","guid":{"rendered":"http:\/\/www.xiaoyebailong.com\/?p=68666"},"modified":"2020-09-25T16:31:21","modified_gmt":"2020-09-25T08:31:21","slug":"arduino%e7%9c%8b%e9%97%a8%e7%8b%97%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.xiaoyebailong.com\/index.php\/2020\/09\/25\/68666.htm","title":{"rendered":"Arduino\u770b\u95e8\u72d7\u7684\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u4f7f\u7528Arduino UNO\u7684\u770b\u95e8\u72d7\uff1a<\/p>\n\n\n\n<p>&nbsp; &nbsp; \u4ecb\u7ecd\uff1a\u770b\u95e8\u72d7\u5b9a\u65f6\u5668\u7528\u907f\u514d\u7535\u5b50\u8bbe\u5907\u51fa\u73b0\u6545\u969c\u9020\u6210\u6b7b\u673a\uff0c\u4f8b\u5982\u6b7b\u5faa\u73af\u3002\u4e3a\u6b64\uff0c\u770b\u95e8\u72d7\u4f5c\u4e3a\u4e00\u4e2a\u7269\u7406\u5b9a\u65f6\u5668\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4e0d\u65ad\u7684\u589e\u52a0\uff0c\u5e76\u9a71\u52a8\u4e00\u4e2a\u4e2d\u65ad\uff0c \u5982\u679c\u8fbe\u5230\u67d0\u4e00\u4e2a\u8bbe\u5b9a\u7684\u503c\uff0c\u5c31\u4f1a\u590d\u4f4d\u5fae\u5904\u7406\u5668\u3002\u5728\u6b63\u5e38\u64cd\u4f5c\u4e0b\uff0c\u5b9a\u65f6\u5668\u5728\u7a0b\u5e8f\u6d41\u7a0b\u4e2d\u7684\u5b9a\u671f\u8bbe\u7f6e\u4e3a0\uff0c \u4f46\u5982\u679c\u4ee3\u7801\u8fdb\u5165\u6b7b\u5faa\u73af\uff0c\u5b9a\u65f6\u5668\u590d\u4f4d\u4e0d\u4f1a\u53d1\u751f\uff0c\u770b\u95e8\u72d7\u89e6\u53d1\u4e2d\u65ad\u5e76\u91cd\u65b0\u542f\u52a8\u3002<\/p>\n\n\n\n<p>\u00a0 \u00a0 \u4f7f\u7528\u770b\u95e8\u72d7\u9632\u6b62\u6b7b\u673a\uff1aArduino\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u65b9\u4fbf\u64cd\u4f5c\u7684\u51fd\u6570\u548c\u5b8f\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f15\u5165\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;avr\/wdt.h><\/code><\/pre>\n\n\n\n<p>\u00a0 \u00a0 \u8fd9\u4e2a\u51fd\u6570\u5e93\u63d0\u4f9b\u4e86\u4e09\u4e2aAPI\u51fd\u6570\u4f9b\u6211\u4eec\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wdt_enable(timeout) \/\/\u770b\u95e8\u72d7\u4f7f\u80fd\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\nwdt_disable() \/\/\u5173\u95edwdt_reset() \/\/\u590d\u4f4d<\/code><\/pre>\n\n\n\n<p>timeout\u53ef\u4ee5\u4f7f\u7528wdt.h\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u5b8f\u5e38\u91cf\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65f6\u95f4\uff1b<\/p>\n\n\n\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5728setup\uff08\uff09\u51fd\u6570\u4e2d\u4f7f\u80fd\u770b\u95e8\u72d7\u5b9a\u65f6\u5668\uff0c\u5e76\u5728loop\uff08\uff09\u4e2d\u4e0d\u65ad\u7684\u5582\u72d7\u9632\u6b62\u6ea2\u51fa\uff0c\u5b98\u65b9\u7ed9\u51fa\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;avr\/wdt.h>\nvoid setup()\n{\n   pinMode(13,OUTPUT);\n   wdt_enable(WDTO_2S); \/\/\u5f00\u542f\u770b\u95e8\u72d7\uff0c\u5e76\u8bbe\u7f6e\u6ea2\u51fa\u65f6\u95f4\u4e3a\u4e24\u79d2\n}\n \nvoid loop()\n{\n   digitalWrite(13,HIGH);\n   delay(100);\n   digitalWrite(13,LOW);\n   delay(100);\n   wdt_reset(); \/\/\u5582\u72d7\u64cd\u4f5c\uff0c\u4f7f\u770b\u95e8\u72d7\u5b9a\u65f6\u5668\u590d\u4f4d\n}<\/code><\/pre>\n\n\n\n<p>\u6ce8\uff1a\u770b\u95e8\u72d7\u4e5f\u53ef\u4ee5\u5524\u9192\u7701\u7535\u6a21\u5f0f\u4e0b\u7684 Arduino\uff0c \u8be6\u7ec6\u8bf4\u660e\u53ef\u67e5\u9605<a rel=\"nofollow\" href=\"https:\/\/playground.arduino.cc\/Learning\/ArduinoSleepCode\">\u5b98\u65b9\u6587\u6863<\/a>\uff0c\u7279\u522b\u6307\u51fa\u7684\u662f\uff0c\u5982\u679c\u4f7f\u7528POWER_MODE_IDLE\u6a21\u5f0f\uff08UART\u3001SPI\u3001timer\u7b49\u5904\u4e8e\u5de5\u4f5c\u72b6\u6001\uff09\uff0c\u9700\u5728\u8fdb\u5165\u4f11\u7720\u72b6\u6001\u524d\u5173\u95ed\u770b\u95e8\u72d7\u529f\u80fd\uff0c\u4f11\u7720\u7ed3\u675f\u4e4b\u540e\u518d\u6b21\u542f\u52a8\uff0c\u5426\u5219\u51e0\u79d2\u4e4b\u540e\u770b\u95e8\u72d7\u4f1a\u5f3a\u5236\u91cd\u542fAruino\uff0c \u4f11\u7720\u5931\u8d25\u3002\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/* Sleep Demo Serial\n * -----------------\n * Example code to demonstrate the sleep functions in a Arduino. Arduino will wake up\n * when new data is received in the serial port USART\n * Based on Sleep Demo Serial from http:\/\/www.arduino.cc\/playground\/Learning\/ArduinoSleepCode\n *\n * Copyright \u00a9 2006 MacSimski 2006-12-30\n * Copyright \u00a9 2007 D. Cuartielles 2007-07-08 - Mexico DF\n *\n * With modifications from Ruben Laguna 2008-10-15\n *\n * This program is free software: you can redistribute it and\/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see &lt;http:\/\/www.gnu.org\/licenses\/>.\n *\n *\/\n \n#include &lt;avr\/power.h>\n#include &lt;avr\/sleep.h>\n \nint sleepStatus = 0; \/\/ variable to store a request for sleep\nint count = 0; \/\/ counter\n \nvoid setup()\n{\n \nSerial.begin(9600);\n}\n \nvoid sleepNow()\n{\n\/* Now is the time to set the sleep mode. In the Atmega8 datasheet\n * http:\/\/www.atmel.com\/dyn\/resources\/prod_documents\/doc2486.pdf on page 35\n * there is a list of sleep modes which explains which clocks and\n * wake up sources are available in which sleep modus.\n *\n * In the avr\/sleep.h file, the call names of these sleep modus are to be found:\n *\n * The 5 different modes are:\n * SLEEP_MODE_IDLE -the least power savings\n * SLEEP_MODE_ADC\n * SLEEP_MODE_PWR_SAVE\n * SLEEP_MODE_STANDBY\n * SLEEP_MODE_PWR_DOWN -the most power savings\n *\n * the power reduction management &lt;avr\/power.h> is described in\n * http:\/\/www.nongnu.org\/avr-libc\/user-manual\/group_avr_power.html\n *\/\n \nset_sleep_mode(SLEEP_MODE_IDLE); \/\/ sleep mode is set here\n \nsleep_enable(); \/\/ enables the sleep bit in the mcucr register\n\/\/ so sleep is possible. just a safety pin\n \npower_adc_disable();\npower_spi_disable();\npower_timer0_disable();\npower_timer1_disable();\npower_timer2_disable();\npower_twi_disable();\nwdt_disable(); \/\/close wdt before Arduino enter sleep mode\n \nsleep_mode(); \/\/ here the device is actually put to sleep!!\n \n\/\/ THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP\nsleep_disable(); \/\/ first thing after waking from sleep:\n\/\/ disable sleep...\nwdt_enable(timeout); \/\/open wdt after wakine from sleep\npower_all_enable();\n \n}\n \nvoid loop()\n{\n\/\/ display information about the counter\nSerial.print(\"Awake for \");\nSerial.print(count);\nSerial.println(\"sec\");\ncount++;\ndelay(1000); \/\/ waits for a second\n \n\/\/ compute the serial input\nif (Serial.available()) {\nint val = Serial.read();\nif (val  'S') {\n      Serial.println(\"Serial: Entering Sleep mode\");\n      delay(100);     \/\/ this delay is needed, the sleep\n                      \/\/function will provoke a Serial error otherwise!!\n      count = 0;\n      sleepNow();     \/\/ sleep function called here\n    }\n    if (val  'A') {\nSerial.println(\"Hola Caracola\"); \/\/ classic dummy message\n}\n}\n \n\/\/ check if it should go asleep because of time\nif (count >= 10) {\nSerial.println(\"Timer: Entering Sleep mode\");\ndelay(100); \/\/ this delay is needed, the sleep\n\/\/function will provoke a Serial error otherwise!!\ncount = 0;\nsleepNow(); \/\/ sleep function called here\n}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Arduino UNO\u7684\u770b\u95e8\u72d7\uff1a &nbsp; &nbsp; \u4ecb\u7ecd\uff1a\u770b\u95e8\u72d7\u5b9a\u65f6\u5668\u7528\u907f\u514d\u7535\u5b50\u8bbe\u5907\u51fa\u73b0\u6545\u969c\u9020\u6210\u6b7b\u673a\uff0c\u4f8b\u5982\u6b7b\u5faa\u73af\u3002\u4e3a\u6b64\uff0c\u770b\u95e8\u72d7\u4f5c\u4e3a\u4e00\u4e2a\u7269\u7406\u5b9a\u65f6\u5668\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4e0d\u65ad\u7684\u589e\u52a0\uff0c\u5e76\u9a71\u52a8\u4e00\u4e2a\u4e2d\u65ad\uff0c \u5982\u679c\u8fbe\u5230\u67d0\u4e00\u4e2a\u8bbe\u5b9a\u7684\u503c\uff0c\u5c31\u4f1a\u590d\u4f4d\u5fae\u5904\u7406&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159],"tags":[],"topic":[],"class_list":["post-68666","post","type-post","status-publish","format-standard","hentry","category-moshuiping"],"_links":{"self":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/68666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/comments?post=68666"}],"version-history":[{"count":3,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/68666\/revisions"}],"predecessor-version":[{"id":68669,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/68666\/revisions\/68669"}],"wp:attachment":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/media?parent=68666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/categories?post=68666"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/tags?post=68666"},{"taxonomy":"topic","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/topic?post=68666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}