{"id":1075,"date":"2016-01-06T11:40:31","date_gmt":"2016-01-06T03:40:31","guid":{"rendered":"http:\/\/www.kumouse.com\/?p=1075"},"modified":"2016-01-06T11:40:31","modified_gmt":"2016-01-06T03:40:31","slug":"analogwrite%e6%80%8e%e9%ba%bc%e5%81%9apwm%e8%bc%b8%e5%87%ba%e6%94%b9pwm%e9%a0%bb%e7%8e%87%e5%ae%9a%e6%97%b6%e5%99%a8%e7%9b%b8%e9%97%9c","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=1075","title":{"rendered":"analogWrite\u600e\u9ebc\u505aPWM\u8f38\u51fa?\u6539PWM\u983b\u7387,\u5b9a\u65f6\u5668\u76f8\u95dc"},"content":{"rendered":"<p><strong>\u5927\u5bb6\u90fd\u77e5\u9053\u5728 Arduino UNO \u6709\u516d\u652f pin \u53ef\u4ee5\u4f7f\u7528 analogWrite( )\u505a PWM \u8f38\u51fa,<\/strong><br \/>\n<strong>\u5728\u677f\u5b50\u4e0a pin \u65c1\u908a\u6a19\u793a\u6709 &#8220;~&#8221; \u7b26\u865f, analogWrite( ) \u53ef\u4ee5\u7528\u4f86\u7522\u751f\u6a21\u64ec\u96fb\u58d3,<\/strong><br \/>\n\u5f88\u591a\u4eba\u4e00\u5b9a\u5f88\u597d\u5947\u90a3\u662f\u600e\u9ebc\u505a\u5230\u7684 ?<\/p>\n<p><strong>\u9996\u5148\u4f86\u770b\u770b PWM \u6a21\u64ec\u96fb\u58d3\u7684\u539f\u7406, \u9019\u5728\u5b98\u7db2\u4e0a\u5c31\u6709\u89e3\u8aaa\u4e86:<\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u00a0 \u00a0\u00a0\u00a0<a href=\"http:\/\/arduino.cc\/en\/Tutorial\/SecretsOfArduinoPWM\" target=\"_blank\">http:\/\/arduino.cc\/en\/Tutorial\/SecretsOfArduinoPWM<\/a><\/span><\/strong><br \/>\n<strong><span style=\"color: #8b0000;\">\u6240\u8b02\u7684 PWM \u5168\u7a31\u662f Pulse-Width Modulation (PWM), \u5176\u5be6\u9019\u4e5f\u6c92\u5565\u5b78\u554f,<\/span><\/strong><br \/>\n<strong><span style=\"color: #8b0000;\">\u5c31\u662f\u5c0d GPIO \u8173\u4f4d\u4e0d\u65b7\u7684\u5207\u63db &#8220;\u6709\u96fb&#8221; &#8220;\u6c92\u96fb&#8221;, <\/span><\/strong><br \/>\n<strong>\u6bcf\u79d2\u9418\u5faa\u74b0\u5e7e\u6b21\u5373\u70ba\u5176 Frequency(\u983b\u7387), <\/strong><br \/>\n<strong>\u6bcf\u6b21&#8221;\u6709\u96fb&#8221;\u6642\u9593\u4f54\u4e00\u500b\u5faa\u74b0\u7684\u767e\u5206\u6bd4\u7a31\u70ba\u5176\u4f54\u7a7a\u6bd4(Duty cycle);<\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u4ee5\u4e0b\u662f\u5b98\u7db2\u4e0a\u7684\u6a21\u64ec\u7bc4\u4f8b:<\/span><\/strong><br \/>\n<span style=\"color: #0000ff;\">int pin = 13;<\/span><br \/>\n<span style=\"color: #0000ff;\">void setup() {<\/span><br \/>\n<span style=\"color: #0000ff;\">\u00a0\u00a0pinMode(pin, OUTPUT);<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">void loop(){<\/span><br \/>\n<span style=\"color: #0000ff;\">\u00a0\u00a0digitalWrite(pin, HIGH);<\/span><br \/>\n<span style=\"color: #0000ff;\">\u00a0\u00a0delayMicroseconds(100); \/\/ Approximately 10% duty cycle @ 1KHz<\/span><br \/>\n<span style=\"color: #0000ff;\">\u00a0\u00a0digitalWrite(pin, LOW);<\/span><br \/>\n<span style=\"color: #0000ff;\">\u00a0\u00a0delayMicroseconds(1000 &#8211; 100);<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n\u9019\u500b\u7bc4\u4f8b\u4e2d, <strong><span style=\"color: #0000ff;\">\u4e00\u500b\u5faa\u74b0\u662f 1000 us = 1ms, \u6240\u4ee5\u4e00\u79d2\u5faa\u74b0 1000\u6b21, \u56e0\u6b64 Frequency \u662f 1 KHz,<\/span><\/strong><br \/>\n\u6bcf\u500b\u5faa\u74b0\u4e2d, <strong><span style=\"color: #0000ff;\">\u6709\u96fb\u7684\u6bd4\u7387\u662f 100\/1000 * 100% = 10%, \u6240\u4ee5 duty cycle (\u4f54\u7a7a\u6bd4)\u70ba 10%;<\/span><\/strong><br \/>\n\u9019\u6a23\u5c31\u53ef\u4ee5\u6a21\u64ec\u51fa 5Volt x 10% = <strong><span style=\"color: #0000ff;\">0.5 Volt \u7684\u96fb\u58d3!<\/span><\/strong><br \/>\n\u5982\u679c\u771f\u7684\u9019\u6a23\u505a, \u6709\u597d\u8655\u4e5f\u6709\u58de\u8655, \u5b98\u7db2\u4e0a\u5df2\u7d93\u8aaa\u4e86:<br \/>\n\u597d\u8655\u662f\u4efb\u4e00\u652f pin \u90fd\u53ef\u9019\u6a23\u7528, \u5305\u62ec Pint 0 \u5230 Pin 13, \u4ee5\u53ca Pin A0 \u5230 A5 \u5171 20\u652f pin \u90fd\u53ef\u4ee5!<br \/>\n\u58de\u8655\u537b\u66f4\u591a, \u9996\u5148\u5c31\u662f\u983b\u7387(Frequency)\u548c\u4f54\u7a7a\u6bd4(duty cycle)\u53ef\u80fd\u53d7\u4e2d\u65b7(Interrup)\u7684\u5f71\u97ff\u8b8a\u6210\u4e0d\u662f\u5f88\u6e96\u78ba !!<br \/>\n<strong><span style=\"color: #ff0000;\">\u6700\u5927\u7684\u58de\u8655\u662f, \u5728\u67d0\u652f pin \u505a PWM \u8f38\u51fa\u671f\u9593\u90fd\u6c92\u8fa6\u6cd5\u505a\u5225\u7684\u4e8b\u60c5 !!<\/span><\/strong><\/p>\n<p>\u65e2\u7136\u8aaa\u9019\u53ea\u662f\u793a\u7bc4\u53ef\u4ee5\u9019\u6a23\u505a, <span style=\"color: #0000ff;\"><strong>\u5728\u00a0\u00a0Arduino \u7576\u7136\u4e0d\u53ef\u80fd\u662f\u9019\u9ebc\u505a,<\/strong><br \/>\n<strong>\u90a3 Arduino \u662f\u600e\u9ebc\u505a\u7684\u5462?<\/strong><\/span><br \/>\n\u5c31\u662f\u900f\u904e <strong>Timer <\/strong>\u8a08\u6642\u5668\u76f4\u63a5\u63a7\u5236 pin \u505a PWM \u8f38\u51fa, Arduino UNO \u7684 MCU \u6709\u4e09\u500b timer,<br \/>\n\u5176\u4e2d <strong>timer0 <\/strong>\u63a7\u5236 <strong>pin 5, pin 6; <span style=\"color: #0000ff;\">timer1 <\/span><\/strong>\u63a7\u5236 <strong><span style=\"color: #0000ff;\">pin 9, pin 10;<\/span><\/strong> <span style=\"color: #8b0000;\"><strong>timer 2 \u63a7\u5236 pin 11, pin 3;<\/strong><\/span><br \/>\n\u6240\u4ee5, \u6211\u5011\u53ef\u4ee5\u5c0d\u9019\u4e9b <strong>pin <\/strong>\u7528<strong><span style=\"color: #0000ff;\"> analogWrite(pin, val);<\/span><\/strong> \u8f38\u51fa 0 \u5230 255 \u7684 <strong>val <\/strong>\u503c\u5230 pin ;<br \/>\n<strong>\u5982\u679c\u8f38\u51fa val \u662f 0, \u5b83\u6703\u5077\u5077\u76f4\u63a5\u6539\u7528 digitalWrite(pin, 0); \u8f38\u51fa,<\/strong><br \/>\n<strong>\u5982\u679c val \u662f 255, \u4e5f\u662f\u6703\u5077\u5077\u76f4\u63a5\u6539\u7528 digitalWrite(pin, 1); \u8f38\u51fa!<\/strong><br \/>\n<strong><span style=\"color: #8b0000;\">\u5982\u679c val \u662f 1 \u5230 254, \u5247\u6703\u4e0b\u547d\u4ee4<\/span><span style=\"color: #0000ff;\">\u8acb pin \u8173\u5c0d\u61c9\u7684 timer \u8a08\u6642\u5668<\/span><span style=\"color: #8b0000;\">(\u5b9a\u6642\u5668)\u5e6b\u5fd9!!<\/span><\/strong><br \/>\n<strong>How ?<\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">\u9996\u5148\u8981\u77e5\u9053 timer \u7684\u57fa\u672c\u77e5\u8b58:<\/span><\/strong><br \/>\n<strong>(1)\u6bcf\u500b timer \u4e00\u5b9a\u6709\u500b counter, \u4f8b\u5982 timer0 \u7684TCNT0, timer1 \u7684TCNT1, timer2 \u7684TCNT2;<\/strong><br \/>\n\u8a72 counter \u4e00\u5b9a\u662f\u6bcf\u500b tick \u6703\u52a0 1, \u6bcf\u500b tick \u901a\u5e38\u662f\u628a CPU \u7684 clock \u62ff\u4f86\u7d93\u904e\u4e00\u500b\u9664\u983b\u96fb\u8def,<br \/>\n\u7136\u5f8c\u7d66 timer \u4f7f\u7528; Arduino UNO \u63a1\u7528 AVR ATmega328 MCU, \u4e14 clock Rate \u662f 16MHz,<br \/>\n\u6bcf\u500b timer \u7684\u9664\u983b Prescaler \u662f\u7368\u7acb\u8a2d\u5b9a\u7684, \u901a\u5e38\u53ef\u4ee5\u8a2d 1, 2, 4, 8, 64, 256, or 1024 \u7b49,<br \/>\n\u9019\u5fc5\u9808\u770b MCU \u7684 datasheet.<\/p>\n<p><strong>(2)\u6bcf\u500b timer \u901a\u5e38\u63d0\u4f9b\u8a31\u591a mode \u904b\u4f5c\u6a21\u5f0f, \u4f8b\u5982 counter \u6ea2\u51fa(Overflow)\u6216Rollover\u6b78\u96f6\u6642\u7522\u751f\u4e2d\u65b7,<\/strong><br \/>\n\u6216 TCNT? \u9054\u5230\u67d0\u500b\u503c\u6642\u7522\u751f\u4e2d\u65b7\u7b49, Arduino ATmega328 \u7684 timer \u6709 16\u7a2e mode, \u8a31\u591a Mode \u662f\u8207 PWM \u6709\u95dc;<br \/>\n<strong>\u8981\u8a2d\u5b9a timer \u7684 Mode \u53ef\u4ee5\u900f\u904e\u4fee\u6539 timer \u7684\u63a7\u5236\u66ab\u5b58\u5668, \u4f8b\u5982 TCCR?A, TCCR?B, <\/strong><strong><span style=\"color: #8b0000;\">\u00a0 \u00a0\u6ce8\u610f\u4ee5 ATmega328 \u70ba\u4f8b, TCCR?A \u548c TCCR?B \u8981\u5408\u8d77\u4f86\u7528, \u6b64\u8655\u7684 A, B \u8207 channel A, channel B \u7121\u95dc!!<\/span><\/strong><\/p>\n<p><strong>(3)\u6bcf\u500b timer \u901a\u5e38\u6709\u6bd4\u8f03\u66ab\u5b58\u5668(Compare Register), \u7576 TCNT? \u503c\u8207\u8a72\u4e9b\u6bd4\u8f03\u66ab\u5b58\u5668\u76f8\u540c\u6642\u53ef\u4ee5\u505a\u67d0\u4e8b,<\/strong><br \/>\n<strong>\u00a0 \u00a0\u4e0d\u4e00\u5b9a\u662f\u5c0d CPU \u7522\u751f\u4e2d\u65b7!<\/strong> Arduino<strong> \u6bcf\u500b timer \u6709\u5169\u500b\u6bd4\u8f03\u66ab\u5b58\u5668<\/strong>, \u5206\u5225\u547d\u540d<strong> OCR?A \u548c OCR?B,<\/strong><br \/>\n<strong>\u00a0\u00a0\u5176\u4e2d ? \u662f 0, 1, 2 \u5206\u5225\u5c0d\u61c9\u5230 timer0, timer1, \u548c timer2 \u9019\u4e09\u500b\u8a08\u6642\u5668.<br \/>\n<\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">\u4f60\u53ef\u4ee5\u5148\u5077\u770b analogWrite( ) \u7684\u7a0b\u5e8f\u78bc:<\/span><\/strong><br \/>\n<strong>\u00a0 \u00a0 \u5728\u4f60 Arduino IDE \u4e0b\u7684 hardware\\arduino\\cores\\arduino\\wiring_analog.c<\/strong><br \/>\n<strong>\u5f88\u7c21\u55ae, \u771f\u6b63\u8acb timer \u5e6b\u5fd9\u53ea\u505a\u4e09\u4ef6\u4e8b: a.\u627e\u51fa\u5c0d\u61c9\u7684 port, b.\u8a2d\u5b9a\u63a7\u5236\u66ab\u5b58\u5668, c.\u586b\u5165 analog\u7684\u503c\u5230\u6bd4\u8f03\u66ab\u5b58\u5668!<\/strong><br \/>\n\u4e0d\u904e\u4f60\u6703\u767c\u73fe\u770b\u4e0d\u592a\u61c2, \u56e0\u70ba\u9084\u4e0d\u77e5\u9053\u786c\u9ad4 timer \u63a7\u5236 PWM \u904b\u4f5c\u65b9\u5f0f\u8207\u539f\u7406!<br \/>\n\u4e0d\u60f3\u770b datasheet \u53ef\u4ee5\u53c3\u8003\u9019:<br \/>\n<a href=\"http:\/\/letsmakerobots.com\/content\/arduino-101-timers-and-interrupts\" target=\"_blank\">http:\/\/letsmakerobots.com\/conten &#8230; mers-and-interrupts<\/a><\/p>\n<p>\u4ee5Arduino UNO \u7684 timer1\u70ba\u4f8b, \u5728 mode 5 (Fast PWM, 8 bit), \u6b64\u6642, TCNT1 \u5f9e 0 \u6578\u5230 255, \u7136\u5f8c\u53c8\u52a0 1 \u5c31\u8b8a0, &#8230;<br \/>\n\u901a\u5e38\u5f9e 255 (\u6b64 mode \u7684\u6700\u5927\u503c)\u53c8\u52a0 1 \u8b8a\u70ba 0 \u4e4b\u6642\u6703\u7522\u751f OVF \u4e2d\u65b7(TIMSK1\u7684TOIE1\u8981 set), \u4e0d\u904e\u9019\u8207 PWM \u7121\u95dc!<br \/>\n<strong><span style=\"color: #8b0000;\">PWM \u4e0d\u662f\u7528 Interrupt\u00a0\u00a0\u4e2d\u65b7\u8acb\u6c42\u505a\u7684, \u4e0d\u5fc5\u9ebb\u7169 CPU, CPU \u53ea\u8981\u4e0b\u547d\u4ee4\u7d66 timer, timer \u5c31\u6703\u7167\u547d\u4ee4\u57f7\u884cPWM\u5de5\u4f5c !!<\/span><\/strong><br \/>\n<strong>\u00a0 \u00a0 PWM \u662f\u5229\u7528\u6bcf\u500b timer \u4e0a\u7684\u5169\u500b&#8221;\u5339\u914d\u7b26\u5408\u8f38\u51fa&#8221;\u66ab\u5b58\u5668(Compare Match Output) COM?A \u548c COM?B;<\/strong><br \/>\n(\u6ce8\u610f\u96d6\u662f Compare Match Output, \u4f46\u66ab\u5b58\u5668\u540d\u7a31\u662f<strong> COM<\/strong>xy \u4e0d\u662f CMOxy \u5594 !)<br \/>\n<strong>\u5728timer1 \u7684 mode 5, <\/strong>\u53c8\u7a31 Fast PWM mode, (\u4e0d\u904e\u8acb\u6ce8\u610f Arduino \u7684 init( ) \u8a2d\u5b9a\u53ea\u6709 timer0 \u7528\u9019, \u53e6\u5916 timer1 \u548c timer2 \u4e0d\u662f\u7528\u9019 mode),<br \/>\n<strong>\u9019\u6642\u53ef\u4ee5\u628a 1 \u5230 254 \u4e4b\u9593\u7684\u503c\u653e\u5165 OCR1A \u6216 OCR1B \u4ee5\u4fbf\u63a7\u5236 pin 9 \u6216 pin 10<br \/>\n\u7684 PWM duty cycle, 1 \u5230 254 \u5206\u5225\u5c0d\u61c9\u5230 (1+1)\/256, .., (254+1)\/256 \u7684 duty cycle.<\/strong><br \/>\n\u6703 +1 \u662f\u786c\u9ad4\u96fb\u8def\u8a2d\u8a08\u4e0a\u7684\u95dc\u4fc2, data sheet \u4e0a\u8aaa:<br \/>\n<strong><span style=\"color: #0000ff;\">\u00a0 \u00a0Note that fast PWM holds the output high one cycle longer than the compare register value.<\/span><\/strong><br \/>\n\u5728 TCNT1 \u7b49\u65bc 0\u00a0\u00a0\u4e4b\u6642, COM1A and\/or COM1B \u6703\u8f38\u51fa(\u7576\u7136\u8981 TCCR1A \u5167\u7684 COM1A1 and\/or COM1B1 \u6709set),<br \/>\n\u7136\u5f8c\u5728 TCNT1 \u7b49\u65bc OCR1A \u5247\u95dc\u9589 COM1A, \u7576 TCNT1 \u7b49\u65bc OCR1B \u5247\u95dc\u9589 COM1B,<br \/>\n\u6ce8\u610f\u6c92\u6709\u7acb\u5373\u95dc\u9589, \u662f\u5ef6\u9072\u4e00\u500b tick \u624d\u95dc\u9589 ! \u6240\u4ee5\u624d\u6703\u591a\u52a01, \u56e0\u70ba\u4e00\u500b\u5faa\u74b0\u662f 256, \u4e0d\u662f 255,\u5982\u679c\u4e0d\u5ef6\u9072\u52a0 1, \u5247\u8f38\u51fa val \u662f 254 \u6642\u8b8a\u6210 254\/256, \u9084\u5dee\u4e00\u9ede\u9ede, \u6240\u4ee5\u72a7\u7272 1\/256, \u5c31\u662f\u6c92\u6709 1\/256\u4f54\u7a7a\u6bd4 !!<\/p>\n<p><strong><span style=\"color: #0000ff;\">\u00a0 \u00a0\u7531\u65bc Arduino \u7684 init( )\u628a timer1 \u7684 Prescaler \u8a2d\u5b9a\u70ba 64,<\/span><\/strong><br \/>\n<strong>(\u53c3\u8003\u5728\u4f60 Arduino IDE \u5167\u7684\u00a0 \u00a0hardware\\arduino\\cores\\arduino\\wiring.c )<\/strong><br \/>\n\u4e14\u628a timer1 \u8a2d\u5b9a\u70ba<strong><span style=\"color: #0000ff;\"> 8-bit phase correct pwm mode,<\/span><\/strong> \u6240\u4ee5\u5176\u983b\u7387\u662f 490.196Hz, \u4e0d\u662f 976.5625Hz;<br \/>\n<strong><span style=\"color: #0000ff;\">\u6240\u8b02\u7684 8-bit phase correct pwm mode, \u610f\u601d\u662f TCNT? \u5f9e 0 \u6578\u5230 255, \u63a5\u8457\u53c8\u5f9e 255 \u5012\u8457\u6578\u56de 0,<\/span><br \/>\n<span style=\"color: #8b0000;\">\u90a3\u4f55\u6642\u628a COM1A and\/or COM1B \u7684\u8f38\u51fa\u6253\u958b\u6216\u95dc\u9589\u5462?<\/span><\/strong><span style=\"color: #8b0000;\"><br \/>\n<\/span>\u6839\u64da datasheet, \u5728<strong>\u5f9e 0 \u5f80\u4e0a\u6578, \u78b0\u5230 OCR1A \u6642\u628a COM1A \u95dc\u9589,<\/strong><br \/>\n\u7136<strong><span style=\"color: #0000ff;\">\u5f8c\u5f9e 255 \u5f80\u56de\u6578, \u6578\u5230 OCR1A \u6642\u628a COM1A \u6253\u958b(\u6709\u96fb); <\/span><\/strong>\u5c0d\u65bc OCR1B \u548c COM1B \u4e5f\u662f\u9019\u6a23!<br \/>\n\u9019\u4f7f\u5f97 duty cycle (\u4f54\u7a7a\u6bd4) \u66f4\u6e96\u78ba, \u4e5f\u5c31\u662f<strong> val 1 ~ 254 \u5206\u5225\u5c0d\u61c9\u5230 1\/255 \u5230 254\/255 \u7684 duty cycle.<\/strong><br \/>\n\u4f46\u662f Frequency \u5247\u4e0d\u662f\u9664\u4ee5 256, \u662f\u8981\u9664\u4ee5 255 \u518d\u9664\u4ee5 2, \u65bc\u662f: (\u6ce8\u610f\u662f 255, \u4e0d\u662f 256\u5594!)<br \/>\n<strong><span style=\"color: #ff0000;\">Frequency = 16 MHz \/ 64 \/ 255 \/ 2 = 490.196Hz;<br \/>\n<\/span><\/strong><br \/>\n<strong>timer 2 \u4e5f\u662f\u5728 init( )\u88ab\u8a2d\u70ba Prescaler 64 \u7684<span style=\"color: #0000ff;\"> phase correct pwm (8-bit);<\/span><\/strong><br \/>\n<strong><span style=\"color: #8b0000;\">\u4f46\u662f, timer0 \u96d6\u7136 Prescaler \u4e5f\u8a2d 64, <\/span><span style=\"color: #000000;\">\u4f46 PWM \u662f\u7528<\/span><span style=\"color: #8b0000;\"> Fast PWM mode,<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u4e0d\u4f7f\u7528 phase correct mode \u662f\u70ba\u4e86\u907f\u514d\u5f71\u97ff\u7dad\u8b77 millis( ) \u7684\u4e2d\u65b7 timer0 Overflow Interrupt,<\/span><\/strong><br \/>\n<strong>\u5373 ISR(TIMER0_OVF_vect) \u9019\u4e2d\u65b7\u8655\u7406\u7a0b\u5e8f, \u5426\u5247 millis( ) \u548c micros( ) \u4ee5\u53ca delay() \u90fd\u6703\u53d7\u5230\u5f71\u97ff !!<\/strong><br \/>\n\u56e0\u6b64 , <strong>timer0 <\/strong>\u63a7\u5236\u7684 <strong>PWM <\/strong>\u5176 <strong>Frequency <\/strong>\u662f <strong>976.5625Hz<\/strong>,<br \/>\n<strong><span style=\"color: #ff0000;\">\u00a0 \u00a0 16 MHz \/ 64 \/ 256 = 976.5625Hz<\/span><\/strong><br \/>\n\u6ce8\u610f<strong>\u7528 timer0 \u63a7\u5236\u7684pin 5, pin 6 \u4e4b PWM \u7684 duty cycle \u7121\u6cd5\u662f 1\/256, \u5b83\u662f 0 \u518d\u4f86\u5c31 2\/256\u4e86!<br \/>\n\u7d50\u8ad6:<\/strong><br \/>\n<strong>timer0 \u63a7\u5236 pin 5, pin 6,<\/strong> PWM \u983b\u7387<strong> 976.5625Hz<\/strong>, duty cycle\u53ef\u4ee5 2\/256 ~ 255\/256 (\u5c0d\u61c9 1 \u5230254);<br \/>\n<strong>timer1 \u63a7\u5236 pin 9, pin 10,<\/strong> PWM \u983b\u7387<strong> 490.196Hz<\/strong>, duty cycle \u53ef\u4ee5 1\/255 ~ 254\/255(\u5c0d\u61c9 1 \u5230254);<br \/>\n<strong>timer2 \u63a7\u5236 pin 11, pin 3<\/strong>, PWM \u983b\u7387 \u8207 duty cycle \u8ddf <strong>timer1 <\/strong>\u63a7\u5236\u7684\u76f8\u540c !!<\/p>\n<p><strong><span style=\"color: #ff0000;\">Q: \u90a3 PWM \u7684 Frequency \u53ef\u4e0d\u53ef\u4ee5\u66f4\u6539?<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">A: \u53ef\u4ee5, \u5077\u6539 timer \u7684 Prescaler \u5c31\u53ef\u4ee5\u9054\u5230\u66f4\u6539 Frequency \u7684\u76ee\u7684 !<\/span><\/strong><br \/>\n\u4f46\u662f, <strong>\u5343\u842c\u4e0d\u8981\u66f4\u6539 timer0 \u7684 Prescaler, \u5426\u5247 millis( ) \u548c micros( ) \u4ee5\u53ca delay() \u90fd\u6703\u53d7\u5230\u5f71\u97ff !!!<\/strong><br \/>\n\u4ee5\u4e0b\u662f\u4ee5<span style=\"color: #8b0000;\"><strong> timer1 \u63a7\u5236\u7684 pin 9, pin 10<\/strong> <\/span>\u70ba\u4f8b(\u6ce8\u610f\u5169\u500b pin \u7684\u983b\u7387\u76f8\u540c!)<br \/>\n\u5728\u4f60\u7684 setup( ) { \u5167, \u5beb\u5982\u4e0b\u5169\u53e5\u5373\u53ef:<br \/>\n<strong><span style=\"color: #0000ff;\">\u00a0 \u00a0\u00a0 \u00a0int fff = 3;\u00a0\u00a0\/\/ \u53ef\u4ee5\u662f 1, 2, 3, 4, 5<\/span><\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u00a0 \u00a0\u00a0 \u00a0TCCR1B = TCCR1B &amp; 0xF8 | fff;<\/span><\/strong><br \/>\n\u5176\u4e2d fff \u8207\u5c0d\u61c9\u983b\u7387\u5982\u4e0b:<br \/>\n<strong>\u00a0\u00a0fff\u00a0 \u00a0Prescaler\u00a0 \u00a0Frequency<\/strong><br \/>\n<strong>\u00a0 \u00a01\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a01\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a031372.549 Hz<\/strong><br \/>\n<strong>\u00a0 \u00a02\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a08\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 3921.569<\/strong><br \/>\n<strong>\u00a0 \u00a03\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a064\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0490.196\u00a0 \u00a0&lt;&#8211;DEFAULT<\/strong><br \/>\n<strong>\u00a0 \u00a04\u00a0 \u00a0\u00a0 \u00a0 256\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0122.549<\/strong><br \/>\n<strong>\u00a0 \u00a05\u00a0 \u00a0 1024\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a030.637 Hz<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">\u81f3\u65bc<\/span><span style=\"color: #8b0000;\"> timer2 \u63a7\u5236\u7684 pin 11 \u548c pin 3<\/span><span style=\"color: #ff0000;\">,<\/span><\/strong><br \/>\n\u5247\u5728 setup( ) { \u5167\u5beb:<br \/>\n<strong><span style=\"color: #0000ff;\">\u00a0 \u00a0\u00a0\u00a0TCCR2B = TCCR2B &amp; 0xF8 | ?;<\/span><\/strong><br \/>\n<strong>\u6b64\u8655\u7684 ? \u53ef\u4ee5\u6709\u4e03\u7a2e:<\/strong><br \/>\n<strong>\u00a0 \u00a0?\u00a0\u00a0Prescaler\u00a0 \u00a0Frequency<\/strong><br \/>\n<strong>\u00a0 \u00a01\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a01\u00a0 \u00a0\u00a0 \u00a0 31372.549 Hz<\/strong><br \/>\n<strong>\u00a0 \u00a02\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a08\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a03921.569<\/strong><br \/>\n<strong>\u00a0 \u00a03\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a032\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 980.392<\/strong><br \/>\n<strong>\u00a0 \u00a04\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a064\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0490.196\u00a0 \u00a0&lt;&#8211;DEFAULT<\/strong><br \/>\n<strong>\u00a0 \u00a05\u00a0 \u00a0\u00a0 \u00a0128\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0245.098<\/strong><br \/>\n<strong>\u00a0 \u00a06\u00a0 \u00a0\u00a0 \u00a0256\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0122.549<\/strong><br \/>\n<strong>\u00a0 \u00a07\u00a0 \u00a0 1024\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a030.637 Hz<\/strong><\/p>\n<p><strong>\u5982\u679c\u4f60\u5805\u6301<span style=\"color: #8b0000;\">\u8981\u6539 timer0 \u7684 Prescaler, \u4ee5\u66f4\u6539 pin 5, pin 6 <\/span><span style=\"color: #000000;\">\u7684 PWM \u983b\u7387:<\/span><\/strong><br \/>\n(<span style=\"color: #ff0000;\">\u6ce8\u610f <strong>millis( ) \u548c micros( ) \u4ee5\u53ca delay() <\/strong>\u90fd\u6703\u53d7\u5230\u5f71\u97ff !!<\/span> )<br \/>\n<strong>\u5247\u5728 setup( ) { \u5167\u5beb:<\/strong><br \/>\n<strong><span style=\"color: #0000ff;\">\u00a0 \u00a0\u00a0\u00a0TCCR0B = TCCR0B &amp; 0xF8 | ?;<\/span><\/strong><br \/>\n\u6b64\u8655\u7684 ? \u53ef\u4ee5\u6709\u4e94\u7a2e:<br \/>\n<strong>\u00a0 \u00a0?\u00a0\u00a0Prescaler\u00a0 \u00a0Frequency<\/strong><br \/>\n<strong>\u00a0 \u00a01\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 1\u00a0 \u00a0\u00a0 \u00a0 362500 Hz<\/strong><br \/>\n<strong>\u00a0 \u00a02\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 8\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a07812.5<\/strong><br \/>\n<strong>\u00a0 \u00a03\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a064\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 976.5625\u00a0 \u00a0&lt;&#8211;DEFAULT<\/strong><br \/>\n<strong>\u00a0 \u00a04\u00a0 \u00a0\u00a0 \u00a0256\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0 244.140625<\/strong><br \/>\n<strong>\u00a0 \u00a05\u00a0 \u00a0 1024\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a061.03515625 Hz<\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">\u53c3\u8003:<\/span><\/strong><br \/>\n<a href=\"http:\/\/playground.arduino.cc\/Main\/TimerPWMCheatsheet\" target=\"_blank\">http:\/\/playground.arduino.cc\/Main\/TimerPWMCheatsheet<\/a><br \/>\n<a href=\"http:\/\/www.atmel.com\/Images\/doc8161.pdf\" target=\"_blank\">http:\/\/www.atmel.com\/Images\/doc8161.pdf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u90fd\u77e5\u9053\u5728 Arduino UNO \u6709\u516d\u652f pin \u53ef\u4ee5\u4f7f\u7528 analogWrite( )\u505a PWM \u8f38\u51fa, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/1075","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=1075"}],"version-history":[{"count":1,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/1075\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/1075\/revisions\/1076"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}