Raspberry pi 只有一个PWM。看到国外高手有人用软件模拟PWM。我也测试一下。以下部分只是测试记录,文档和代码均来自官方网站。
使用root用户安装softpwm,这个安装记录直接拷贝到系统里运行成功就行。
apt-get -y update
apt-get -y upgrade
apt-get install -y devscripts build-essential fakeroot dh-make mercurial dh-make
apt-get install -y devscripts build-essential fakeroot
mkdir temp
cd temp
hg clone http://hg.code.sf.net/p/raspberry-gpio-python/code raspberry-gpio-python -r 0.5.4
cd raspberry-gpio-python
python setup.py sdist
cd dist/
tar xvfz RPi.GPIO-0.5.4.tar.gz
mv RPi.GPIO-0.5.4 rpi.gpio-0.5.4
cd rpi.gpio-0.5.4
dh_make -s -f ../RPi.GPIO-0.5.4.tar.gz
rm -rf debian
cp -a ../../debian/ ./
debuild -us -uc
debuild clean
python setup.py install
测试代码如下,使用LED小灯测试:
#!/bin/env python # -*-coding:utf-8 -*- import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(7, GPIO.OUT) p = GPIO.PWM(7, 50) #frequency=100Hz p.start(0) try: while 1: for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1) except KeyboardInterrupt: pass p.stop() GPIO.cleanup()
来源
http://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/
发表评论