﻿{"id":2135,"date":"2021-11-11T13:41:43","date_gmt":"2021-11-11T05:41:43","guid":{"rendered":"http:\/\/nick.txtcc.com\/?p=2135"},"modified":"2021-11-17T11:27:01","modified_gmt":"2021-11-17T03:27:01","slug":"%e6%a0%91%e8%8e%93%e6%b4%be%e6%8e%a7%e5%88%b616%e8%b7%afpwm%e8%be%93%e5%87%ba%e7%9a%84pca9685%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/nick.txtcc.com\/index.php\/linux\/2135","title":{"rendered":"\u6811\u8393\u6d3e\u63a7\u523616\u8defPWM\u8f93\u51fa\u7684PCA9685\u6a21\u5757"},"content":{"rendered":"<p>\u6811\u8393\u6d3e\u63a7\u523616\u8defPWM\u8f93\u51fa\u7684PCA9685\u6a21\u5757\n1.PCA9685\n1.1\u7b80\u4ecb\n\u91c7\u7528I2C\u901a\u4fe1\uff0c\u5185\u7f6e\u4e86PWM\u9a71\u52a8\u5668\u548c\u4e00\u4e2a\u65f6\u949f\uff0c\u4e0d\u9700\u8981\u4e0d\u65ad\u53d1\u9001\u4fe1\u53f7\u5360\u7528\u5355\u7247\u673a\u8d44\u6e90\n\u652f\u630116\u8defPWM\u8f93\u51fa\uff0c\u6bcf\u8def12\u4f4d\u5206\u8fa8\u7387(4096\u7ea7)\uff0c\u572860Hz\u7684\u66f4\u65b0\u7387\u80fd\u591f\u8fbe\u52304us\u5206\u8fa8\u7387\n\u5185\u7f6e25MHz\u6676\u632f\uff0c\u53ef\u4e0d\u8fde\u63a5\u5916\u90e8\u6676\u632f\uff0c\u4e5f\u53ef\u4ee5\u8fde\u63a5\u5916\u90e8\u6676\u632f\uff0c\u6700\u592750MHz\n\u5206\u4e3a\u63a7\u5236\u7535\u548c\u9a71\u52a8\u7535\u4e24\u4e2a\u7535\u6e90\uff0c\u63a7\u5236\u7535\u652f\u63012.3V-5.5V\u7535\u538b\uff0c\u6700\u5927\u8010\u538b\u503c5.5V\uff0c\u903b\u8f91\u7535\u5e733.3V\uff0c\u9a71\u52a8\u7535\u652f\u63015V-10V\u76f4\u6d41\u7535\u538b\u3002\n1.2\u5f15\u811a\n\u5f15\u811a \u529f\u80fd\nGND \u63a5\u5730\nOE \u7a7a\u5f15\u811a\nSCL I2C\u603b\u7ebf\u63a7\u5236\u7ebf\nSDA I2C\u603b\u7ebf\u6570\u636e\u7ebf\nVCC \u63a7\u5236\u7535\u538b\uff0c2.3V-5.5V\nV+ \u9a71\u52a8\u7535\u538b\uff0c5V-10V\nV+\u662f\u7ed9\u5916\u63a5\u8bbe\u5907\u4f9b\u7535\u7684\u5f15\u811a\uff0c\u5728\u9a71\u52a8\u5927\u529f\u7387\u8235\u673a\u65f6\u5019\u901a\u5e38\u4e0d\u4f7f\u7528V+\uff0c\u800c\u662f\u4f7f\u7528\u5916\u63a5\u7535\u6e90\u7684\u4e24\u4e2a\u7aef\u53e3\uff0c\u5373\u4e0b\u56fe\u6211\u6807\u51fa\u6765\u7684VCC,GND\u3002<\/p>\n<p>V+\u7684\u7535\u538b\u662f\u591a\u5c11\uff0c\u8235\u673a\u7684\u7535\u538b\u5c31\u662f\u591a\u5c11\u3002\u6211\u4f7f\u7528\u7684\u662fSG90\uff0c\u5de5\u4f5c\u7535\u538b\u4e3a4.8V-6V\uff0cV+\u5c31\u5e94\u8be5\u57284.8V-6V\u3002<\/p>\n<p>PCA9685\u8fde\u63a5\u6811\u8393\u6d3e\u548c\u8235\u673a<\/p>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2152\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112340765.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"1181\" height=\"785\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112340765.png 1181w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112340765-300x199.png 300w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112340765-1024x681.png 1024w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112340765-768x510.png 768w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112340765-624x415.png 624w\" sizes=\"auto, (max-width: 1181px) 100vw, 1181px\" \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2187\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20190502115235780.png\" width=\"696\" height=\"569\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20190502115235780.png 696w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20190502115235780-300x245.png 300w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20190502115235780-624x510.png 624w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/>\n<p>2.\u6811\u8393\u6d3e\u90e8\u5206\n\u70e7\u5f55\u597d\u7cfb\u7edf\uff0c\u5f00\u673a\u3002\n\u6211\u4f7f\u7528\u7684\u662fubuntu mate\u3002\n\u66f4\u65b0\u4e00\u4e0b\u8f6f\u4ef6\u5305\u3002<\/p>\n<p>sudo apt-get update\nsudo apt-get dist-upgrade<\/p>\n<p>\u68c0\u67e5\u4e00\u4e0bpip\u7248\u672c<\/p>\n<p>pip -V<\/p>\n<p>\u6211\u66f4\u65b0\u8fc7\u4e86\uff0c\u6240\u4ee5\u662f20.0.2\u3002\n\u5982\u679c\u7248\u672c\u4f4e\u4e86\uff0c\u66f4\u65b0\u4e00\u4e0b\u3002<\/p>\n<p>sudo python -m pip install &#8211;upgrade pip<\/p>\n<p>2.1\u6253\u5f00IIC<\/p>\n<p>ubuntu\u7cfb\u7edf\u9700\u8981\u624b\u52a8\u5b89\u88c5\uff1a<a href=\"http:\/\/archive.raspberrypi.org\/debian\/pool\/main\/r\/raspi-config\/raspi-config_20211111_all.deb\">http:\/\/archive.raspberrypi.org\/debian\/pool\/main\/r\/raspi-config\/raspi-config_20211111_all.deb<\/a><\/p>\n<p><span class=\"hljs-attr\">\u4f9d\u8d56\u5305\uff1asudo<\/span> <span class=\"hljs-string\">apt install lua5.1 libatopology2 libfftw3-single3 libsamplerate0 alsa-utils<\/span><\/p>\n<p><span class=\"hljs-attr\">sudo<\/span> <span class=\"hljs-string\">dpkg -i raspi-config_20200601_all.deb<\/span><\/p>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2153\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/2020041711264633.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"550\" height=\"133\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/2020041711264633.png 550w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/2020041711264633-300x73.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2154\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112702121.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"564\" height=\"154\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112702121.png 564w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112702121-300x82.png 300w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2155\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112712992.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"675\" height=\"425\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112712992.png 675w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112712992-300x189.png 300w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112712992-624x393.png 624w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2156\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112725169.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"675\" height=\"425\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112725169.png 675w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112725169-300x189.png 300w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112725169-624x393.png 624w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/>\n<p>sudo raspi-config -> Interfacing Options -> I2C->enable<\/p>\n<p>sudo raspi-config<\/p>\n<h3>Utilities<\/h3>\n<p>There is a set of command-line utility programs that can help get an I2C interface working. You can get them with the apt package manager.<\/p>\n<pre class=\"rainbow-show\" data-trimmed=\"true\"><code class=\"rainbow rainbow-show\" data-language=\"bash\">sudo apt-get install -y i2c-tools\n<\/code><\/pre>\n<div class=\"preloader\">\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<p>In particular, the\u00a0<code>i2cdetect<\/code>\u00a0program will probe all the addresses on a bus, and report whether any devices are present. Enter the following command in the command line. The\u00a0<code>-y<\/code>\u00a0flag will disable interactive mode so that you do not have to wait for confirmation. The\u00a0<code>1<\/code>\u00a0indicates that we are scanning for I<sup>2<\/sup>C devices on I<sup>2<\/sup>C bus 1 (e.g. i2c-1).<\/p>\n<pre class=\"rainbow-show\" data-trimmed=\"true\"><code class=\"rainbow rainbow-show\" data-language=\"bash\">i2cdetect -y 1\n<\/code><\/pre>\n<div class=\"preloader\">\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<p>You will get an output from your Raspberry Pi similar to the output below.<\/p>\n<pre class=\"rainbow-show\" data-trimmed=\"true\"><code class=\"rainbow rainbow-show\" data-language=\"bash\">pi@raspberrypi:~\/$ i2cdetect -y 1\n     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n00:          -- -- -- -- -- -- -- -- -- -- -- -- --\n10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n70: -- -- -- -- -- -- -- --\n<\/code><\/pre>\n<div class=\"preloader\">\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<p>This map indicates that there is a peripheral at address\u00a0<strong>0x60<\/strong>. We can try to read and write its registers using the\u00a0<code>i2cget<\/code>,\u00a0<code>i2cset<\/code>\u00a0and\u00a0<code>i2cdump<\/code>\u00a0commands.<\/p>\n<p><strong>A few comments:<\/strong><\/p>\n<p>The instructions provided by\u00a0<strong>anvoice<\/strong>\u00a0in\u00a0<a href=\"https:\/\/askubuntu.com\/questions\/1130052\/enable-i2c-on-raspberry-pi-ubuntu\">this question<\/a>\u00a0were essential, but the actual dependencies are a few years out of date. So I suggest you try step #1 and read the dpkg output to tailor dependencies to your version of raspi-config.<\/p>\n<p>It appears that raspi-config may not be necessary, but you might only need to add the\u00a0<code>dtparam=i2c_arm=on<\/code>\u00a0flag to \/boot\/config.txt (see\u00a0<a href=\"https:\/\/www.raspberrypi.org\/documentation\/configuration\/config-txt\/README.md\" rel=\"nofollow noreferrer\">the official docs<\/a>). I haven&#8217;t tried (yet). If you want to try,\u00a0<code>sudo nano \/boot\/config.txt<\/code>, add the flag on a new line, CTRL-X, confirm with Y, then start at step #6.<\/p>\n<p>&nbsp;<\/p>\n<p>2.2\u5b89\u88c5adafruit\n\u65b9\u6cd51\nsudo pip3 install adafruit-pca9685<\/p>\n<p>\u65b9\u6cd52\nsudo apt-get install git build-essential python-dev\ncd ~\ngit clone <a href=\"https:\/\/github.com\/adafruit\/Adafruit_Python_PCA9685.git\">https:\/\/github.com\/adafruit\/Adafruit_Python_PCA9685.git<\/a>\ncd Adafruit_Python_PCA9685\nsudo python3 setup.py install<\/p>\n<p>\u65b9\u6cd52\u5b89\u88c5\u7684\u5728Adafruit_Python_PCA9685-master\/examples\u4e0b\u6709\u4e2a\u4f8b\u7a0bsimpletest.py\n\u65b9\u6cd5\u4e00\u6211\u6ca1\u627e\u5230\u8fd9\u4e2a\u6587\u4ef6\uff0c\u627e\u4e0d\u5230\u7684\u53ef\u4ee5\u53bbgithub\u91cc\u627e\u3002\u70b9\u8fd9\u91cc\u00a0<a href=\"https:\/\/github.com\/adafruit\/Adafruit_Python_PCA9685\/tree\/master\/examples\">https:\/\/github.com\/adafruit\/Adafruit_Python_PCA9685\/tree\/master\/examples<\/a><\/p>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2157\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112925166.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"1295\" height=\"159\" srcset=\"https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112925166.png 1295w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112925166-300x37.png 300w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112925166-1024x126.png 1024w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112925166-768x94.png 768w, https:\/\/nick.txtcc.com\/wp-content\/uploads\/2021\/11\/20200417112925166-624x77.png 624w\" sizes=\"auto, (max-width: 1295px) 100vw, 1295px\" \/>\n<p>\u590d\u5236\u6216\u4e0b\u8f7d\uff0c\u7136\u540e\u8fd0\u884c<\/p>\n<p>python3 simpletest.py\n\u53ef\u4ee5\u770b\u89c1\u8235\u673a\u8f6c\u52a8<\/p>\n<p>\u6211\u6539\u8fc7\u7684\u4ee3\u7801<\/p>\n<h1>from <strong>future<\/strong> import division # python2\u4f7f\u7528<\/h1>\n<p>import time\nimport Adafruit_PCA9685 # \u8c03\u7528PCA9685\u6a21\u5757<\/p>\n<p>pwm = Adafruit_PCA9685.PCA9685()<\/p>\n<h1>\u8bbe\u7f6e\u6700\u5927\u6700\u5c0f\u8109\u51b2\u957f\u5ea6<\/h1>\n<p>servo_min = 90 # 4096\u7684\u6700\u5c0f\u8109\u51b2\u957f\u5ea6\nservo_max = 640 # 4096\u7684\u6700\u5927\u8109\u51b2\u957f\u5ea6\nservo_mid = 365 # 4096\u7684\u4e2d\u95f4\u8109\u51b2\u957f\u5ea6<\/p>\n<h1>\u8bbe\u7f6e\u9891\u7387\u4e3a60<\/h1>\n<p>pwm.set_pwm_freq(60)\nprint(&#8216;Moving servo on, press Ctrl-C to quit&#8230;&#8217;)\nwhile True:\npwm.set_pwm(0, 0, servo_min)\ntime.sleep(1)\npwm.set_pwm(0, 0, servo_max)\ntime.sleep(1)\n\u8fd0\u884c\u5c31\u53ef\u4ee5\u770b\u89c1\u63a5\u5728\u901a\u90530\u4e0a\u7684\u8235\u673a\u53cd\u590d\u8fd0\u52a8<\/p>","protected":false},"excerpt":{"rendered":"<p>\u6811\u8393\u6d3e\u63a7\u523616\u8defPWM\u8f93\u51fa\u7684PCA9685\u6a21\u5757 1.PCA9685 1.1\u7b80\u4ecb \u91c7\u7528I2C\u901a\u4fe1\uff0c\u5185\u7f6e\u4e86PWM\u9a71\u52a8\u5668\u548c\u4e00\u4e2a\u65f6\u949f\uff0c\u4e0d\u9700\u8981\u4e0d\u65ad\u53d1\u9001\u4fe1\u53f7\u5360\u7528\u5355\u7247\u673a\u8d44\u6e90 \u652f\u630116\u8defPWM\u8f93\u51fa\uff0c\u6bcf\u8def12\u4f4d\u5206\u8fa8\u7387(&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":2152,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,274,39],"tags":[509,510],"class_list":["post-2135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-python","category-scm","tag-pca9685","tag-510"],"_links":{"self":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/2135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=2135"}],"version-history":[{"count":6,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/2135\/revisions"}],"predecessor-version":[{"id":2188,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/2135\/revisions\/2188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media\/2152"}],"wp:attachment":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=2135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=2135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=2135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}