1.参考链接
http://www.lai18.com/content/8359588.html
https://briancode.wordpress.com/2015/01/06/working-with-pwm-on-a-beaglebone-black/
2. 串口PIN脚对应表
EXPORT | NUMBERPIN | NAMEPINS |
---|---|---|
0 | EHRPWM0A | P9.22,P9.31 |
1 | EHRPWM0B | P9.21,P9.29 |
2 | ECAPPWM0 | P9.42 |
3 | EHRPWM1A | P9.14,P8.36 |
4 | EHRPWM1B | P9.16,P8.34 |
5 | EHRPWM2A | P8.19,P8.45 |
6 | EHRPWM2B | P8.13,P8.46 |
7 | ECAPPWM2 | P9.28 |
3.加载PWM设备驱动
首先加载am33xx_pwm, 各个pwm串口都会需要依赖它。
通过cat slots
可以查看已加载的结果列表,其中P-O-L表示已加载并且启用。
root@beaglebone:/sys/class/pwm# cd /sys/devices/bone_capemgr.9
root@beaglebone:/sys/devices/bone_capemgr.9# echo am33xx_pwm > slots
root@beaglebone:/sys/devices/bone_capemgr.9# cat slots
0: 54:PF---
1: 55:PF---
2: 56:PF---
3: 57:PF---
4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
7: ff:P-O-L Override Board Name,00A0,Override Manuf,am33xx_pwm
然后加载指定pwm设备模块。
root@beaglebone:/sys/devices/bone_capemgr.9# cd /sys/class/pwm
root@beaglebone:/sys/class/pwm# echo 6 > export
root@beaglebone:/sys/class/pwm# ls
export pwm6 pwmchip0 pwmchip2 pwmchip3 pwmchip5 pwmchip7 unexport
最后加载指定端口P8_13的pwm驱动。
root@beaglebone:/sys/class/pwm# cd /sys/devices/bone_capemgr.9
root@beaglebone:/sys/devices/bone_capemgr.9# echo bone_pwm_P8_13 > slots
root@beaglebone:/sys/devices/bone_capemgr.9# cd /sys/class/pwm
root@beaglebone:/sys/class/pwm# ls
export pwm6 pwmchip0 pwmchip2 pwmchip3 pwmchip5 pwmchip7 unexport
root@beaglebone:/sys/class/pwm# cd pwm6
root@beaglebone:/sys/class/pwm/pwm6# ls
device duty_ns period_ns polarity power run subsystem uevent
4. 通过shell操作PWM
修改period_ns
与duty_ns
可以改变输出电压, 从而达到控制外设的效果。
值得注意的是, 如果连接了pwm的外设使用了独立电源的话, 也需要把BBB的GND地线接口与独立电源的地线连接起来;否则因为回路不通而导致PWM控制失效。
另外,在每次修改两个值之前把run值置为0,修改完成后再置为1, 表示打开状态。
root@beaglebone:/sys/class/pwm/pwm6# echo 0 > run
root@beaglebone:/sys/class/pwm/pwm6# echo 1000000 > period_ns
root@beaglebone:/sys/class/pwm/pwm6# echo 990000 > duty_ns
root@beaglebone:/sys/class/pwm/pwm6# echo 1 > run
root@beaglebone:/sys/class/pwm/pwm6# echo 0 > run
root@beaglebone:/sys/class/pwm/pwm6# echo 1000000 > period_ns
root@beaglebone:/sys/class/pwm/pwm6# echo 100000 > duty_ns
root@beaglebone:/sys/class/pwm/pwm6# echo 1 > run
5. 通过python操作RGB led灯
连线图(分别使用了P8_13, P8_19, P9_14作为rgb的控制串口, P8_2作为地线回路串口)
python代码示例
import Adafruit_BBIO.PWM as PWM
import time
import random
red = "P8_13"
green = "P8_19"
blue = "P9_14"
PWM.start(red, 100)
PWM.start(blue, 100)
PWM.start(green, 100)
if __name__ == "__main__":
for i in range(0, 10000):
PWM.set_duty_cycle(red, random.randint(0, 100))
PWM.set_duty_cycle(green, random.randint(0, 100))
PWM.set_duty_cycle(blue, random.randint(0, 100))
time.sleep(0.5)