{"id":39287,"date":"2023-09-29T09:00:57","date_gmt":"2023-09-29T01:00:57","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=39287"},"modified":"2023-09-29T09:00:57","modified_gmt":"2023-09-29T01:00:57","slug":"sygajkzlsdsybg","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/39287.html","title":{"rendered":"\u4f7f\u7528GPIO\u6309\u952e\u63a7\u5236\u6d41\u6c34\u706f\u5b9e\u9a8c\u62a5\u544a"},"content":{"rendered":"
\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528GPIO\u6309\u952e\u63a7\u5236\u6d41\u6c34\u706f\uff0c\u4ee5\u53ca\u5982\u4f55\u5b9e\u73b0\u6d41\u6c34\u706f\u529f\u80fd\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u9a8c\u62a5\u544a\uff0c\u4ece\u5b9e\u9a8c\u51c6\u5907\u5230\u5b9e\u9a8c\u5b8c\u6210\uff0c\u4ee5\u53ca\u6700\u7ec8\u7684\u5b9e\u9a8c\u7ed3\u8bba\u3002<\/p>\n
\u5728\u5f00\u59cb\u5b9e\u9a8c\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e00\u4e9b\u5fc5\u8981\u7684\u5de5\u5177\u548c\u6750\u6599\uff0c\u5305\u62ec\uff1a<\/p>\n
\u5b9e\u9a8c\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6240\u6709\u7684\u6750\u6599\u8fde\u63a5\u5230Raspberry Pi\u677f\u4e0a\uff0c\u4ee5\u5b8c\u6210\u5b9e\u9a8c\u51c6\u5907\u5de5\u4f5c\u3002<\/p>\n
\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u7528\u4e8e\u63a7\u5236\u6d41\u6c34\u706f\u3002\u7a0b\u5e8f\u4e2d\u9700\u8981\u5b9a\u4e49\u4e00\u4e2aGPIO\u6309\u952e\uff0c\u7528\u4e8e\u89e6\u53d1\u6d41\u6c34\u706f\u7684\u5f00\u542f\u548c\u5173\u95ed\u3002\u7a0b\u5e8f\u7684\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n
# \u5bfc\u5165GPIO\u5e93 import RPi.GPIO as GPIO<\/p>
# \u8bbe\u7f6eGPIO\u6a21\u5f0f<\/p>
GPIO.setmode(GPIO.BCM)<\/p>
# \u5b9a\u4e49GPIO\u6309\u952e<\/p>
GPIO_BUTTON = 18<\/p>
# \u8bbe\u7f6eGPIO\u6309\u952e\u4e3a\u8f93\u5165\u6a21\u5f0f<\/p>
GPIO.setup(GPIO_BUTTON, GPIO.IN)<\/p>
# \u5b9a\u4e49\u6d41\u6c34\u706fGPIO\u7aef\u53e3<\/p>
GPIO_LED = 17<\/p>
# \u8bbe\u7f6e\u6d41\u6c34\u706fGPIO\u7aef\u53e3\u4e3a\u8f93\u51fa\u6a21\u5f0f<\/p>
GPIO.setup(GPIO_LED, GPIO.OUT)<\/p>
try:<\/p>
while True:<\/p>
# \u8bfb\u53d6\u6309\u952e\u8f93\u5165<\/p>
button_state = GPIO.input(GPIO_BUTTON)<\/p>
if button_state == False:<\/p>
# \u6309\u952e\u6309\u4e0b\uff0c\u5f00\u542f\u6d41\u6c34\u706f<\/p>
GPIO.output(GPIO_LED, True)<\/p>
else:<\/p>
# \u6309\u952e\u672a\u6309\u4e0b\uff0c\u5173\u95ed\u6d41\u6c34\u706f<\/p>
GPIO.output(GPIO_LED, False)<\/p>
finally:<\/p>
# \u6e05\u7406GPIO<\/p>
GPIO.cleanup()<\/p><\/code><\/pre>\n
\u7b2c\u4e8c\u6b65\uff1a\u8fd0\u884c\u7a0b\u5e8f<\/h3>\n
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u7a0b\u5e8f\u590d\u5236\u5230Raspberry Pi\u4e2d\uff0c\u5e76\u8fd0\u884c\u5b83\u3002\u5728\u8fd0\u884c\u7a0b\u5e8f\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u770b\u5230\u6d41\u6c34\u706f\u5f00\u59cb\u95ea\u70c1\uff0c\u800c\u6309\u952e\u6309\u4e0b\u65f6\uff0c\u6d41\u6c34\u706f\u5c06\u88ab\u6fc0\u6d3b\uff0c\u53cd\u4e4b\u5219\u5173\u95ed\u3002<\/p>\n
\u5b9e\u9a8c\u7ed3\u8bba<\/h2>\n
\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9a8c\u6559\u7a0b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528GPIO\u6309\u952e\u63a7\u5236\u6d41\u6c34\u706f\uff0c\u4ee5\u53ca\u5982\u4f55\u7f16\u5199\u7a0b\u5e8f\u5b9e\u73b0\u5176\u529f\u80fd\u3002<\/strong>\u901a\u8fc7\u672c\u5b9e\u9a8c\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u7406\u89e3GPIO\u6309\u952e\u63a7\u5236\u6d41\u6c34\u706f\u7684\u539f\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u8fd0\u7528\u7f16\u7a0b\u6280\u672f\u5b9e\u73b0\u5176\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528GPIO\u6309\u952e\u63a7\u5236\u6d41\u6c34\u706f\uff0c\u4ee5\u53ca\u5982\u4f55\u5b9e\u73b0\u6d41\u6c34\u706f\u529f\u80fd\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u9a8c\u62a5\u544a\uff0c\u4ece\u5b9e\u9a8c\u51c6\u5907\u5230\u5b9e\u9a8c\u5b8c\u6210\uff0c\u4ee5\u53ca\u6700\u7ec8\u7684\u5b9e\u9a8c\u7ed3\u8bba\u3002\u5b9e\u9a8c\u51c6\u5907<\/p>\n","protected":false},"author":1,"featured_media":4132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[222],"class_list":["post-39287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-ycio"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/39287"}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=39287"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/39287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/4132"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=39287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=39287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=39287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}