pwmio – 支持基于 PWM 的协议

pwmio 模块包含提供对基本脉冲 IO 的访问的类。

如果程序在使用后继续,所有类都会更改硬件状态,并且在不再需要它们时应取消初始化。为此,请调用deinit() 或使用上下文管理器。有关更多信息,请参阅 Lifetime 和 ContextManagers

例如:

import pwmio
import time
from board import *

pwm = pwmio.PWMOut(D13)
pwm.duty_cycle = 2 ** 15
time.sleep(0.1)

此示例将初始化设备,设置 duty_cycle,然后休眠 0.1 秒。程序完成后,CircuitPython 将在重置所有硬件时自动关闭 PWM。使用deinit()with语句自行完成。

在这些板上可用
  • 8086 Commander
  • @sarfata shIRtty
  • ARAMCON Badge 2019
  • ARAMCON2 Badge
  • ATMegaZero ESP32-S2
  • Adafruit CLUE nRF52840 Express
  • Adafruit Circuit Playground Bluefruit
  • Adafruit Circuit Playground Express 4-H
  • Adafruit CircuitPlayground Express
  • Adafruit CircuitPlayground Express with Crickit libraries
  • Adafruit CircuitPlayground Express with displayio
  • Adafruit EdgeBadge
  • Adafruit Feather Bluefruit Sense
  • Adafruit Feather M0 Adalogger
  • Adafruit Feather M0 Basic
  • Adafruit Feather M0 Express
  • Adafruit Feather M0 Express with Crickit libraries
  • Adafruit Feather M0 RFM69
  • Adafruit Feather M0 RFM9x
  • Adafruit Feather M4 CAN
  • Adafruit Feather M4 Express
  • Adafruit Feather MIMXRT1011
  • Adafruit Feather RP2040
  • Adafruit Feather STM32F405 Express
  • Adafruit Feather nRF52840 Express
  • Adafruit FunHouse
  • Adafruit Gemma M0
  • Adafruit Gemma M0 PyCon 2018
  • Adafruit Grand Central M4 Express
  • Adafruit Hallowing M4 Express
  • Adafruit ItsyBitsy M0 Express
  • Adafruit ItsyBitsy M4 Express
  • Adafruit ItsyBitsy RP2040
  • Adafruit ItsyBitsy nRF52840 Express
  • Adafruit LED Glasses Driver nRF52840
  • Adafruit Macropad RP2040
  • Adafruit MagTag
  • Adafruit Matrix Portal M4
  • Adafruit Metro ESP32S2
  • Adafruit Metro M0 Express
  • Adafruit Metro M4 Airlift Lite
  • Adafruit Metro M4 Express
  • Adafruit Metro nRF52840 Express
  • Adafruit Monster M4SK
  • Adafruit PyGamer
  • Adafruit PyPortal
  • Adafruit PyPortal Pynt
  • Adafruit PyPortal Titano
  • Adafruit PyRuler
  • Adafruit Pybadge
  • Adafruit QT Py M0
  • Adafruit QT Py M0 Haxpress
  • Adafruit QT Py RP2040
  • Adafruit QT2040 Trinkey
  • Adafruit Trellis M4 Express
  • Adafruit Trinket M0
  • AloriumTech Evo M51
  • Arduino MKR Zero
  • Arduino MKR1300
  • Arduino Nano 33 BLE
  • Arduino Nano 33 IoT
  • Arduino Nano RP2040 Connect
  • Arduino Zero
  • Artisense Reference Design RD00
  • AtelierDuMaker nRF52840 Breakout
  • BDMICRO VINA-D21
  • BDMICRO VINA-D51
  • BLE-SS dev board Multi Sensor
  • BastBLE
  • BastWiFi
  • BlueMicro840
  • CP Sapling M0
  • CP Sapling M0 w/ SPI Flash
  • CP32-M4
  • Capable Robot Programmable USB Hub
  • Cedar Grove StringCar M0 Express
  • Challenger RP2040 WiFi
  • Circuit Playground Express Digi-Key PyCon 2019
  • CircuitBrains Basic
  • CircuitBrains Deluxe
  • CrumpS2
  • Cytron Maker Pi RP2040
  • DynOSSAT-EDU-EPS
  • DynOSSAT-EDU-OBC
  • DynaLoRa_USB
  • ESP 12k NodeMCU
  • Electronic Cats Bast Pro Mini M0
  • Electronic Cats CatWAN USBStick
  • Electronic Cats Hunter Cat NFC
  • Electronic Cats NFC Copy Cat
  • Electronut Labs Blip
  • Electronut Labs Papyr
  • EncoderPad RP2040
  • Escornabot Makech
  • Espruino Pico
  • Espruino Wifi
  • Feather ESP32S2 without PSRAM
  • Feather MIMXRT1011
  • Feather MIMXRT1062
  • FeatherS2
  • FeatherS2 Neo
  • FeatherS2 PreRelease
  • Fluff M0
  • Franzininho WIFI w/Wroom
  • Franzininho WIFI w/Wrover
  • Gravitech Cucumber M
  • Gravitech Cucumber MS
  • Gravitech Cucumber R
  • Gravitech Cucumber RS
  • HMI-DevKit-1.1
  • Hacked Feather M0 Express with 8Mbyte SPI flash
  • HalloWing M0 Express
  • HiiBot BlueFi
  • IMXRT1010-EVK
  • IkigaiSense Vita nRF52840
  • J&J Studios datum-Distance
  • J&J Studios datum-IMU
  • J&J Studios datum-Light
  • J&J Studios datum-Weather
  • Kaluga 1
  • LILYGO TTGO T8 ESP32-S2 w/Display
  • LoC BeR M4 base board
  • MDBT50Q-DB-40
  • MDBT50Q-RX Dongle
  • MEOWBIT
  • MORPHEANS MorphESP-240
  • MakerDiary nRF52840 MDK
  • MakerDiary nRF52840 MDK USB Dongle
  • Makerdiary M60 Keyboard
  • Makerdiary Pitaya Go
  • Makerdiary nRF52840 M.2 Developer Kit
  • Melopero Shake RP2040
  • Metro MIMXRT1011
  • Mini SAM M4
  • NUCLEO STM32F746
  • NUCLEO STM32F767
  • Oak Dev Tech BREAD2040
  • Oak Dev Tech PixelWing ESP32S2
  • Open Hardware Summit 2020 Badge
  • PCA10056 nRF52840-DK
  • PCA10059 nRF52840 Dongle
  • PCA10100 nRF52833 Dongle
  • PYB LR Nano V2
  • Particle Argon
  • Particle Boron
  • Particle Xenon
  • PewPew 10.2
  • PewPew 13
  • PicoPlanet
  • Pimoroni Interstate 75
  • Pimoroni Keybow 2040
  • Pimoroni PGA2040
  • Pimoroni Pico LiPo (16MB)
  • Pimoroni Pico LiPo (4MB)
  • Pimoroni PicoSystem
  • Pimoroni Plasma 2040
  • Pimoroni Tiny 2040
  • PyCubedv04
  • PyCubedv04-MRAM
  • PyCubedv05
  • PyCubedv05-MRAM
  • PyKey60
  • PyboardV1_1
  • Raspberry Pi Pico
  • Robo HAT MM1 M4
  • S2Mini
  • SAM E54 Xplained Pro
  • SAM32v26
  • SPRESENSE
  • ST STM32F746G Discovery
  • STM32F411E_DISCO
  • STM32F412G_DISCO
  • STM32F4_DISCO
  • Saola 1 w/Wroom
  • Saola 1 w/Wrover
  • Seeeduino Wio Terminal
  • Seeeduino XIAO
  • Serpente
  • Silicognition LLC M4-Shim
  • Simmel
  • SparkFun LUMIDrive
  • SparkFun MicroMod RP2040 Processor
  • SparkFun MicroMod SAMD51 Processor
  • SparkFun MicroMod nRF52840 Processor
  • SparkFun Pro Micro RP2040
  • SparkFun Pro nRF52840 Mini
  • SparkFun Qwiic Micro
  • SparkFun RedBoard Turbo
  • SparkFun SAMD21 Dev Breakout
  • SparkFun SAMD21 Mini Breakout
  • SparkFun STM32 MicroMod Processor
  • SparkFun Thing Plus - RP2040
  • SparkFun Thing Plus - SAMD51
  • Sprite_v2b
  • StackRduino M0 PRO
  • TG-Boards' Datalore IP M4
  • TG-Watch
  • THUNDERPACK_v11
  • THUNDERPACK_v12
  • Targett Module Clip w/Wroom
  • Targett Module Clip w/Wrover
  • Teensy 4.0
  • Teensy 4.1
  • Teknikio Bluebird
  • The Open Book Feather
  • TinkeringTech ScoutMakes Azul
  • TinyS2
  • Trinket M0 Haxpress
  • UARTLogger II
  • WarmBit BluePixel nRF52840
  • Winterbloom Big Honking Button
  • Winterbloom Sol
  • XinaBox CC03
  • XinaBox CS11
  • iMX RT 1020 EVK
  • iMX RT 1060 EVK
  • keithp.com snekboard
  • micro:bit v2
  • microS2
  • nanoESP32-S2 w/Wrover
  • nanoESP32-S2 w/Wroom
  • ndGarage[n°] Bit6: FeatherSnow-v2
  • ndGarage[n°]Bit6:FeatherSnow
  • nice!nano
  • senseBox MCU
  • stm32f411ce-blackpill
  • stm32f411ce-blackpill-with-flash
  • uChip
  • uGame10

