PWM

这篇文档主要介绍如何使用硬件PWM(Pulse Width Modulation)。

设置以及打开PWM

PWM_F:
1
2
3
4
$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/export
$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
$ echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
PWM_D:
1
2
3
4
$ echo 1 | sudo tee /sys/class/pwm/pwmchip1/export
$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/period
$ echo 500000 | sudo tee /sys/class/pwm/pwmchip1/pwm1/duty_cycle
$ echo 1 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
PWM_F:
1
2
3
4
$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/export
$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
$ echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
PWM_F:
1
2
3
4
$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/export
$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
$ echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable

如果你成功开启了硬件PWM,你可以通过示波器看到波形:

pwm-Oscilloscope

注意

如果使用示波器去查看波形,记得板子和示波器需要共地。

关闭PWM

1
$ echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
1
$ echo 0 | sudo tee /sys/class/pwm/pwmchip1/pwm1/enable
1
$ echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
1
$ echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable