【问题标题】:Change the frequency of a digital pwm on Arduino Uno rev 3在 Arduino Uno rev 3 上更改数字 pwm 的频率
【发布时间】:2015-11-12 20:43:27
【问题描述】:

我正在尝试将 Arduino Uno 上的数字 pwm 引脚 #9 的频率更改为 70hz。我找到了一些更改引脚频率的代码示例,但没有什么很清楚。有人可以解释一下吗?谢谢

【问题讨论】:

    标签: arduino pwm


    【解决方案1】:

    Atmel ATMega 脉宽调制 (PWM) 硬件功能主要支持更改占空比。频率只能从几个固定选项中选择。

    在 arduino.stackexchange.com 上有一个非常好的和详细的解释。查看帖子:How can I set two PWM at two different frequencies?

    【讨论】:

    • 不正确,您可以使用具有相位和频率校正模式的 TIMER1 更改频率。您可以输出 0,119 到 4MHz 的频率。 Check this answer
    【解决方案2】:

    您可以通过更改 IRC1 和 OCRA1 值将引脚 9 PWM 更改为 70Hz。

        double freq = 70.0;    // Set frequency to 70Hz
    
        // Set Timer1 to phase and frequency correct mode. NON-inverted mode
        TCCR1A = _BV(COM1A1) | _BV(COM1B1); 
    
        // Set Timer1 prescaler to clk/8 (outputs from 15,259Hz to 500000Hz
        TCCR1B = _BV(WGM13) | _BV(CS11);
    
        //ICR Register, which controls the PWM total pulse length
        double icr = 8000000.0/8.0/freq; //ICR1 = (clk/2)/tmr_prescaler/freq.
        ICR1 = round(icr); // defines total PWM length in clock/8 
    
        // Now to change the PWM output value (duty cycle):
        double pwm_duty = 50.0 // Set PWM duty cycle to 50% (higher precision)
        // OR (delete line above or below)
        double pwm_duty = 120 /255.0; // is the same as analogWrite(9, 120)
    
        //OCR Registers, which control the PWM duty cycle.
        OCR1A = round(icr * pwm_duty/100.0);
        OCR1B = round(icr * (100.0-pwm_duty)/100.0);
    

    提示:您可以将其他频率值用于其他频率。

    请记住,频率越高,占空比分辨率就越低。

    注意: 此后,任何使用 TIMER1 的 Arduino 代码都将无法工作(或将无法正常工作)。 Arduino 使用 TIMER1 作为伺服库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多