闪电 发表于 2021-11-19 09:05:20

RP2 快速参考

RP2 快速参考http://micropython.circuitpython.com.cn/en/latet/_images/pico_pinout.pngRaspberry Pi Pico 开发板(图片来源:Raspberry Pi Foundation)。以下是 Raspberry Pi RP2xxx 板的快速参考。如果这是您第一次使用该板,了解微控制器的概述可能会很有用:
[*]关于 RP2xxx 端口的一般信息
[*]在 RP2xxx 上开始使用 MicroPython

安装 MicroPython请参阅教程的相应部分:RP2xxx 上的 MicroPython 入门。它还包括故障排除小节。
通用板控制MicroPython REPL 通过 USB 串行端口访问。制表符完成对于找出对象具有哪些方法很有用。粘贴模式 (ctrl-E) 可用于将大量 Python 代码粘贴到 REPL 中。该 machine 模块:import machinemachine.freq()          # get the current frequency of the CPUmachine.freq(240000000) # set the CPU frequency to 240 MHz

该 rp2 模块:import rp2


延迟和计时使用 time模块:import timetime.sleep(1)         # sleep for 1 secondtime.sleep_ms(500)      # sleep for 500 millisecondstime.sleep_us(10)       # sleep for 10 microsecondsstart = time.ticks_ms() # get millisecond counterdelta = time.ticks_diff(time.ticks_ms(), start) # compute time difference


计时器RP2040 的系统定时器外设提供全局微秒时基并为其产生中断。软件定时器目前可用,数量不限(内存允许)。不需要指定计时器 id(目前支持 id=-1),因为它会默认为这个。使用 machine.Timer类:from machine import Timertim = 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 Pinp0 = Pin(0, Pin.OUT)    # create output pin on GPIO0p0.on()               # set pin to "on" (high) levelp0.off()                # set pin to "off" (low) levelp0.value(1)             # set pin to on/highp2 = Pin(2, Pin.IN)   # create input pin on GPIO2print(p2.value())       # get value, 0 or 1p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistorp5 = Pin(5, Pin.OUT, value=1) # set pin high on creation


UART(串行总线)有两个 UART,UART0 和 UART1。UART0 可以映射到 GPIO 0/1、12/13 和 16/17,UART1 可以映射到 GPIO 4/5 和 8/9。参见 machine.UART。from machine import UART, Pinuart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))uart1.write('hello')# write 5 bytesuart1.read(5)         # read up to 5 bytes

笔记ote认情况下禁用通过 UART 的 REPL。您可以在RP2xxx 上查看MicroPython 入门以了解有关如何通过 UART 启用 REPL 的详细信息。

PWM(脉宽调制)有 8 个独立通道,每个通道有 2 个输出,总共 16 个 PWM 通道,时钟频率可以从 7Hz 到 125Mhz。使用 machine.PWM类:from machine import Pin, PWMpwm0 = PWM(Pin(0))      # create PWM object from a pinpwm0.freq()             # get current frequencypwm0.freq(1000)         # set frequencypwm0.duty_u16()         # get current duty cycle, range 0-65535pwm0.duty_u16(200)      # set duty cycle, range 0-65535pwm0.deinit()         # turn off PWM on the pin


ADC(模数转换)RP2040 共有五个 ADC 通道,其中四个是基于 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, Pinadc = ADC(Pin(26))   # create ADC object on ADC pinadc.read_u16()         # read value, 0-65535 across voltage range 0.0v - 3.3v


软件SPI总线软件 SPI(使用 bit-banging)适用于所有引脚,并通过 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 secondspi = SoftSPI(baudrate=100_000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))spi.init(baudrate=200000) # set the baudratespi.read(10)            # read 10 bytes on MISOspi.read(10, 0xff)      # read 10 bytes while outputting 0xff on MOSIbuf = bytearray(50)   # create a bufferspi.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 MOSIspi.write(b'12345')   # write 5 bytes on MOSIbuf = bytearray(4)      # create a bufferspi.write_readinto(b'1234', buf) # write to MOSI and read from MISO into the bufferspi.write_readinto(buf, buf) # write buf to MOSI and read MISO back into buf

警告目前所有的sck, mosi 而且 miso必须初始化软件SPI时指定。

硬件SPI总线RP2040 有 2 条硬件 SPI 总线,可通过 machine.SPI c类访问, 方法与上述软件 SPI 相同:from machine import Pin, SPIspi = 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(使用 bit-banging)适用于所有具有输出功能的引脚,并通过machine.SoftI2C类访问:from machine import Pin, SoftI2Ci2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100_000)i2c.scan()            # scan for devicesi2c.readfrom(0x3a, 4)   # read 4 bytes from device with address 0x3ai2c.writeto(0x3a, '12') # write '12' to device with address 0x3abuf = bytearray(10)   # create a buffer with 10 bytesi2c.writeto(0x3a, buf)# write the given buffer to the peripheral


硬件 I2C 总线驱动程序通过 machine.I2C类访问,并具有与上述软件 I2C 相同的方法:from machine import Pin, I2Ci2c = I2C(0)   # default assignment: scl=Pin(9), sda=Pin(8)i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=400_000)


实时时钟 (RTC)见机器.machine.RTCfrom machine import RTCrtc = RTC()rtc.datetime((2017, 8, 23, 2, 12, 48, 0, 0)) # set a specific date and                                             # time, eg. 2017/8/23 1:12:48rtc.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()


单线驱动OneWire 驱动程序在软件中实现并适用于所有引脚:from machine import Pinimport onewireow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12ow.scan()               # return a list of devices on the busow.reset()            # reset the busow.readbyte()         # read a byteow.writebyte(0x12)      # write a byte on the busow.write('123')         # write bytes on the busow.select_rom(b'12345678') # select a specific device by its ROM code

DS18S20 和 DS18B20 设备有一个特定的驱动程序:import time, ds18x20ds = 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 Pinfrom neopixel import NeoPixelpin = Pin(0, Pin.OUT)   # set GPIO0 to output to drive NeoPixelsnp = NeoPixel(pin, 8)   # create NeoPixel driver on GPIO0 for 8 pixelsnp[0] = (255, 255, 255) # set the first pixel to whitenp.write()            # write data to all pixelsr, g, b = np[0]         # get first pixel colour

APA106 驱动程序扩展了 NeoPixel,但在内部使用了不同的颜色顺序:from apa106 import APA106ap = APA106(pin, 8)r, g, b = ap[0]

APA102 (DotStar) 使用不同的驱动器,因为它有一个额外的时钟引脚。
页: [1]
查看完整版本: RP2 快速参考