请选择 进入手机版 | 继续访问电脑版

Discuz! Board

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1357|回复: 0

RP2 快速参考

[复制链接]

13

主题

14

帖子

631

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
631
发表于 2022-1-20 09:09:23 | 显示全部楼层 |阅读模式
RP2 快速参考

Raspberry Pi Pico 开发板(图片来源:Raspberry Pi Foundation)。

以下是 Raspberry Pi RP2xxx 板的快速参考。如果这是您第一次使用该板,了解微控制器的概述可能会很有用:


安装 MicroPython

请参阅教程的相应部分:RP2xxx 上的 MicroPython 入门。它还包括故障排除小节。


通用板控制

MicroPython REPL 通过 USB 串行端口访问。制表符完成对于找出对象具有哪些方法很有用。粘贴模式 (ctrl-E) 可用于将大量 Python 代码粘贴到 REPL 中。

machine 模块:

  1. import machine

  2. machine.freq()          # get the current frequency of the CPU
  3. machine.freq(240000000) # set the CPU frequency to 240 MHz
复制代码



rp2 模块:

import rp2


延迟和计时

使用 time模块:

  1. import time

  2. time.sleep(1)           # sleep for 1 second
  3. time.sleep_ms(500)      # sleep for 500 milliseconds
  4. time.sleep_us(10)       # sleep for 10 microseconds
  5. start = time.ticks_ms() # get millisecond counter
  6. delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
复制代码




计时器

RP2040 的系统定时器外设提供全局微秒时基并为其产生中断。软件定时器目前可用,数量不限(内存允许)。不需要指定计时器 id(目前支持 id=-1),因为它会默认为这个。

使用 machine.Timer类:

  1. from machine import Timer

  2. tim = Timer(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
  3. tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t:print(2))
复制代码




引脚和 GPIO

使用 machine.Pin类:

  1. from machine import Pin

  2. p0 = Pin(0, Pin.OUT)    # create output pin on GPIO0
  3. p0.on()                 # set pin to "on" (high) level
  4. p0.off()                # set pin to "off" (low) level
  5. p0.value(1)             # set pin to on/high

  6. p2 = Pin(2, Pin.IN)     # create input pin on GPIO2
  7. print(p2.value())       # get value, 0 or 1

  8. p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
  9. p5 = 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

  1. from machine import UART, Pin
  2. uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
  3. uart1.write('hello')  # write 5 bytes
  4. uart1.read(5)         # read up to 5 bytes
复制代码



笔记ote

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



PWM(脉宽调制)

有 8 个独立通道,每个通道有 2 个输出,总共 16 个 PWM 通道,时钟频率可以从 7Hz 到 125Mhz。

使用 machine.PWM类:

  1. from machine import Pin, PWM

  2. pwm0 = PWM(Pin(0))      # create PWM object from a pin
  3. pwm0.freq()             # get current frequency
  4. pwm0.freq(1000)         # set frequency
  5. pwm0.duty_u16()         # get current duty cycle, range 0-65535
  6. pwm0.duty_u16(200)      # set duty cycle, range 0-65535
  7. pwm0.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类:

  1. from machine import ADC, Pin
  2. adc = ADC(Pin(26))     # create ADC object on ADC pin
  3. adc.read_u16()         # read value, 0-65535 across voltage range 0.0v - 3.3v
复制代码




软件SPI总线

