Categories
python

树莓派 香橙派 PWM问题

查看GPIO引脚

gpio readall

用python调用PWM有两种方法

1.RPi.GPIO

import RPi.GPIO  as GPIO


GPIO.setmode(GPIO.BCM)

GPIO.setup(18,GPIO.OUT) #设置引脚为输出
pwm = GPIO.PWM(18,100) #只有12脚 和 18脚 支持硬件PWM   100为100hz
pwm.start(50) #占空比为50并输出

pwm.changeDutyCycle(20) #改变占空比
pwm.stop() #停止PWM波形输出

2OPI.GPIO

PWM is created in the form of an object.

1. First set up OPi.GPIO and create the object

    .. code:: python

       import OPi.GPIO as GPIO
       PWM_Class = GPIO.PWM(PWM_chip, PWM_pin, frequency_Hz, Duty_Cycle_Percent)


一般PWM_chip 为0 第一块 可以通过
 ls -l /sys/class/pwm/ 查看 一般返回 pwmchip0

PWM_pin 就是引脚了 


    Note currently you do not need to specify setmode before creating the class as for a GPIO
    only the PWM_chip number and the PWM_pin.
    The reson for this and how to find what they are is explained in the sysfs PWM section.

2. Begin the PWM cycle

    .. code:: python

        PWM_Class.start_pwm()

3. Change PWM duty Cycle

    .. code:: python
        PWM_Class.duty_cycle(50)

    Note this changes the Duty cycle to 50%

4. Change the frequency

    .. code:: python
        PWM_Class.change_frequency(500)

    Note this changes the Frequency to 500Hz

5. Stop the PWM device

    .. code:: python
        PWM_Class.stop_pwm()

    Note this stops the signal by setting the duty cycle to 0%

6. Change the Polarity of the signal

    .. code:: python
        PWM_Class.pwm_polarity()

    Note this changes swaps the on-off times. For example a duty cycle set to 75% before
    this will result in the signal being on 75% and off 25% of the time. After this is
    called it would be on 25% and off 75%.

7. Remove PWM Object

    .. code:: python
        PWM_Class.pwm_close()

3.wiringpi

import wiringpi

pwm0 =  wiringpi.GPIO(0)        
pwm0.pinMode(1,pwm0.PWM_OUTPUT)
pwm0.pwmSetMode(pwm0.PWM_MODE_MS)
pwm0.pwmSetClock(25)
pwm0.pwmWrite(1,self.brightness)




import wiringpi

PWM_OUTPUT  = 2
PWM_PIN = 1  #指定第二个PWM接口

# 初始化
wiringpi.wiringPiSetup()
wiringpi.pinMode(PWM_PIN, PWM_OUTPUT)  #初始化第二个PWM接口 18  

wiringpi.pwmSetMode(wiringpi.PWM_MODE_MS)  
#设置PWM工作模式 有两个值PWM_MODE_MS PWM_MODE_BAL
#一般选第一个  PWM_MODE_BAL or PWM_MODE_MS(平衡模式/占空比模式)

wiringpi.pwmSetRange(1024)  


#设置总周期默认1024 所以一个周期是 600khz/1024 约等于600hz

wiringpi.pwmSetClock(32)  
#设置分频 默认32 即600khz 总时钟频率是19.2Mhz 

wiringpi.pwmWrite(PWM_PIN,0) #设置占空比 0~1024  512 为一半  
#0的话相当于写入全部低电平 没有波形输出

while 1:
    for bright in range(0, 1000):
        wiringpi.pwmWrite(PWM_PIN, bright)
        wiringpi.delay(1)
    for bright in range(1000, 0, -1):
        wiringpi.pwmWrite(PWM_PIN, bright)
        wiringpi.delay(1)
Categories
android

orangepi PWM

打开pwm15

echo 0 > /sys/class/pwm/pwmchip2/export

设置频率1khz

echo 1000000 > /sys/class/pwm/pwmchip2/pwm0/period                                                                         

设置占空比 10%

echo 100000 > /sys/class/pwm/pwmchip2/pwm0/duty_cycle                                                                      

开始pwm

echo 1 > /sys/class/pwm/pwmchip2/pwm0/enable                                                  

关闭pwm

echo 0 > /sys/class/pwm/pwmchip2/pwm0/enable    
echo 0 > /sys/class/pwm/pwmchip2/unexport