ezpwmforraspberry

1.0.1 • Public • Published

ezPWMForRaspberry

本模块使用了node-rpio, 所以,请配置以下环境。

由于使用pwm,需要sudo权限。 或者把pi用户放入gpio用户组。

Disable GPIO interrupts

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.

Enable /dev/gpiomem access

By default the module will use /dev/gpiomem when using simple GPIO access. To access this device, your user will need to be a member of the gpio group, and you may need to configure udev with the following rule (as root):

$ cat >/etc/udev/rules.d/20-gpiomem.rules <<EOF
SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660"
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 as root for access to /dev/mem.

安装

npm i ezpwmforraspberry --save

初始化

const ezPWM = require('ezpwmforraspberry');

const pwm = new ezPWM();

设置占空比

1.百分比方式(精度100, 从0~100)

// 打开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.高精度方式

// 打开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);
}

PWM频率说明

树莓派4B之前的版本,时钟频率是19.2MHz, 4B的时钟频率是54MHz。

给PWM口分配频率

通过divosor来设置PWM口的时钟频率的,

const pwm = new ezPWM('physical', 2048); 

如上,设置了2048, 则divosor=54e6/2048,约2.6367MHz频率。

设置了时钟频率后,再设置精度, 也可称为分辨率。 假设我们设置精度为1024

那么,2.6367Mhz/1024 ~= 25Hz, 也就是每秒25个1024分辨率的PWM信号。

设置占空比

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

Readme

Keywords

Package Sidebar

Install

npm i ezpwmforraspberry

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

9.91 kB

Total Files

5

Last publish

Collaborators

  • xujialiang