|
|
参照论坛上的帖子用自带的pwm口调速12V的风扇没问题,但在速度较低的情况下出现啸叫,希望通过提高pwm频率解决这个问题,测试了几个方法都不成功。从DFRobot找来这个段代码,无法调节空占比,无法调速,请高手指点下,另外求高手给出更完美的解决方案,谢谢。
- /**************************************************************************************************
- *
- * PWM_Timer2_31kHz_00
- *
- * Version: 00 - Mai 2010
- * Author: Tom Pawlofsky www.caad.arch.ethz.ch tom-DOT-pawlofsky-AT-arch-DOT-ethz-DOT-ch
- *
- * Desc: change prescaler of Timer 2 to have 31kHz Frequency at pin 3 and 11
- *
- ***************************************************************************************************/
- int pinA = 3; // pin 3 and 11 are PWM output controled by Timer2
- int pinB = 11; // connect pinA/B to H-Bridge
- void setup(){
- //__________________________________TIMER2_for_Motor_PWM_________________________________
- // set TIMER2 for PWM 32 Hz
- //
- // clear all prescaler bits in TCCR2B = the last 3 Bits
- // leave other bits as set by arduino init() in wiring.c
- byte mask = B11111000;
- TCCR2B &= mask; // TCCR2B is now xxxxx000
- //
- // set CS22:20 in TCCR2B see p 156 of datasheet
- TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001
- //__pinmode
- pinMode(pinA,OUTPUT);
- pinMode(pinB,OUTPUT);
- //
-
- }
- void loop(){
- analogWrite(pinA,128); // 50% Duty
- analogWrite(pinB,32); //12.5 % Duty
- }
复制代码 |
|