a pwm library
npm install ezpwmforraspberry本模块使用了node-rpio, 所以,请配置以下环境。
由于使用pwm,需要sudo权限。 或者把pi用户放入gpio用户组。
If running a newer Raspbian release, you will need to add the following line to/boot/config.txt and reboot:
```
dtoverlay=gpio-no-irq
Without this you may see crashes with newer kernels when trying to poll for pin
changes.
By default the module will use /dev/gpiomem when using simple GPIO access.gpio
To access this device, your user will need to be a member of the group,
and you may need to configure udev with the following rule (as root):
`shell`
$ cat >/etc/udev/rules.d/20-gpiomem.rules <
EOF
For access to i²c, PWM, and SPI, or if you are running an older kernel which
does not have the bcm2835-gpiomem module, you will need to run your programs/dev/mem
as root for access to .
``
npm i ezpwmforraspberry --save
`javascript
const ezPWM = require('ezpwmforraspberry');
const pwm = new ezPWM();
`
#### 1.百分比方式(精度100, 从0~100)
`javascript
// 打开PWM
pwm.openPWMByPercent(ezPWM.PWMPin.PIN12);
// 更新占空比
pwm.updatePWMByPercent(ezPWM.PWMPin.PIN12,0);
// 从0到100
for (let index = 0; index < 100; index++) {
rpio.sleep(0.1);
console.log('updatePWMByPercent', ezPWM.PWMPin.PIN12 ,index);
pwm.updatePWMByPercent(ezPWM.PWMPin.PIN12,index);
}
`
#### 2.高精度方式
`javascript
// 打开PWM
pwm.openPWM(ezPWM.PWMPin.PIN12, 0, total=1024);
// 更新占空比
pwm.updatePWM(ezPWM.PWMPin.PIN12, 0)
// 从0到100
for (let index = 0; index < 1024; index++) {
rpio.sleep(0.1);
console.log('updatePWMByPercent', ezPWM.PWMPin.PIN12 ,index);
pwm.updatePWMByPercent(ezPWM.PWMPin.PIN12,index);
}
`
树莓派4B之前的版本,时钟频率是19.2MHz, 4B的时钟频率是54MHz。
#### 给PWM口分配频率
通过divosor来设置PWM口的时钟频率的,
`javascript`
const pwm = new ezPWM('physical', 2048);
如上,设置了2048, 则divosor=54e6/2048,约2.6367MHz频率。
设置了时钟频率后,再设置精度, 也可称为分辨率。 假设我们设置精度为1024
那么,2.6367Mhz/1024 ~= 25Hz, 也就是每秒25个1024分辨率的PWM信号。
设置占空比
`javascript
pwm.openPWM(ezPWM.PWMPin.PIN12, low=0, total=1024);
pwm.updatePWM(ezPWM.PWMPin.PIN12, low=800);
``
参考博文:
https://raspberrypi.stackexchange.com/questions/4906/control-hardware-pwm-frequency/9725#9725
https://github.com/jperkin/node-rpio/issues/2