一般操作
MicroPython REPL通过USB串行端口访问。Tab 补全可用于找出对象具有哪些方法。粘贴模式(ctrl-E)可用于粘贴 Python 代码到 REPL 中。
machine 模块:
import machine machine.freq() # get the current frequency of the CPU machine.freq(240000000) # set the CPU frequency to 240 MHz
rp2 模块:
import rp2
延迟和定时
使用 time 模块:
import time time.sleep(1) # sleep for 1 second time.sleep_ms(500) # sleep for 500 milliseconds time.sleep_us(10) # sleep for 10 microseconds start = time.ticks_ms() # get millisecond counter delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
定时器
RP2040 的系统定时器外设提供全局微秒时基和为其生成中断。软件定时器目前可用,并且有无限的数量(内存允许)。没有必要指定计时器 ID(目前支持 id=-1),因为它将是默认的。
使用 machine.Timer 类:
from machine import Timer tim = Timer(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1)) tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t:print(2))
引脚 和 GPIO
使用 machine.Pin 类:
from machine import Pin p0 = Pin(0, Pin.OUT) # create output pin on GPIO0 p0.on() # set pin to "on" (high) level p0.off() # set pin to "off" (low) level p0.value(1) # set pin to on/high p2 = Pin(2, Pin.IN) # create input pin on GPIO2 print(p2.value()) # get value, 0 or 1 p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor p5 = Pin(5, Pin.OUT, value=1) # set pin high on creation
可编程 IO (PIO)
PIO 对于从头开始构建低级 IO 接口非常有用。使用 PIO 以 1Hz 闪烁 LED 的示例:
from machine import Pin import rp2 @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW) def blink_1hz(): # Cycles: 1 + 7 + 32 * (30 + 1) = 1000 set(pins, 1) set(x, 31) [6] label("delay_high") nop() [29] jmp(x_dec, "delay_high") # Cycles: 1 + 7 + 32 * (30 + 1) = 1000 set(pins, 0) set(x, 31) [6] label("delay_low") nop() [29] jmp(x_dec, "delay_low") # Create and start a StateMachine with blink_1hz, outputting on Pin(25) sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25)) sm.active(1)
UART(串口总线)
有两个串口:UART0 和 UART1。UART0 可以映射到 GPIO 0/1、12/13 和 16/17;UART1 可映射到 GPIO 4/5 和 8/9。
from machine import UART, Pin uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5)) uart1.write('hello') # write 5 bytes uart1.read(5) # read up to 5 bytes
默认情况下,UART 上的 REPL 处于禁用状态。
PWM(脉宽调制)
有 8 个独立的 PWM 发生器,称为切片,每个发生器有两个通道使其总共 16 个 PWM 通道,可以从 8Hz 至 62.5Mhz,machine.freq() 为 125Mhz。一个的两个通道切片以相同的频率运行,但可以具有不同的占空比。
这两个通道通常分配给相邻的 GPIO 引脚对偶数/奇数。所以 GPIO0 和 GPIO1 位于片 0、GPIO2 和 GPIO3 位于切片 1,依此类推。可以将某个通道分配给不同的 GPIO 引脚(请参阅引脚排列)。例如,片 0,可以分配通道 A 到 GPIO0 和 GPIO16。
使用 machine.PWM 类:
from machine import Pin, PWM # create PWM object from a pin and set the frequency of slice 0 # and duty cycle for channel A pwm0 = PWM(Pin(0), freq=2000, duty_u16=32768) pwm0.freq() # get the current frequency of slice 0 pwm0.freq(1000) # set/change the frequency of slice 0 pwm0.duty_u16() # get the current duty cycle of channel A, range 0-65535 pwm0.duty_u16(200) # set the duty cycle of channel A, range 0-65535 pwm0.duty_u16(0) # stop the output at channel A print(pwm0) # show the properties of the PWM object. pwm0.deinit() # turn off PWM of slice 0, stopping channels A and B
ADC(模数转换器)
RP2040 共有 5 个 ADC 通道,其中 4 个基于 12 位 SAR。ADC:GP26、GP27、GP28 和 GP29。ADC0、ADC1、ADC2 和 ADC3 可分别与 GP26、GP27、GP28、GP29 连接(在 Pico 主板上,GP29 已连接到 VSYS)。标准 ADC 范围为 0-3.3V。第五个通道连接到内置温度传感器,可用于测量温度。
使用 machine.ADC 类:
from machine import ADC, Pin adc = ADC(Pin(26)) # create ADC object on ADC pin adc.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v
软件 SPI 总线
软件 SPI 适用于所有引脚,可通过 machine.SoftSPI 类:
from machine import Pin, SoftSPI # construct a SoftSPI bus on the given pins # polarity is the idle state of SCK # phase=0 means sample on the first edge of SCK, phase=1 means the second spi = SoftSPI(baudrate=100_000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4)) spi.init(baudrate=200000) # set the baudrate spi.read(10) # read 10 bytes on MISO spi.read(10, 0xff) # read 10 bytes while outputting 0xff on MOSI buf = bytearray(50) # create a buffer spi.readinto(buf) # read into the given buffer (reads 50 bytes in this case) spi.readinto(buf, 0xff) # read into the given buffer and output 0xff on MOSI spi.write(b'12345') # write 5 bytes on MOSI buf = bytearray(4) # create a buffer spi.write_readinto(b'1234', buf) # write to MOSI and read from MISO into the buffer spi.write_readinto(buf, buf) # write buf to MOSI and read MISO back into buf
目前,初始化软 SPI 时必须指定 sck、mosi 和 miso。
硬件 SPI 总线
RP2040 具有 2 个硬件 SPI 总线,可通过 machine.SPI 类,具有与软件相同的方法:
from machine import Pin, SPI spi = SPI(1, 10_000_000) # Default assignment: sck=Pin(10), mosi=Pin(11), miso=Pin(8) spi = SPI(1, 10_000_000, sck=Pin(14), mosi=Pin(15), miso=Pin(12)) spi = SPI(0, baudrate=80_000_000, polarity=0, phase=0, bits=8, sck=Pin(6), mosi=Pin(7), miso=Pin(4))
软件 I2C 总线
软件 I2C 适用于所有具有输出功能的引脚,可通过 machine.SoftI2C 类:
from machine import Pin, SoftI2C i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100_000) i2c.scan() # scan for devices i2c.readfrom(0x3a, 4) # read 4 bytes from device with address 0x3a i2c.writeto(0x3a, '12') # write '12' to device with address 0x3a buf = bytearray(10) # create a buffer with 10 bytes i2c.writeto(0x3a, buf) # write the given buffer to the peripheral
硬件 I2C 总线
可通过 machine.I2C 访问硬件 I2C 总线。和上述软件 I2C 相同的用法:
from machine import Pin, I2C i2c = I2C(0) # default assignment: scl=Pin(9), sda=Pin(8) i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=400_000)
I2C 总线
参考 machine.I2S 类:
from machine import I2S, Pin i2s = I2S(0, sck=Pin(16), ws=Pin(17), sd=Pin(18), mode=I2S.TX, bits=16, format=I2S.STEREO, rate=44100, ibuf=40000) # create I2S object i2s.write(buf) # write buffer of audio samples to I2S device i2s = I2S(1, sck=Pin(0), ws=Pin(1), sd=Pin(2), mode=I2S.RX, bits=16, format=I2S.MONO, rate=22050, ibuf=40000) # create I2S object i2s.readinto(buf) # fill buffer with audio samples from I2S device
“ws” 引脚号必须比 “sck” 引脚号大 1。I2S 类目前作为技术预览版提供。在预览期间,鼓励用户使用。根据此反馈,可能会更改 I2S 类 API 和实现。支持两条 I2S 总线,id=0 和 id=1。
实时时钟(RTC)
参考 machine.RTC 类:
from machine import RTC rtc = RTC() rtc.datetime((2017, 8, 23, 2, 12, 48, 0, 0)) # set a specific date and # time, eg. 2017/8/23 1:12:48 rtc.datetime() # get date and time
WDT(看门狗定时器)
RP2040 内置了一个看门狗,这是一个可以重新启动的倒数计时器芯片的零件,如果倒计时到零。
使用 machine.WDT 类:
from machine import WDT # enable the WDT with a timeout of 5s (1s is the minimum) wdt = WDT(timeout=5000) wdt.feed()
超时的最大值为 8388 毫秒。
OneWire 驱动
OneWire 驱动程序在软件中实现,适用于所有引脚:
from machine import Pin import onewire ow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12 ow.scan() # return a list of devices on the bus ow.reset() # reset the bus ow.readbyte() # read a byte ow.writebyte(0x12) # write a byte on the bus ow.write('123') # write bytes on the bus ow.select_rom(b'12345678') # select a specific device by its ROM code
有一个适用于 DS18S20 和 DS18B20 设备的特定驱动程序:
import time, ds18x20 ds = ds18x20.DS18X20(ow) roms = ds.scan() ds.convert_temp() time.sleep_ms(750) for rom in roms: print(ds.read_temp(rom))
确保在数据线上放置一个4.7k上拉电阻。 请注意,每次需要时都必须调用 “convert_temp()” 方法。
对温度进行采样。
NeoPixel 和 APA106 驱动
使用 neopixel 和 apa106 模块:
from machine import Pin from neopixel import NeoPixel pin = Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels np = NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels np[0] = (255, 255, 255) # set the first pixel to white np.write() # write data to all pixels r, g, b = np[0] # get first pixel colour
APA106 驱动程序扩展了 NeoPixel,但内部使用不同的颜色顺序:
from apa106 import APA106 ap = APA106(pin, 8) r, g, b = ap[0]
APA102(DotStar)使用不同的驱动程序,因为它有一个额外的时钟引脚。