{"id":1174,"date":"2024-03-03T04:05:53","date_gmt":"2024-03-03T04:05:53","guid":{"rendered":"http:\/\/robinluo.top\/?p=1174"},"modified":"2024-03-03T16:32:35","modified_gmt":"2024-03-03T16:32:35","slug":"%e6%a0%91%e8%8e%93%e6%b4%bepwm%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=1174","title":{"rendered":"\u6811\u8393\u6d3e \u9999\u6a59\u6d3e PWM\u95ee\u9898"},"content":{"rendered":"\n<p>\u67e5\u770bGPIO\u5f15\u811a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gpio readall<\/code><\/pre>\n\n\n\n<p>\u7528python\u8c03\u7528PWM\u6709\u4e24\u79cd\u65b9\u6cd5<\/p>\n\n\n\n<p>1.RPi.GPIO<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import RPi.GPIO  as GPIO\n\n\nGPIO.setmode(GPIO.BCM)\n\nGPIO.setup(18,GPIO.OUT) #\u8bbe\u7f6e\u5f15\u811a\u4e3a\u8f93\u51fa\npwm = GPIO.PWM(18,100) #\u53ea\u670912\u811a \u548c 18\u811a \u652f\u6301\u786c\u4ef6PWM   100\u4e3a100hz\npwm.start(50) #\u5360\u7a7a\u6bd4\u4e3a50\u5e76\u8f93\u51fa\n\npwm.changeDutyCycle(20) #\u6539\u53d8\u5360\u7a7a\u6bd4\npwm.stop() #\u505c\u6b62PWM\u6ce2\u5f62\u8f93\u51fa\n<\/code><\/pre>\n\n\n\n<p>2OPI.GPIO<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PWM is created in the form of an object.\r\n\r\n1. First set up OPi.GPIO and create the object\r\n\r\n    .. code:: python\r\n\r\n       import OPi.GPIO as GPIO\r\n       PWM_Class = GPIO.PWM(PWM_chip, PWM_pin, frequency_Hz, Duty_Cycle_Percent)\n\r\n\r\u4e00\u822cPWM_chip \u4e3a0 \u7b2c\u4e00\u5757 \u53ef\u4ee5\u901a\u8fc7\n ls -l \/sys\/class\/pwm\/ \u67e5\u770b \u4e00\u822c\u8fd4\u56de pwmchip0\n\nPWM_pin \u5c31\u662f\u5f15\u811a\u4e86 \n\n\n    Note currently you do not need to specify setmode before creating the class as for a GPIO\r\n    only the PWM_chip number and the PWM_pin.\r\n    The reson for this and how to find what they are is explained in the sysfs PWM section.\r\n\r\n2. Begin the PWM cycle\r\n\r\n    .. code:: python\r\n\r\n        PWM_Class.start_pwm()\r\n\r\n3. Change PWM duty Cycle\r\n\r\n    .. code:: python\r\n        PWM_Class.duty_cycle(50)\r\n\r\n    Note this changes the Duty cycle to 50%\r\n\r\n4. Change the frequency\r\n\r\n    .. code:: python\r\n        PWM_Class.change_frequency(500)\r\n\r\n    Note this changes the Frequency to 500Hz\r\n\r\n5. Stop the PWM device\r\n\r\n    .. code:: python\r\n        PWM_Class.stop_pwm()\r\n\r\n    Note this stops the signal by setting the duty cycle to 0%\r\n\r\n6. Change the Polarity of the signal\r\n\r\n    .. code:: python\r\n        PWM_Class.pwm_polarity()\r\n\r\n    Note this changes swaps the on-off times. For example a duty cycle set to 75% before\r\n    this will result in the signal being on 75% and off 25% of the time. After this is\r\n    called it would be on 25% and off 75%.\r\n\r\n7. Remove PWM Object\r\n\r\n    .. code:: python\r\n        PWM_Class.pwm_close()<\/code><\/pre>\n\n\n\n<p>3.wiringpi<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import wiringpi\n\npwm0 =  wiringpi.GPIO(0)        \npwm0.pinMode(1,pwm0.PWM_OUTPUT)\npwm0.pwmSetMode(pwm0.PWM_MODE_MS)\npwm0.pwmSetClock(25)\npwm0.pwmWrite(1,self.brightness)\n\n\n\n\nimport wiringpi\n\nPWM_OUTPUT  = 2\nPWM_PIN = 1  #\u6307\u5b9a\u7b2c\u4e8c\u4e2aPWM\u63a5\u53e3\n\n# \u521d\u59cb\u5316\nwiringpi.wiringPiSetup()\nwiringpi.pinMode(PWM_PIN, PWM_OUTPUT)  #\u521d\u59cb\u5316\u7b2c\u4e8c\u4e2aPWM\u63a5\u53e3 18  \n\nwiringpi.pwmSetMode(wiringpi.PWM_MODE_MS)  \n#\u8bbe\u7f6ePWM\u5de5\u4f5c\u6a21\u5f0f \u6709\u4e24\u4e2a\u503cPWM_MODE_MS PWM_MODE_BAL\n#\u4e00\u822c\u9009\u7b2c\u4e00\u4e2a  PWM_MODE_BAL or PWM_MODE_MS(\u5e73\u8861\u6a21\u5f0f\/\u5360\u7a7a\u6bd4\u6a21\u5f0f)\n\nwiringpi.pwmSetRange(1024)  \n\n\n#\u8bbe\u7f6e\u603b\u5468\u671f\u9ed8\u8ba41024 \u6240\u4ee5\u4e00\u4e2a\u5468\u671f\u662f 600khz\/1024 \u7ea6\u7b49\u4e8e600hz\n\nwiringpi.pwmSetClock(32)  \n#\u8bbe\u7f6e\u5206\u9891 \u9ed8\u8ba432 \u5373600khz \u603b\u65f6\u949f\u9891\u7387\u662f19.2Mhz \n\nwiringpi.pwmWrite(PWM_PIN,0) #\u8bbe\u7f6e\u5360\u7a7a\u6bd4 0~1024  512 \u4e3a\u4e00\u534a  \n#0\u7684\u8bdd\u76f8\u5f53\u4e8e\u5199\u5165\u5168\u90e8\u4f4e\u7535\u5e73 \u6ca1\u6709\u6ce2\u5f62\u8f93\u51fa\n\nwhile 1:\n    for bright in range(0, 1000):\n        wiringpi.pwmWrite(PWM_PIN, bright)\n        wiringpi.delay(1)\n    for bright in range(1000, 0, -1):\n        wiringpi.pwmWrite(PWM_PIN, bright)\n        wiringpi.delay(1)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u770bGPIO\u5f15\u811a \u7528python\u8c03\u7528PWM\u6709\u4e24\u79cd\u65b9\u6cd5 1.RPi.GPIO 2OPI.GPIO 3.wiringpi<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[175,129],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1174"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1174"}],"version-history":[{"count":20,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1195,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1174\/revisions\/1195"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}