软件 SPI(使用 bit-banging)适用于所有引脚,并通过 machine.SoftSPI 类访问 :

  1. from machine import Pin, SoftSPI

  2. # construct a SoftSPI bus on the given pins
  3. # polarity is the idle state of SCK
  4. # phase=0 means sample on the first edge of SCK, phase=1 means the second
  5. spi = SoftSPI(baudrate=100_000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))

  6. spi.init(baudrate=200000) # set the baudrate

  7. spi.read(10)            # read 10 bytes on MISO
  8. spi.read(10, 0xff)      # read 10 bytes while outputting 0xff on MOSI

  9. buf = bytearray(50)     # create a buffer
  10. spi.readinto(buf)       # read into the given buffer (reads 50 bytes in this case)
  11. spi.readinto(buf, 0xff) # read into the given buffer and output 0xff on MOSI

  12. spi.write(b'12345')     # write 5 bytes on MOSI

  13. buf = bytearray(4)      # create a buffer
  14. spi.write_readinto(b'1234', buf) # write to MOSI and read from MISO into the buffer
  15. spi.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 相同:

  1. from machine import Pin, SPI

  2. spi = SPI(1, 10_000_000)  # Default assignment: sck=Pin(10), mosi=Pin(11), miso=Pin(8)
  3. spi = SPI(1, 10_000_000, sck=Pin(14), mosi=Pin(15), miso=Pin(12))
  4. 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类访问:

  1. from machine import Pin, SoftI2C

  2. i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100_000)

  3. i2c.scan()              # scan for devices

  4. i2c.readfrom(0x3a, 4)   # read 4 bytes from device with address 0x3a
  5. i2c.writeto(0x3a, '12') # write '12' to device with address 0x3a

  6. buf = bytearray(10)     # create a buffer with 10 bytes
  7. i2c.writeto(0x3a, buf)  # write the given buffer to the peripheral
复制代码




硬件 I2C 总线

驱动程序通过 machine.I2C类访问,并具有与上述软件 I2C 相同的方法:

  1. from machine import Pin, I2C

  2. i2c = I2C(0)   # default assignment: scl=Pin(9), sda=Pin(8)
  3. i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=400_000)
复制代码




实时时钟 (RTC)

见机器.machine.RTC

  1. from machine import RTC

  2. rtc = RTC()
  3. rtc.datetime((2017, 8, 23, 2, 12, 48, 0, 0)) # set a specific date and
  4.                                              # time, eg. 2017/8/23 1:12:48
  5. rtc.datetime() # get date and time
复制代码




WDT(看门狗定时器)

RP2040 有一个看门狗,它是一个倒数计时器,可以在它达到零时重新启动芯片的某些部分。

参见 machine.WDT.

  1. from machine import WDT

  2. # enable the WDT with a timeout of 5s (1s is the minimum)
  3. wdt = WDT(timeout=5000)
  4. wdt.feed()
复制代码




单线驱动

OneWire 驱动程序在软件中实现并适用于所有引脚:

  1. from machine import Pin
  2. import onewire

  3. ow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12
  4. ow.scan()               # return a list of devices on the bus
  5. ow.reset()              # reset the bus
  6. ow.readbyte()           # read a byte
  7. ow.writebyte(0x12)      # write a byte on the bus
  8. ow.write('123')         # write bytes on the bus
  9. ow.select_rom(b'12345678') # select a specific device by its ROM code
复制代码



DS18S20 和 DS18B20 设备有一个特定的驱动程序:

  1. import time, ds18x20
  2. ds = ds18x20.DS18X20(ow)
  3. roms = ds.scan()
  4. ds.convert_temp()
  5. time.sleep_ms(750)
  6. for rom in roms:
  7.     print(ds.read_temp(rom))
复制代码



一定要在数据线上放一个4.7k的上拉电阻。请注意,convert_temp()每次要对温度进行采样时都必须调用该方法。


NeoPixel 和 APA106 驱动程序

使用 neopixel和 apa106模块:

  1. from machine import Pin
  2. from neopixel import NeoPixel

  3. pin = Pin(0, Pin.OUT)   # set GPIO0 to output to drive NeoPixels
  4. np = NeoPixel(pin, 8)   # create NeoPixel driver on GPIO0 for 8 pixels
  5. np[0] = (255, 255, 255) # set the first pixel to white
  6. np.write()              # write data to all pixels
  7. r, g, b = np[0]         # get first pixel colour
复制代码



APA106 驱动程序扩展了 NeoPixel,但在内部使用了不同的颜色顺序:

  1. from apa106 import APA106
  2. ap = APA106(pin, 8)
  3. r, g, b = ap[0]
复制代码



APA102 (DotStar) 使用不同的驱动器,因为它有一个额外的时钟引脚。



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|MicroPython中文社区 ( 粤ICP备15040352号 )microPython技术交流

粤公网安备 44030702004355号

GMT+8, 2024-4-18 21:06 , Processed in 0.109200 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表