{"id":45,"date":"2020-05-10T16:49:23","date_gmt":"2020-05-10T08:49:23","guid":{"rendered":"https:\/\/jiangshibo.cn\/?p=45"},"modified":"2020-12-13T22:41:41","modified_gmt":"2020-12-13T14:41:41","slug":"%e6%a0%91%e8%8e%93%e6%b4%begpio%e5%92%8cpwm%e6%8e%a7%e5%88%b6%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/jiangshibo.cn\/index.php\/2020\/05\/10\/%e6%a0%91%e8%8e%93%e6%b4%begpio%e5%92%8cpwm%e6%8e%a7%e5%88%b6%e6%95%99%e7%a8%8b\/","title":{"rendered":"\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b"},"content":{"rendered":"<p><!--more--><\/p>\n<h1>1. \u76ee\u7684<\/h1>\n<p>\u5728\u6c7d\u8f66\u7535\u5b50\u5f00\u53d1\u4e2d\uff0cCAN\u901a\u8baf\u662f\u5f88\u96be\u907f\u5f00\u7684\u4e00\u4e2a\u6280\u672f\u70b9\uff0c\u5f53\u524dCAN\u5df2\u7ecf\u5f62\u6210\u4e86\u884c\u4e1a\u5185\u8f83\u4e3a\u7a33\u5b9a\u7684\u5f00\u53d1\u53ca\u76f8\u5173\u6d4b\u8bd5\u9a8c\u8bc1\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e00\u79cd\u8f83\u4e3a\u4e3b\u6d41\u7684\u5c31\u662f\u4f7f\u7528CANoe\u548c\u5f00\u53d1\u76f8\u5e94\u7684CAPL\u811a\u672c\u914d\u5408\u6d4b\u8bd5\u3002\u7136\u800c\uff0c\u8fd9\u5957\u65b9\u6848\u4e0b\u6765\u65e0\u8bba\u4eba\u529b\u8d22\u529b\u90fd\u662f\u4ef7\u683c\u4e0d\u83f2\uff0c\u800c\u4e14\u9762\u5bf9\u65e5\u5f02\u6708\u65b0\u7684\u6280\u672f\u53d1\u5c55\u4e5f\u663e\u5f97\u8f83\u4e3a\u4fdd\u5b88\u3002\u6240\u4ee5\u7b14\u8005\u5c1d\u8bd5\u4f7f\u7528\u6811\u8393\u6d3e\u914d\u5408MCP2515\u6a21\u5757\uff0c\u8fdb\u884c\u4f4e\u6210\u672c\u7684CAN\u7f51\u7edc\u8282\u70b9\u642d\u5efa\u548c\u62a5\u6587\u6a21\u62df\uff0c\u8bd5\u56fe\u62d3\u5bbd\u4e00\u4e0b\u89c6\u91ce\u548c\u601d\u8def\u3002<\/p>\n<p>\u672c\u6587\u9488\u5bf9\u666e\u901a\u5916\u8bbe\u548c\u901a\u8baf\u5916\u8bbe\uff0c\u5206\u522b\u4f7f\u7528\u4e86Python \u548c C\u8fdb\u884c\u7f16\u5199\u63a7\u5236\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u6848\u4f8b\u4ee5\u4fbf\u53c2\u8003\u3002<\/p>\n<h1>2. \u6750\u6599\u51c6\u5907<\/h1>\n<ul>\n<li>\u6811\u8393\u6d3e zero w<\/li>\n<li>MCP2515\u6a21\u5757<\/li>\n<li>2\u8def\u7ee7\u7535\u5668\u6a21\u5757<\/li>\n<li>linux \u4f7f\u7528\u57fa\u7840<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/9880502.jpg\" alt=\"first\" \/><\/p>\n<h1>3. \u521d\u59cb\u8bbe\u7f6e<\/h1>\n<p>\u6811\u8393\u6d3e\u5b89\u88c5\u6700\u65b0\u7684\u5b98\u65b9\u7a0b\u5e8f\uff0c\u5b89\u88c5\u597d\u4e4b\u540e\u5c06ssh\uff0cgpio\uff0cspi\u7b49\u8bbe\u7f6e\u7edf\u7edf\u8bbe\u4e3a\u5f00\u542f\u3002<\/p>\n<p>\u7136\u540e\u5c06\u6811\u8393\u6d3e\u8fde\u63a5\u5230\u5c40\u57df\u7f51wifi\uff0c\u8fdb\u884cssh\u767b\u5f55\u7ba1\u7406\uff08\u6b64\u5904\u4e5f\u53ef\u4f7f\u7528usb\u7f51\u5361\u6a21\u5f0f\u8fdb\u884cssh\u8fde\u63a5\uff0c\u597d\u5904\u662f\u514d\u9664\u8fde\u63a5\u5c4f\u5e55\u8fdb\u884c\u7cfb\u7edf\u521d\u59cb\u8bbe\u7f6e\uff09\u3002<\/p>\n<p>ssh\u8fde\u63a5\u6210\u529f\u540e\u5c06\u6811\u8393\u6d3e\u6362\u6210\u56fd\u5185\u7684\u6e90(\u7b14\u8005\u4f7f\u7528\u7684\u963f\u91cc\u7684\u6e90)\uff0c\u8fdb\u884c <code>sudo apt-get update<\/code> \u548c <code>sudo apt-get upgrade<\/code>\u66f4\u65b0\u3002<br \/>\n<!-- ![001](001.jpg) --><br \/>\n<!-- ![002](002.png) --><br \/>\n<img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/002.png\" alt=\"002\" \/><\/p>\n<p>\u6211\u4eec\u4f7f\u7528\u6811\u8393\u6d3e\uff0c\u9700\u8981\u8fdb\u884cGPIO\u8bbe\u7f6e\u548cspi\u901a\u8baf\uff0c\u63a7\u5236\u8fd9\u4e24\u90e8\u5206\u53ef\u4ee5\u4f7f\u7528\u5b98\u65b9\u63a8\u8350\u7684<a href=\"http:\/\/wiringpi.com\/\">WringPi<\/a>\u5e93\uff0c\u5728\u5b98\u65b9\u7cfb\u7edf\u4e2d\u662f\u9ed8\u8ba4\u5b89\u88c5\u7684\u3002\u7ecf\u8fc7\u5982\u4e0a\u64cd\u4f5c\u540e\u4f1a\u5c06\u5e93\u66f4\u65b0\u81f3\u6700\u65b0\uff0c\u4f7f\u7528<code>gipo -v<\/code>\u8fdb\u884c\u7248\u672c\u67e5\u770b\uff0c\u4f7f\u7528<code>gpio readall<\/code>\u8fdb\u884c\u6240\u6709\u5f15\u811a\u5b9a\u4e49\u67e5\u770b\u3002<br \/>\n<!-- ![003](003.png) --><br \/>\n<img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/003.png\" alt=\"003\" \/><br \/>\n<!-- ![004](004.png) --><\/p>\n<p>\u9664\u4e86WringPi\u5e93\u4e4b\u5916\uff0c\u6811\u8393\u6d3e\u8fd8\u80fd\u591f\u4f7f\u7528python\u8fdb\u884cGPIO\u7684\u63a7\u5236\u64cd\u4f5c\uff0c\u5bfc\u5165<a href=\"https:\/\/sourceforge.net\/p\/raspberry-gpio-python\/wiki\/Home\/\">RPi.GPIO<\/a>\u6a21\u5757\u5373\u53ef\u3002<\/p>\n<h1>4. GPIO \u63a7\u5236\u7ee7\u7535\u5668<\/h1>\n<p>\u7ee7\u7535\u5668\u6a21\u5757\u91c7\u7528[\u5e26\u5149\u8026\u4fdd\u62a4 2\u8def \u7ee7\u7535\u5668\u6a21\u5757]\uff0c\u6dd8\u5b9d\u53ef\u4ee5\u76f4\u63a5\u4e70\u5230\uff0c\u8be5\u7ee7\u7535\u5668\u6a21\u5757\u662f5v\u4f9b\u7535\uff0c\u9700\u8981\u4e24\u8def\u8f93\u51faIO\u8fdb\u884c\u4f4e\u6709\u6548\u7684\u63a7\u5236\uff0c\u6839\u636e\u786c\u4ef6\u7279\u6027\u9009\u53d6\u6811\u8393\u6d3e\u5982\u4e0b\u5f15\u811a\uff0c\u4e0e\u7ee7\u7535\u5668\u6a21\u5757\u8fdb\u884c\u8fde\u63a5\u3002<br \/>\n<img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/005.png\" alt=\"005\" \/><\/p>\n<h2>4.1. \u4f7f\u7528Python \u7684RPi.GPIO\u6a21\u5757\u8fdb\u884c\u63a7\u5236<\/h2>\n<p>\u9488\u5bf9\u7ee7\u7535\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528python\u7684RPi.GPIO\u6a21\u5757\u8fdb\u884c\u63a7\u5236\uff0c\u9ed8\u8ba4\u7cfb\u7edf\u4e2d\u4f1a\u5b89\u88c5\u6b64\u6a21\u5757\uff0c\u5982\u679c\u6ca1\u6709\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4<code>sudo pip3 install RPi.GPIO<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p>\u6839\u636e\u6a21\u5757\u7269\u7406\u7279\u6027\uff0c\u9700\u8981\u8bbe\u7f6e38\uff0c40\u4e24\u4e2a\u5f15\u811a\u4e3a\u4f4e\u7535\u5e73\u8f93\u51fa\uff0c\u8bbe\u7f6e37\u811a\u4e3aPWM\u6a21\u5f0f\uff0c\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<pre><code class=\"language-python \">import RPi.GPIO as GPIO\n\n# Use BCM chip number as handle\nGPIO.setmode(GPIO.BCM)\n\n# Set useful channel\nRELAY_1 = 20\nRELAY_2 = 21\nPWM_1   = 26\n\n# Collect chip channel as setting list\noutput_list = [RELAY_1,RELAY_2,PWM_1]\n\n# Set GPIO and PWM peripheral\nGPIO.setup(output_list, GPIO.OUT, initial=GPIO.HIGH)\ninit_freq = 50  # initial frequency in Hz\npwm_pin37 = GPIO.PWM(PWM_1, init_freq)\n\n# Set gpio as low output ,if high output then use GPIO.HIGH\nGPIO.output(RELAY_1,GPIO.LOW)\n\n# Start pwm ,initial duty cycle in 0.0 ,100.0\ninit_dc = 50  \npwm_pin37.start(init_dc)\n\n# Change frequency and duty cycle\npara_freq = 60\npara_duty = 20\npwm_pin37.ChangeFrequency(para_freq)\npwm_pin37.ChangeDutyCycle(para_duty)\n\n# Stop PWM out\npwm_pin37.stop()\n\n# Release system resource and close gpio control\nGPIO.cleanup()\n<\/code><\/pre>\n<p>\u5176\u4e2d PWM\u5f15\u811a\u8f93\u51fa\u7535\u5e73\u6700\u9ad8\u4e3a3.3V\uff0c\u8f93\u51fa\u6ce2\u5f62\u8f83\u4e3a\u5de5\u6574\uff0c\u4f46\u662f\u4e0e\u8bbe\u5b9a\u7684\u8bef\u5dee\u8f83\u5927\uff0c\u5f53\u8bbe\u5b9a\u8f7d\u9891\u4e3a1K\u65f6\uff0c\u5b9e\u9645\u6d4b\u8bd5\u4e3a850Hz\uff0c\u8bbe\u5b9a\u8f7d\u9891\u4e3a3K\uff0c\u5b9e\u9645\u8f93\u51fa2K\uff0c\u8bbe\u5b9a10K\uff0c\u5b9e\u9645\u8f93\u51fa3.6\uff2b\u3002\u6240\u4ee5\u5728\u4f7f\u7528PWM\u529f\u80fd\u65f6\u9700\u8981\u5b9e\u9645\u6d4b\u8bd5\u8f93\u51fa\u7684\u8f7d\u9891\uff0c\u5728\u8fdb\u884c\u8bbe\u5b9a\u3002<\/p>\n<p>\u5982\u4e0b\u4e3a\u8f7d\u9891\u8bbe\u5b9a\u4e3a10K\uff0c\u5b9e\u9645\u8f93\u51fa3.6K\u7684\u6ce2\u5f62\u3002<br \/>\n<img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/006.png\" alt=\"006\" \/><\/p>\n<h2>4.2. \u4f7f\u7528WringPi\u5e93\u51fd\u6570\u8fdb\u884c\u63a7\u5236<\/h2>\n<p>WringPi\u5e93\u4e3aC\u8bed\u8a00\uff0c\u9700\u8981\u8fdb\u884c\u7f16\u8bd1\u540e\u8fd0\u884c\uff0c\u4e0eRPi\u7684\u533a\u522b\u5c31\u662fWringPi\u652f\u6301spi\u548ciic\u901a\u8baf\u63a7\u5236\uff0c\u540e\u7eed\u8fdb\u884cCAN\u7f51\u7edc\u8282\u70b9\u6a21\u62df\u4e5f\u53ea\u80fd\u4f7f\u7528WringPi\u8fdb\u884c\u7f16\u5199\u3002\u5982\u4e0b\u4e3a\u57fa\u672c\u7684\u8bbe\u7f6e\u51fd\u6570\u793a\u4f8b\uff0c\u5177\u4f53\u9700\u8981\u67e5\u770b\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u76f8\u5e94\u7684\u51fd\u6570\u8c03\u7528\u53ca\u5f00\u53d1\u3002<\/p>\n<pre><code class=\"language-c \">\/*-----------------------------------------------------------------------------\n Function   : Used for testing RaspberryPi ZeroW's gpio pin -&gt; Relay \n FileName   : TestRelay.c\n Author     : Tomato\n Time       : 2019.2.4\n Version    : 0.1\n Notes      : None\n-----------------------------------------------------------------------------*\/\n#include &lt;wiringPi.h&gt;\n#include &lt;softPwm.h&gt;\n\n\/* Macro config define *\/\n#define RELAY_1   20U\n#define RELAY_2   21U\n#define PWM_1     26U\n\n\/* Parameter define *\/\nstatic int high_edge_time = 0;   \/\/ uint :0.1ms\nstatic int cycle_time     = 200; \/\/ uint :0.1ms\n\nint main (void)\n{\n    \/* Initial gpio with chip pin ,if use mapping pin -&gt; wiringPiSetup*\/\n    wiringPiSetupGpio () ;\n    \/* Set relay1&amp;2 pwm pin as output *\/\n    pinMode (RELAY_1, OUTPUT) ;  \/* INPUT\n                                    OUTPUT\n                                    PWM_OUTPUT\n                                    GPIO_CLOCK\n                                    SOFT_PWM_OUTPUT\n                                    SOFT_TONE_OUTPUT\n                                    PWM_TONE_OUTPUT *\/\n    pinMode (RELAY_2, OUTPUT) ;\n    pinMode (PWM_1, SOFT_PWM_OUTPUT) ;\n    softPwmCreate(PWM_1,high_edge_time,cycle_time);\n\n    \/* Main loop function *\/\n    for (;;)\n    {\n        \/* Control relay *\/\n        digitalWrite (RELAY_1, HIGH) ; \n        delay (500) ;\n        digitalWrite (RELAY_1,  LOW) ; \n        delay (500) ;\n\n        \/* Control pwm *\/\n        softPwmWrite(PWM_1, 20); \/\/ duty = 20\/cycle_time\n        delay (5000);\n        softPwmWrite(PWM_1, 50);\n        delay (5000);\n        softPwmWrite(PWM_1, 90);\n        delay (5000);\n    }\n    return 0 ;\n}\n<\/code><\/pre>\n<p>\u7f16\u5199\u597d\u5982\u4e0a\u51fd\u6570\u540e\uff0c\u8fdb\u884c\u7f16\u8bd1\u548c\u94fe\u63a5\uff0c\u94fe\u63a5\u7684\u65f6\u5019\u9700\u8981\u4f7f\u7528 -lwringPi \u8fd9\u4e2a\u6307\u4ee4\uff0c\u8fdb\u884c\u76f8\u5e94\u5e93\u51fd\u6570\u7684\u94fe\u63a5\u3002\u5982\u9700\u8981\u7f16\u8bd1PWM\u5f15\u811a\u63a7\u5236\u90e8\u5206\uff0c\u5219\u9700\u8981\u589e\u52a0 -lpthread \u94fe\u63a5\u6307\u4ee4\u3002<\/p>\n<p><code>gcc -o TestRelay TestRelay.c -lwiringPi -lpthread<\/code><\/p>\n<p>\u7f16\u8bd1\u6210\u529f\u540e\uff0c\u4f7f\u7528<code>sudo .\/TestRelay<\/code>\u547d\u4ee4\u76f4\u63a5\u8fd0\u884c\u6211\u4eec\u7f16\u8bd1\u6307\u5b9a\u7684\u7a0b\u5e8f\u6807\u8bc6\u540d<code>TestRelay<\/code>\uff0c\u5b9e\u9645\u53ef\u4ee5\u770b\u5230\u7ee7\u7535\u5668\u7684\u5207\u5408\uff0c\u4ee5\u53caPWM\u8f93\u51fa\u3002<br \/>\n<img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/008.png\" alt=\"008\" \/><\/p>\n<p>\u5982\u7a0b\u5e8f\u4e2d\u8bbe\u5b9a\uff0c\u6d4b\u8bd5\u5b9e\u9645\u8f93\u51fa\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4f7f\u7528PWM\u63a7\u5236\u6709\u5982\u4e0b\u9650\u5236\uff0c<br \/>\n- PWM\u8bbe\u5b9a\u7684\u7a0b\u5e8f\u8dd1\u572810ms\u7684\u7ebf\u7a0b\u4efb\u52a1\u4e0b\uff0c\u6240\u4ee5\u6bcfs\u6700\u591a\u66f4\u6539100\u6b21\uff0c<br \/>\n- \u6bcf\u64cd\u63a7\u4e00\u4e2aGPIO\u5f15\u811a\uff0cCPU\u8d1f\u8f7d\u7387\u4f1a\u4e0a\u5347 0.5%\u5de6\u53f3\uff0c\u8981\u6839\u636e\u5f53\u524d\u6811\u8393\u6d3e\u7684\u8d1f\u8f7d\u7387\u8fdb\u884c\u5408\u7406\u8bbe\u7f6e\uff0c\u4ee5\u4fdd\u8bc1\u6709\u8db3\u591f\u7684\u8d44\u6e90\u8fdb\u884c\u5916\u90e8\u901a\u8baf\u8fde\u63a5\u3002<br \/>\n<img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b\/009.png\" alt=\"009\" \/><\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f PWM\u63a7\u5236\u4e2d\uff0c\u4f7f\u7528RPi \u548cWringPi\u6240\u8f93\u5165\u7684\u53c2\u6570\u7684\u89d2\u5ea6\u4e0d\u540c\uff0c\u5982\u9700\u8981\u5207\u6362\u4f7f\u7528\u5219\u8981\u53c2\u8003\u5982\u4e0a\u4e24\u4e2a\u6e90\u7801\u793a\u4f8b\uff0c\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<h1>5. \u5c0f\u7ed3<\/h1>\n<p>\u81f3\u6b64\u5df2\u7ecf\u80fd\u591f\u6b63\u5e38\u64cd\u7eb5\u6811\u8393\u6d3e\u7684GPIO\u548cPWM\u63a5\u53e3\u4e86\uff0c\u540e\u7eed\u6211\u4f1a\u8bb2CAN\u901a\u8baf\u7684\u8bbe\u8ba1\u548c\u4f7f\u7528\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/45"}],"collection":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":2,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions\/86"}],"wp:attachment":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}