class pwmio.PWMOut(pin: microcontroller.Pin, *, duty_cycle: int = 0, frequency: int = 500, variable_frequency: bool = False)

在给定引脚上输出脉宽调制信号。

创建与给定引脚关联的 PWM 对象。这允许您在给定的引脚上写出 PWM 信号。除非variable_frequency 为真,否则频率在 init 之后是固定的。

笔记

variable_frequency 为 True 时,进一步的 PWM 输出可能会受到限制,因为它可能需要更多的内部资源才能灵活。因此,在输出固定和灵活频率信号时,首先构建固定输出。

参数
  • pin (Pin) – 要输出到的引脚

  • duty_cycle (int) – 每个脉冲的高分数。16 位

  • frequency (int) – 以赫兹为单位的目标频率(32 位)

  • variable_frequency (bool) – 如果频率随时间变化,则为真

简单的 LED 渐变:

import pwmio
import board

pwm = pwmio.PWMOut(board.D13)     # output on D13
pwm.duty_cycle = 2 ** 15            # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz

特定频率下的 PWM(伺服和电机):

import pwmio
import board

pwm = pwmio.PWMOut(board.D13, frequency=50)
pwm.duty_cycle = 2 ** 15                  # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz

变频(通常是音调):

import pwmio
import board
import time

pwm = pwmio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True)
time.sleep(0.2)
pwm.frequency = 880
time.sleep(0.1)
duty_cycle :int

16 位值,指示一个周期中有多少是高 (1) 与低 (0)。0xffff 将始终为高,0 将始终为低,0x7fff 将半高然后半低。

根据 PWM 在特定板上的实现方式,占空比的内部表示可能具有小于 16 位的分辨率。读取此属性将从内部表示返回值,因此它可能与设置的值不同。

frequency :int

32 位值,指示以赫兹为单位的 PWM 频率(每秒周期数)。仅在使用variable_frequency=True.

根据 PWM 在特定板上的实现方式,当频率发生变化时,可能需要重新计算 PWM 占空比的内部值。在这些情况下,占空比会根据原始占空比值自动重新计算。这应该发生而无需手动重新设置占空比。

deinit(self)None

取消初始化 PWMOut 并释放任何硬件资源以供重用。

__enter__(self)PWMOut

上下文管理器使用的无操作。

__exit__(self)None

退出上下文时自动取消初始化硬件。有关更多信息,请参阅 Lifetime 和 ContextManagers