def initPWM(pin): """ Sets a pin to PWM """ pwm = PWM(pin, 0) pwm.frequency = 1e3 pwm.duty_cycle = 0 pwm.enable() return pwm
def __init__(self, pin): # Check if the pin is valid super(HPWMPeriphery, self).__init__(pin) if PWM is None: raise ImportError("Failed to import PWM from periphery.") self._pwm = PWM(self._chip, self._SELECTED_COMB.index(pin))
def prepare(self) -> None: logger = get_logger(0) for (pin, initial) in self.__channels.items(): try: logger.info("Probing pwm chip %d channel %d ...", self.__chip, pin) pwm = PWM(self.__chip, pin) self.__pwms[pin] = pwm pwm.period_ns = self.__period pwm.duty_cycle_ns = self.__get_duty_cycle(bool(initial)) pwm.enable() except Exception as err: logger.error("Can't get PWM chip %d channel %d: %s", self.__chip, pin, tools.efmt(err))
def __init__(self, pin, freq=None, range=None, threshold=0.01, debug=False): """ 親クラスのコンストラクタ処理後、指定のピンに対しPWM出力ピンとして設定を行う。 初期値としてPWMサイクル値をゼロに指定する。 引数: pin int PWM番号、必須(1~3) freq int PWM Frequency値(Hz) range int PWMサイクル値の範囲(25から40,000までの整数) threshold float 入力値を0として認識するしきい値(-threshold < value< threshold => 0) """ super().__init__(pin, mode=OUT, debug=debug) self.freq = freq or DEFAULT_FREQ self.threshold = threshold if self.pin == 1: self.chip_number = 0 self.channel_number = 0 elif self.pin == 2: self.chip_number = 1 self.channel_number = 0 elif self.pin == 3: self.chip_number = 2 self.channel_number = 0 else: raise ValueError('[PERIPHERY_PWM] pwm:{} is out of range(1.. 3)'.format(str(self.pin))) try: from periphery import PWM except ImportError: exit('[PERIPHERY_PWM] This code requires periphery package') self.gpio = PWM(self.chip_number, self.channel_number) self.gpio.frequency = self.freq self.gpio.duty_cycle = 0 self.gpio.enable() self.run(0) if self.debug: print('[PERIPHERY_PWM] pwm:{} set freq:{}/dutycycle:{}, enabled and set value 0'.format( str(self.pin), str(self.freq), str(self.gpio.duty_cycle)))
def initPWM(pin): pwm = PWM(pin, 0) pwm.frequency = 1e3 pwm.duty_cycle = 0 pwm.enable() return pwm
# set GPIO mode to BOARD GPIO.setmode(GPIO.BOARD) mode = GPIO.getmode() print(mode) # Open PWM channel 0, pin 32 pins = range(0, 41) """ for pin in pins: try: pwm = PWM(0, pin) print("** Channel 0 pin {} works!".format(pin)) except: print("Pin {} does not work".format(pin)) """ pwm = PWM(0, 2) pwm = PWM(0, 0) #Channel 0 pin 2 # Set frequency to 1 kHz pwm.frequency = 2e3 # Set duty cycle to 75% pwm.duty_cycle = 0.25 pwm.enable() # Change duty cycle to 50% pwm.duty_cycle = 0.25 time.sleep(5 * 60)
"""periphery库 PWM测试.""" import time from periphery import PWM # 打开 PWM 3, channel 0 ,对应开发板上PWM3外设 try: pwm = PWM(2, 0) # 设置PWM输出频率为 1 kHz pwm.frequency = 1e3 # 设置占空比为 50% pwm.duty_cycle = 0.50 # 开启PWM输出 pwm.enable() while True: for i in range(0, 9): time.sleep(0.1) pwm.duty_cycle += 0.05 for i in range(0, 9): time.sleep(0.1) pwm.duty_cycle -= 0.05 if pwm.duty_cycle == 0.0: time.sleep(1) finally: pwm.close()
GPIO.setmode(GPIO.BOARD) mode = GPIO.getmode() print(mode) # Open PWM channel 0, pin 32 pins = range(0, 41) """ for pin in pins: try: pwm = PWM(0, pin) print("** Channel 0 pin {} works!".format(pin)) except: print("Pin {} does not work".format(pin)) """ pwm = PWM(0, 2) #Channel 0 pin 2 # Set frequency to 1 kHz pwm.frequency = 1e3 # Set duty cycle to 75% pwm.duty_cycle = 0.5 pwm.enable() # Change duty cycle to 50% pwm.duty_cycle = 0.50 time.sleep(5 * 60)