{"id":306,"date":"2017-02-04T16:08:57","date_gmt":"2017-02-04T08:08:57","guid":{"rendered":"http:\/\/192.168.1.20\/?p=306"},"modified":"2017-02-04T16:20:56","modified_gmt":"2017-02-04T08:20:56","slug":"{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}e4{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}bd{47dd41ec6bdcbd9a214c783a3735392b82ea6d1d918378c5a53e0640702d813","status":"publish","type":"post","link":"http:\/\/alextop.top\/?p=306","title":{"rendered":"\u4f7f\u7528 RPi.GPIO \u6a21\u5757\u7684\u8f93\u5165\uff08Input\uff09\u529f\u80fd"},"content":{"rendered":"<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u8fd9\u7bc7\u65e5\u5fd7\u7684\u5185\u5bb9\u5e94\u8be5\u7b97\u662f\u300a<\/span><span style=\"color: #3390ca;\"><a href=\"http:\/\/sxfu.tk\/index.php\/archives\/294\"><span style=\"font-family: Times;\">RPi.GPIO <\/span><\/a><span style=\"font-family: \u5b8b\u4f53;\">\u6a21\u5757\u4f7f\u7528\u57fa\u7840<span style=\"color: #333a42;\">\u300b<\/span><\/span><span style=\"font-family: Times;\">Input <\/span><span style=\"color: #333a42;\"><span style=\"font-family: \u5b8b\u4f53;\">\u90e8\u5206\u7684\u6269\u5c55\u8bb2\u89e3\uff0c\u8be6\u7ec6\u8bb2\u89e3\u4e86<\/span><span style=\"font-family: Times;\"> Input <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u90e8\u5206\u7684\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u6280\u5de7\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u76ee\u524d\u6709\u51e0\u79cd\u9014\u5f84\u53ef\u4ee5\u5728\u60a8\u7684\u7a0b\u5e8f\u4e2d\u83b7\u5f97<\/span><span style=\"font-family: Times;\"> GPIO <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u8f93\u5165\u4fe1\u606f\u3002\u7b2c\u4e00\u79cd\u4e5f\u662f\u6700\u7b80\u6613\u7684\u4e00\u79cd\u4e3a\u5728\u67d0\u4e2a\u65f6\u95f4\u70b9\u68c0\u67e5\u8f93\u5165\u503c\u3002\u8fd9\u5373\u662f\u6240\u8c13\u7684<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u8f6e\u8be2\uff08<\/span><span style=\"font-family: Times;\">polling<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff0c\u800c\u4e14\u5982\u679c\u60a8\u7684\u7a0b\u5e8f\u5728\u9519\u8bef\u7684\u65f6\u95f4\u91cc\u8fdb\u884c\u4e86\u8bfb\u53d6\uff0c\u53ef\u80fd\u4f1a\u9519\u8fc7\u67d0\u4e2a\u8f93\u5165\u503c\u3002\u5728\u5faa\u73af\u4e2d\u8fd0\u7528\u8f6e\u8be2\uff0c\u6709\u53ef\u80fd\u4f7f\u5904\u7406\u5668\u8d44\u6e90\u7d27\u5f20\u3002\u53e6\u4e00\u79cd\u5bf9<\/span><span style=\"font-family: Times;\"> GPIO <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u8f93\u5165\u8fdb\u884c\u54cd\u5e94\u7684\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u7528<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e2d\u65ad\uff08<\/span><span style=\"font-family: Times;\">interruots<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u8fb9\u7f18\u68c0\u6d4b\uff08<\/span><span style=\"font-family: Times;\">edge detection<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09\uff09\u3002\u8fb9\u7f18\u53ef\u4ee5\u662f\u4ece<\/span><span style=\"font-family: Times;\"> HIGH <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5230<\/span><span style=\"font-family: Times;\"> LOW <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u8fc7\u5ea6\uff08\u4e0b\u964d\u4e34\u754c\u503c\uff08<\/span><span style=\"font-family: Times;\">falling edge<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09\uff09\u6216\u4ece<\/span><span style=\"font-family: Times;\"> LOW <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5230<\/span><span style=\"font-family: Times;\"> HIGH <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u8fc7\u5ea6\uff08\u4e0a\u5347\u4e34\u754c\u503c\uff08<\/span><span style=\"font-family: Times;\">rising edge<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09\uff09\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: \u5b8b\u4f53;\">\u4e0a\u62c9<\/span><span style=\"font-family: Arial;\">\/<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e0b\u62c9\u7535\u963b<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u5982\u679c\u60a8\u5728\u8f93\u5165\u9488\u811a\u4e0a\u6ca1\u6709\u8fde\u63a5\u4efb\u4f55\u5143\u4ef6\uff0c\u90a3\u4e48\u5b83\u5c06\u662f<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u6d6e\u52a8\uff08<\/span><span style=\"font-family: Times;\">float<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u56e0\u4e3a\u60a8\u6ca1\u6709\u8fde\u63a5\u4efb\u4f55\u5143\u4ef6\uff0c\u5728\u6309\u4e0b\u6309\u94ae\u6216\u5f00\u5173\u4e4b\u524d\uff0c\u8bfb\u53d6\u7684\u503c\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002\u7531\u4e8e\u7535\u6e90\u7684\u6ce2\u52a8\uff0c\u83b7\u53d6\u5230\u7684\u503c\u53ef\u80fd\u4f1a\u6709\u5f88\u5927\u7684\u53d8\u5316\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e0a\u62c9<\/span><span style=\"font-family: Times;\">\/<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e0b\u62c9\u7535\u963b\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u8bbe\u5b9a\u8f93\u5165\u7684\u9ed8\u8ba4\u503c\u4e86\u3002\u5728\u8fd9\u91cc\uff0c\u53ef\u4ee5\u4f7f\u7528\u786c\u4ef6\u6216\u8f6f\u4ef6\u5bf9\u7535\u963b\u8fdb\u884c\u4e0a\u62c9<\/span><span style=\"font-family: Times;\">\/<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e0b\u62c9\u3002\u4f7f\u7528\u786c\u4ef6\u65b9\u5f0f\uff0c\u5c06\u4e00\u4e2a<\/span><span style=\"font-family: Times;\"> 10K <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u7535\u963b\u8fde\u63a5\u5728\u8f93\u5165\u901a\u9053\u4e0e<\/span><span style=\"font-family: Times;\"> 3.3V<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u4e0a\u62c9\uff09\u6216<\/span><span style=\"font-family: Times;\"> 0V<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u4e0b\u62c9\uff09\u4e4b\u95f4\u662f\u5e38\u7528\u7684\u505a\u6cd5\u3002\u800c<\/span><span style=\"font-family: Times;\"> RPi.GPIO <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e5f\u5141\u8bb8\u60a8\u901a\u8fc7\u8f6f\u4ef6\u7684\u65b9\u5f0f\u5bf9\u914d\u7f6e<\/span><span style=\"font-family: Times;\"> Broadcom SOC <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u6765\u8fbe\u5230\u76ee\u7684\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u6216\u8005<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u901a\u9053\u7f16\u53f7\u662f\u57fa\u4e8e\u60a8\u6240\u4f7f\u7528\u7684\u7f16\u53f7\u7cfb\u7edf\u6240\u6307\u5b9a\u7684\uff08<\/span><span style=\"font-family: Times;\">BOARD <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u6216<\/span><span style=\"font-family: Times;\"> BCM<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09\u3002\uff09<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: \u5b8b\u4f53;\">\u8f93\u5165\u6d4b\u8bd5\uff08\u8f6e\u8be2\uff08<\/span><span style=\"font-family: Arial;\">polling<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09\uff09<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u60a8\u53ef\u4ee5\u5728\u67d0\u4e2a\u65f6\u95f4\u70b9\u83b7\u5f97\u4e00\u6b21\u8f93\u5165\u7684\u5feb\u7167\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">if GPIO.input(channel):<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\"> print(&#8216;Input was HIGH&#8217;)<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">else:<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\"> print(&#8216;Input was LOW&#8217;)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u5faa\u73af\u4e2d\u7b49\u5f85\u6309\u94ae\u88ab\u6309\u4e0b\u540e\u8fdb\u884c\u8f6e\u8be2\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">while GPIO.input(channel) == GPIO.LOW:<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"> time.sleep(0.01) # <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e3a<\/span><span style=\"font-family: Consolas;\"> CPU <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7559\u51fa<\/span><span style=\"font-family: Consolas;\"> 10 <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u6beb\u79d2\uff0c\u4f9b\u5176\u5904\u7406\u5176\u5b83\u4e8b\u7269<\/span><span style=\"font-family: Consolas;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u8fd9\u91cc\u5047\u8bbe\u4e3a\u5f53\u6309\u4e0b\u6309\u94ae\u65f6\uff0c\u8f93\u5165\u72b6\u6001\u4ece<\/span><span style=\"font-family: Times;\"> LOW <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5230<\/span><span style=\"font-family: Times;\"> HIGH<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: \u5b8b\u4f53;\">\u4e2d\u65ad\u548c\u8fb9\u68c0\u68c0\u6d4b<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u8fb9\u7f18\u7684\u5b9a\u4e49\u4e3a\u7535\u4fe1\u53f7\u4ece<\/span><span style=\"font-family: Times;\"> LOW <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5230<\/span><span style=\"font-family: Times;\"> HIGH<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u4e0a\u5347\u4e34\u754c\u503c\uff09\u6216\u4ece<\/span><span style=\"font-family: Times;\"> HIGH <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5230<\/span><span style=\"font-family: Times;\"> LOW<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff08\u4e0b\u964d\u4e34\u754c\u503c\uff09\u72b6\u6001\u7684\u6539\u53d8\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u5bf9\u4e8e\u8f93\u5165\u7684\u503c\u6765\u8bf4\uff0c\u6211\u4eec\u66f4\u5173\u5fc3\u7684\u662f\u8f93\u5165\u7684\u72b6\u6001\u662f\u5426\u53d1\u751f\u4e86\u6539\u53d8\u3002\u8fd9\u79cd\u72b6\u6001\u4e0a\u7684\u6539\u53d8\u662f\u5f88\u91cd\u8981\u7684\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u4e3a\u4e86\u907f\u514d\u60a8\u7684\u7a0b\u5e8f\u5728\u5fd9\u4e8e\u5904\u7406\u5176\u5b83\u7684\u4e8b\u7269\u65f6\u800c\u9519\u8fc7\u4e86\u60a8\u6309\u4e0b\u6309\u94ae\u7684\u64cd\u4f5c\uff0c\u8fd9\u91cc\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: Times;\">wait_for_edge() <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u51fd\u6570<\/span><span style=\"font-family: Times;\"><br \/>\nevent_detected() <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u51fd\u6570<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u68c0\u6d4b\u5230\u8fb9\u7f18\u65f6\u6267\u884c\u7ebf\u7a0b\u56de\u8c03\u51fd\u6570<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: Arial;\">wait_for_edge() <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u51fd\u6570<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: Times;\">wait_for_edge() <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u51fd\u6570\u88ab\u8bbe\u8ba1\u7528\u4e8e\u5728\u68c0\u6d4b\u5230\u8fb9\u7f18\u4e4b\u524d\u963b\u6b62\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u7b49\u5f85\u6309\u94ae\u88ab\u6309\u4e0b\u7684\u8bed\u53e5\u53ef\u4ee5\u6539\u5199\u4e3a\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.wait_for_edge(channel, GPIO.RISING)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u6ce8\u610f\uff0c\u60a8\u53ef\u4ee5\u8f93\u5165<\/span><span style=\"font-family: Times;\"> GPIO.RISING<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u3001<\/span><span style=\"font-family: Times;\">GPIO.FALLING<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u3001<\/span><span style=\"font-family: Times;\">GPIO.BOTH <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5bf9\u8fb9\u7f18\u8fdb\u884c\u68c0\u6d4b\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u662f\u5360\u7528<\/span><span style=\"font-family: Times;\"> CPU <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u8d44\u6e90\u5f88\u5c11\uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u6709\u5145\u88d5\u7684\u8d44\u6e90\u5904\u7406\u5176\u5b83\u4e8b\u7269\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: Arial;\">event_detected() <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u51fd\u6570<\/span><span style=\"font-family: Arial;\"> event_detected() <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u51fd\u6570\u88ab\u8bbe\u8ba1\u7528\u4e8e\u5faa\u73af\u4e2d\u6709\u5176\u5b83\u4e1c\u897f\u65f6\u4f7f\u7528\uff0c\u4f46\u4e0d\u540c\u4e8e\u8f6e\u8be2\u7684\u662f\uff0c\u5b83\u4e0d\u4f1a\u9519\u8fc7\u5f53<\/span><span style=\"font-family: Arial;\"> CPU <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5fd9\u4e8e\u5904\u7406\u5176\u5b83\u4e8b\u7269\u65f6\u8f93\u5165\u72b6\u6001\u7684\u6539\u53d8\u3002\u8fd9\u5728\u7c7b\u4f3c\u4f7f\u7528<\/span><span style=\"font-family: Arial;\"> Pygame <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u6216<\/span><span style=\"font-family: Arial;\"> PyQt <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u65f6\u4e3b\u5faa\u73af\u5b9e\u65f6\u76d1\u542c\u548c\u54cd\u5e94<\/span><span style=\"font-family: Arial;\"> GUI <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u4e8b\u4ef6\u662f\u5f88\u6709\u7528\u7684\u3002<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\">GPIO.add_event_detect(channel, GPIO.RISING) # <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u901a\u9053\u4e0a\u6dfb\u52a0\u4e0a\u5347\u4e34\u754c\u503c\u68c0\u6d4b<\/span><span style=\"font-family: Consolas;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">do_something()<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">if GPIO.event_detected(channel):<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\"> print(&#8216;Button pressed&#8217;)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u6ce8\u610f\uff0c\u60a8\u53ef\u4ee5\u8f93\u5165<\/span><span style=\"font-family: Times;\"> GPIO.RISING<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u3001<\/span><span style=\"font-family: Times;\">GPIO.FALLING<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u3001<\/span><span style=\"font-family: Times;\">GPIO.BOTH <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5bf9\u8fb9\u7f18\u8fdb\u884c\u68c0\u6d4b\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: \u5b8b\u4f53;\">\u7ebf\u7a0b\u56de\u8c03<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: Times;\">RPi.GPIO <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u7b2c\u4e8c\u6761\u7ebf\u7a0b\u4e2d\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u540c\u60a8\u7684\u4e3b\u7a0b\u5e8f\u540c\u65f6\u8fd0\u884c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7acb\u5373\u5bf9\u8fb9\u7f18\u8fdb\u884c\u54cd\u5e94\u3002\u4f8b\u5982\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">def my_callback(channel):<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"> print(&#8216;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u8fd9\u662f\u4e00\u4e2a\u8fb9\u7f18\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\uff01<\/span><span style=\"font-family: Consolas;\">&#8216;)<br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"> print(&#8216;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u901a\u9053<\/span><span style=\"font-family: Consolas;\"> {d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}s <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e0a\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b<\/span><span style=\"font-family: Consolas;\">&#8216;{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}channel)<br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"> print(&#8216;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u8be5\u7a0b\u5e8f\u4e0e\u60a8\u7684\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e2d<\/span><span style=\"font-family: Consolas;\">&#8216;)<br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\">GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback) # <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u901a\u9053\u4e0a\u6dfb\u52a0\u4e0a\u5347\u4e34\u754c\u503c\u68c0\u6d4b<\/span><span style=\"font-family: Consolas;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\">&#8230; <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5176\u5b83\u7a0b\u5e8f\u4ee3\u7801<\/span><span style=\"font-family: Consolas;\"> &#8230;<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u5982\u679c\u60a8\u9700\u8981\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">def my_callback_one(channel):<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"> print(&#8216;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u56de\u8c03<\/span><span style=\"font-family: Consolas;\"> 1&#8242;)<br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">def my_callback_two(channel):<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"> print(&#8216;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u56de\u8c03<\/span><span style=\"font-family: Consolas;\"> 2&#8242;)<br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.add_event_detect(channel, GPIO.RISING)<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.add_event_callback(channel, my_callback_one)<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.add_event_callback(channel, my_callback_two)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u6ce8\u610f\uff0c\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u4e3a\u987a\u5e8f\u8fd0\u884c\u800c\u4e0d\u662f\u540c\u65f6\u8fd0\u884c\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u524d\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b\u4f9b\u56de\u8c03\u4f7f\u7528\uff0c\u800c\u56de\u8c03\u7684\u8fd0\u884c\u987a\u5e8f\u662f\u4f9d\u636e\u5b83\u4eec\u88ab\u5b9a\u4e49\u7684\u987a\u5e8f\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: \u5b8b\u4f53;\">\u5f00\u5173\u9632\u6296<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u60a8\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u6bcf\u6b21\u6309\u94ae\u6309\u4e0b\u65f6\uff0c\u56de\u8c03\u64cd\u4f5c\u88ab\u8c03\u7528\u4e0d\u6b62\u4e00\u6b21\u3002\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4f5c<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5f00\u5173\u6296\u52a8\uff08<\/span><span style=\"font-family: Times;\">switch bounce<\/span><span style=\"font-family: \u5b8b\u4f53;\">\uff09<\/span><span style=\"font-family: Times;\">&#8220;<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u3002\u8fd9\u91cc\u6709\u4e24\u79cd\u65b9\u6cd5\u89e3\u51b3\u5f00\u5173\u6296\u52a8\u95ee\u9898\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u5c06\u4e00\u4e2a<\/span><span style=\"font-family: Times;\"> 0.1uF <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u7535\u5bb9\u8fde\u63a5\u5230\u5f00\u5173\u4e0a\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u8f6f\u4ef6\u9632\u6b62\u6296\u52a8<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4e24\u79cd\u65b9\u5f0f\u4e00\u8d77\u7528<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u4f7f\u7528\u8f6f\u4ef6\u65b9\u5f0f\u6296\u52a8\uff0c\u53ef\u4ee5\u5728\u60a8\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u4e2d\u6dfb\u52a0<\/span><span style=\"font-family: Times;\"> bouncetime= <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u53c2\u6570\u3002<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u6296\u52a8\u65f6\u95f4\u9700\u8981\u4f7f\u7528\u6beb\u79d2\u4e3a\u5355\u4f4d\u8fdb\u884c\u4e66\u5199\u3002\u4f8b\u5982\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-size: 10pt;\"><span style=\"font-family: Consolas;\"># <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u5728\u901a\u9053\u4e0a\u6dfb\u52a0\u4e0a\u5347\u4e34\u754c\u503c\u68c0\u6d4b\uff0c\u5ffd\u7565\u7531\u4e8e\u5f00\u5173\u6296\u52a8\u5f15\u8d77\u7684\u5c0f\u4e8e<\/span><span style=\"font-family: Consolas;\"> 200ms <\/span><span style=\"font-family: \u5b8b\u4f53;\">\u7684\u8fb9\u7f18\u64cd\u4f5c<\/span><span style=\"font-family: Consolas;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=200)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u6216\u8005<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.add_event_callback(channel, my_callback, bouncetime=200)<br \/>\n<\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">remove_event_detect()<br \/>\n<\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u60a8\u4e0d\u5e0c\u671b\u60a8\u7684\u7a0b\u5e8f\u68c0\u6d4b\u8fb9\u7f18\u4e8b\u4ef6\uff0c\u60a8\u53ef\u4ee5\u5c06\u5b83\u505c\u6b62\uff1a<\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><\/span><\/p>\n<p style=\"background: #f7f7f7;\"><span style=\"color: #333333; font-family: Consolas; font-size: 10pt;\">GPIO.remove_event_detect(channel)<br \/>\n<\/span><\/p>\n<p><span style=\"color: #323e4e; font-size: 13pt;\"><strong><span style=\"font-family: \u5b8b\u4f53;\">\u76f8\u5173\u7f51\u7ad9<\/span><span style=\"font-family: Arial;\"><br \/>\n<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u539f\u6587\u5730\u5740\uff1a<\/span><span style=\"color: #3390ca;\"><span style=\"font-family: Times;\">RPi.GPIO module Inputs<span style=\"color: #333a42;\"><br \/>\n<\/span><\/span><span style=\"font-family: \u5b8b\u4f53;\">\u9879\u76ee\u5730\u5740\uff1a<\/span><span style=\"font-family: Times;\">RPi.GPIO<span style=\"color: #333a42;\"><br \/>\n<\/span><\/span><span style=\"font-family: \u5b8b\u4f53;\">\u539f\u6587\u7ffb\u8bd1\uff1a<\/span><span style=\"font-family: Times;\">dreamcolor.net<span style=\"color: #333a42;\"><br \/>\n<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color: #333a42; font-size: 11pt;\"><span style=\"font-family: \u5b8b\u4f53;\">\u672c\u6587\u6765\u81ea\uff1a<a title=\"\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4\" href=\"http:\/\/shumeipai.nxez.com\/\"><span style=\"color: #3390ca;\">\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4<\/span><\/a><\/span><span style=\"font-family: Times;\"><br \/>\n<\/span><span style=\"font-family: \u5b8b\u4f53;\">\u94fe\u63a5\u5730\u5740\uff1a<\/span><span style=\"color: #3390ca; font-family: Times;\">http:\/\/shumeipai.nxez.com\/2016\/09\/28\/rpi-gpio-module-inputs.html<span style=\"color: #333a42;\"><br \/>\n<\/span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u65e5\u5fd7\u7684\u5185\u5bb9\u5e94\u8be5\u7b97\u662f\u300aRPi&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-reapberry"],"_links":{"self":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/306"}],"collection":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":0,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"wp:attachment":[{"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}