{"id":1073,"date":"2016-01-06T11:31:35","date_gmt":"2016-01-06T03:31:35","guid":{"rendered":"http:\/\/www.kumouse.com\/?p=1073"},"modified":"2016-01-06T11:31:35","modified_gmt":"2016-01-06T03:31:35","slug":"arduino-%e6%9b%b4%e6%94%b9pwm%e9%a2%91%e7%8e%87%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=1073","title":{"rendered":"arduino \u66f4\u6539pwm\u9891\u7387\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>\u6211\u4eec\u7684PWM\u53e3\u53ea\u80fd\u901a\u8fc7\u8f93\u51fa\u5360\u7a7a\u6bd4\u6765\u8f93\u51fa\u6a21\u62df\u91cf\uff0c\u4f46\u662f\u5982\u679c\u8981\u8f93\u51fa1-60KHz\u6216\u66f4\u9ad8\u7684\u8109\u51b2\u9891\u7387\u7684\u8bdd\uff0c\u5c31\u4e0d\u597d\u505a\u5230\u5566\u3002\u6211\u4e00\u76f4\u5728\u9700\u627e\u89e3\u51b3\u65b9\u6848\uff0c\u65e0\u610f\u4e2d\u627e\u5230\u5566\uff0c\u73b0\u63d0\u4f9b\u7ed9\u5927\u5bb6\u5206\u4eab\uff0c\u5e0c\u671b\u80fd\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u6e90\u4ee3\u7801\u5982\u4e0b\uff1aHere are some usage examples of the function:<\/p>\n<p><i><span style=\"color: #666666;\">\/\/ Set pin 9&#8217;s PWM frequency to 3906 Hz (31250\/8 = 3906)<br \/>\n\/\/ Note that the base frequency for pins 3, 9, 10, and 11 is 31250 Hz<br \/>\n<\/span><\/i>setPwmFrequency<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000dd;\">9<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/p>\n<p><\/span><i><span style=\"color: #666666;\">\/\/ Set pin 6&#8217;s PWM frequency to 62500 Hz (62500\/1 = 62500)<br \/>\n\/\/ Note that the base frequency for pins 5 and 6 is 62500 Hz<br \/>\n<\/span><\/i>setPwmFrequency<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000dd;\">6<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/p>\n<p><\/span><i><span style=\"color: #666666;\">\/\/ Set pin 10&#8217;s PWM frequency to 31 Hz (31250\/1024 = 31)<br \/>\n<\/span><\/i>setPwmFrequency<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000dd;\">10<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">1024<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span><\/p>\n<p>Please keep in mind that changing the PWM frequency changes the Atmega&#8217;s timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).<\/p>\n<p><i><span style=\"color: #808080;\">\/**<br \/>\n* Divides a given PWM pin frequency by a divisor.<br \/>\n*<br \/>\n* The resulting frequency is equal to the base frequency divided by<br \/>\n* the given divisor:<br \/>\n*\u00a0 \u00a0&#8211; Base frequencies:<br \/>\n*\u00a0 \u00a0\u00a0 \u00a0o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.<br \/>\n*\u00a0 \u00a0\u00a0 \u00a0o The base frequency for pins 5 and 6 is 62500 Hz.<br \/>\n*\u00a0 \u00a0&#8211; Divisors:<br \/>\n*\u00a0 \u00a0\u00a0 \u00a0o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,<br \/>\n*\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0256, and 1024.<br \/>\n*\u00a0 \u00a0\u00a0 \u00a0o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,<br \/>\n*\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0128, 256, and 1024.<br \/>\n*<br \/>\n* PWM frequencies are tied together in pairs of pins. If one in a<br \/>\n* pair is changed, the other is also changed to match:<br \/>\n*\u00a0 \u00a0&#8211; Pins 5 and 6 are paired on timer0<br \/>\n*\u00a0 \u00a0&#8211; Pins 9 and 10 are paired on timer1<br \/>\n*\u00a0 \u00a0&#8211; Pins 3 and 11 are paired on timer2<br \/>\n*<br \/>\n* Note that this function will have side effects on anything else<br \/>\n* that uses timers:<br \/>\n*\u00a0 \u00a0&#8211; Changes on pins 3, 5, 6, or 11 may cause the delay() and<br \/>\n*\u00a0 \u00a0\u00a0\u00a0millis() functions to stop working. Other timing-related<br \/>\n*\u00a0 \u00a0\u00a0\u00a0functions may also be affected.<br \/>\n*\u00a0 \u00a0&#8211; Changes on pins 9 or 10 will cause the Servo library to function<br \/>\n*\u00a0 \u00a0\u00a0\u00a0incorrectly.<br \/>\n*<br \/>\n* Thanks to macegr of the Arduino forums for his documentation of the<br \/>\n* PWM frequency divisors. His post can be viewed at:<br \/>\n*\u00a0 \u00a0<a href=\"http:\/\/www.arduino.cc\/cgi-bin\/yabb2\/YaBB.pl?num=1235060559\/0#4\" target=\"_blank\">http:\/\/www.arduino.cc\/cgi-bin\/yabb2\/YaBB.pl?num=1235060559\/0#4<\/a><br \/>\n*\/<br \/>\n<\/span><\/i><span style=\"color: #993333;\">void<\/span> setPwmFrequency<span style=\"color: #009900;\">(<\/span><span style=\"color: #993333;\">int<\/span> pin<span style=\"color: #339933;\">,<\/span> <span style=\"color: #993333;\">int<\/span> divisor<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0\u00a0byte mode<span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0\u00a0<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span>pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">5<\/span> <span style=\"color: #339933;\">||<\/span> pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">6<\/span> <span style=\"color: #339933;\">||<\/span> pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">9<\/span> <span style=\"color: #339933;\">||<\/span> pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">10<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #b1b100;\">switch<\/span><span style=\"color: #009900;\">(<\/span>divisor<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x01<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x02<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">64<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x03<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">256<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x04<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">1024<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x05<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">default<\/span><span style=\"color: #339933;\">:<\/span> <span style=\"color: #b1b100;\">return<\/span><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #009900;\">}<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span>pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">5<\/span> <span style=\"color: #339933;\">||<\/span> pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">6<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0TCCR0B <span style=\"color: #339933;\">=<\/span> TCCR0B <span style=\"color: #339933;\">&amp;<\/span> <span style=\"color: #208080;\">0b11111000<\/span> <span style=\"color: #339933;\">|<\/span> mode<span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #009900;\">}<\/span> <span style=\"color: #b1b100;\">else<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0TCCR1B <span style=\"color: #339933;\">=<\/span> TCCR1B <span style=\"color: #339933;\">&amp;<\/span> <span style=\"color: #208080;\">0b11111000<\/span> <span style=\"color: #339933;\">|<\/span> mode<span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #009900;\">}<br \/>\n<\/span>\u00a0\u00a0<span style=\"color: #009900;\">}<\/span> <span style=\"color: #b1b100;\">else<\/span> <span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span>pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">3<\/span> <span style=\"color: #339933;\">||<\/span> pin <span style=\"color: #339933;\">==<\/span> <span style=\"color: #0000dd;\">11<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #b1b100;\">switch<\/span><span style=\"color: #009900;\">(<\/span>divisor<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x01<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x02<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">32<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x03<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">64<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x04<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">128<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x05<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">256<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x06<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000dd;\">1024<\/span><span style=\"color: #339933;\">:<\/span> mode <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x7<\/span><span style=\"color: #339933;\">;<\/span> <strong>break<\/strong><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0\u00a0 \u00a0<span style=\"color: #b1b100;\">default<\/span><span style=\"color: #339933;\">:<\/span> <span style=\"color: #b1b100;\">return<\/span><span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0 \u00a0 <span style=\"color: #009900;\">}<br \/>\n<\/span>\u00a0 \u00a0 TCCR2B <span style=\"color: #339933;\">=<\/span> TCCR2B <span style=\"color: #339933;\">&amp;<\/span> <span style=\"color: #208080;\">0b11111000<\/span> <span style=\"color: #339933;\">|<\/span> mode<span style=\"color: #339933;\">;<br \/>\n<\/span>\u00a0\u00a0<span style=\"color: #009900;\">}<br \/>\n}<\/p>\n<p>\u63d0\u4f9b\u53c2\u8003\u7f51\u5740\uff1a<br \/>\n<a href=\"http:\/\/arduino.cc\/playground\/Code\/PwmFrequency\" target=\"_blank\">http:\/\/arduino.cc\/playground\/Code\/PwmFrequency<\/a><br \/>\n<a href=\"http:\/\/arduino.cc\/playground\/Main\/TimerPWMCheatsheet\" target=\"_blank\">http:\/\/arduino.cc\/playground\/Main\/TimerPWMCheatsheet<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u7684PWM\u53e3\u53ea\u80fd\u901a\u8fc7\u8f93\u51fa\u5360\u7a7a\u6bd4\u6765\u8f93\u51fa\u6a21\u62df\u91cf\uff0c\u4f46\u662f\u5982\u679c\u8981\u8f93\u51fa1-60KHz\u6216\u66f4\u9ad8\u7684\u8109\u51b2\u9891\u7387\u7684\u8bdd\uff0c\u5c31\u4e0d\u597d\u505a\u5230\u5566\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,13],"tags":[],"class_list":["post-1073","post","type-post","status-publish","format-standard","hentry","category-arduino","category-13"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/1073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1073"}],"version-history":[{"count":1,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/1073\/revisions"}],"predecessor-version":[{"id":1074,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/1073\/revisions\/1074"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}