Todo tipo de periféricos de pyb
Visión general
Como un solo chip, controlar el puerto IO, IIC, SPI, CAN, PWM y temporizadores es totalmente manejable. Además, usar el lenguaje Python es una forma sencilla de manejarlos sin tener que preocuparse por los registros.

| Tablas | OpenMV2 M4 | OpenMV3 M7 | OpenMV4 H7 | OpenMV4 H7 Plus | OpenMV RT1062 |
|---|---|---|---|---|---|
| Pin | 9 | 10 | 10 | 10 | 14 |
| ADC | 1 | 1 | 1 | 1 | 1 |
| DAC | 1 | 1 | 1 | 1 | 0 |
| SPI | 1 | 1 | 1 | 1 | 1 |
| I2C | 1 | 2 | 2 | 2 | 1 |
| UART | 1 | 2 | 2 | 2 | 1 |
| Servo | 2 | 3 | 3 | 2 | 4 |
| Bus CAN | 0 | 1 | 1 | 1 | 1 |
| Botón de encendido | 0 | 0 | 0 | 0 | 1 |
| Botones personalizables | 0 | 0 | 0 | 0 | 1 |
| Tolerancia del pin | 5V | 5V | 5V | 5V | 3.3V |
| Nivel del pin | 3.3V | 3.3V | 3.3V | 3.3V | 3.3V |
| IC | STM32F427 | STM32F765 | STM32H743 | STM32H743 | IMXRT1062 |
| RAM | 256KB | 512KB | 1MB | 32MB + 1MB | 32MB + 1MB |
| Flash | 1MB | 2MB | 2MB | 32MB + 2MB | 16MB |
| frecuencia | 180MHz | 216MHZ | 480MHZ | 480MHZ | 600MHZ |
| Elemento fotosensible estándar | OV7725 (30W píxeles) | OV7725 (30W píxeles) | OV7725 (30W píxeles) | OV5640 (5 millones de píxeles) | OV5640 (5 millones de píxeles) |
Nota: Como MicroPython puede ejecutarse en muchas plataformas, al principio el módulo pyb, de pyboard, se basaba en STM32, pero más tarde se añadieron esp8266 y esp32, así como la serie nrf. Sus arquitecturas son diferentes de STM32. Por lo tanto, oficialmente se unificó el módulo machine, que es más universal, y pyb terminará siendo eliminado.
OpenMV4 H7 Plus / OpenMV4 H7 / OpenMV3, cuyo controlador principal es STM32, generalmente usan el módulo pyb.
La OpenMV RT1062 más reciente, de 2024, cuyo controlador principal es IMXRT1062, solo admite el módulo machine, no el módulo pyb. Por favor, use el código correspondiente de machine~
Funciones de uso común
pyb.delay(50) # 延时 50 毫秒
pyb.millis() # 获取从启动开始计时的毫秒数
LED
from pyb import LED
led = LED(1) # 红led
led.toggle()
led.on()#亮
led.off()#灭
LED(1) -> LED rojo\ LED(2) -> LED verde\ LED(3) -> LED azul\ LED(4) -> LED infrarrojo, dos
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import LED
import time
red_led = LED("LED_RED")
green_led = LED("LED_GREEN")
blue_led = LED("LED_BLUE")
blue_led.off()
green_led.off()
red_led.off()
while(1):
green_led.toggle()
red_led.on()
time.sleep_ms(250)
red_led.off()
time.sleep_ms(250)
IO
from pyb import Pin
p_out = Pin('P7', Pin.OUT_PP)#设置p_out为输出引脚
p_out.high()#设置p_out引脚为高
p_out.low()#设置p_out引脚为低
p_in = Pin('P7', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
value = p_in.value() # get value, 0 or 1#读入p_in引脚的值
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import Pin
p_out = Pin("P7", Pin.OUT)#设置p_out为输出引脚
p_out.high()#设置p_out引脚为高
p_out.low()#设置p_out引脚为低
p_in = Pin("P8", Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
value = p_in.value() # get value, 0 or 1#读入p_in引脚的值
Servo
Tutorial en video 34 - OpenMV controla tres servos: https://singtown.com/learn/50541/
from pyb import Servo
s1 = Servo(1) # servo on position 1 (P7)
s1.angle(45) # move to 45 degrees
s1.angle(-60, 1500) # move to -60 degrees in 1500ms
s1.speed(50) # for continuous rotation servos
- Servo(1) -> P7 (PD12)
- Servo(2) -> P8 (PD13)
Añadido en OpenMV3 M7 / OpenMV4 H7:
- Servo(3) -> P9 (PD14)
Nota: En OpenMV4 H7 Plus, P9 no puede usar PWM
La OpenMV RT1062 tiene cuatro pines PWM: P7 P8 P9 P10. No se puede usar el módulo pyb en OpenMV RT, solo se puede usar el siguiente módulo machine:
# 舵机控制例子
#
# 这个例子展示了如何使用OpenMV来控制舵机
#
# 伺服系统需要 50 Hz PWM,脉冲宽度为 1000us 至 2000us。
import time
from machine import PWM
# P7 和 P8 可以共享相同的 PWM module,它们需要具有相同的频率。
p7 = PWM("P7", freq=50, duty_ns=(2000 * 1000))
p8 = PWM("P8", freq=50, duty_ns=(2000 * 1000))
# P9 和 P10 可以共享相同的 PWM module,它们需要具有相同的频率。
p9 = PWM("P9", freq=50, duty_ns=(2000 * 1000))
p10 = PWM("P10", freq=50, duty_ns=(2000 * 1000))
while True:
for i in range(1000, 2000, 100):
p7.duty_ns(i * 1000)
p8.duty_ns(i * 1000)
p9.duty_ns(i * 1000)
p10.duty_ns(i * 1000)
time.sleep_ms(1000)
for i in range(2000, 1000, -100):
p7.duty_ns(i * 1000)
p8.duty_ns(i * 1000)
p9.duty_ns(i * 1000)
p10.duty_ns(i * 1000)
time.sleep_ms(1000)
Interrupción de IO
from pyb import Pin, ExtInt
callback = lambda e: print("intr")
ext = ExtInt(Pin('P7'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import Pin
import time
pin0 = Pin("P0", Pin.IN, Pin.PULL_UP)
callback = lambda e: print("intr")
pin0.irq(callback, Pin.IRQ_FALLING)
while True:
time.sleep(1)
Timer
from pyb import Timer
tim = Timer(4, freq=1000)
tim.counter() # get counter value
tim.freq(0.5) # 0.5 Hz
tim.callback(lambda t: pyb.LED(1).toggle())
Timer 1 Canal 3 Negativo -> P0\ Timer 1 Canal 2 Negativo -> P1\ Timer 1 Canal 1 Negativo -> P2\ Timer 2 Canal 3 Positivo -> P4\ Timer 2 Canal 4 Positivo -> P5\ Timer 2 Canal 1 Positivo -> P6\ Timer 4 Canal 1 Negativo -> P7\ Timer 4 Canal 2 Negativo -> P8
Añadido en OpenMV M7:\ Timer 4 Canal 3 Positivo -> P9
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import LED
from machine import Timer
blue_led = LED("LED_BLUE")
# 当被调用时,我们将返回timer对象
# 注意:在回调中不允许分配内存的函数
def tick(timer):
blue_led.toggle()
# machine模块目前仅支持-1虚拟定时器。
tim = Timer(-1, freq=1, callback=tick) # 创建一个定时器对象—以1Hz触发
PWM
from pyb import Pin, Timer
p = Pin('P7') # P7 has TIM4, CH1
tim = Timer(4, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import PWM
import time
p7 = PWM("P7", freq=100, duty_u16=32768)
while True:
for i in range(0, 65536, 256):
p7.duty_u16(65535 - i)
time.sleep_ms(10)
p7.duty_u16(32768)
ADC
from pyb import Pin, ADC
adc = ADC('P6')
adc.read() # read value, 0-4095
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import ADC
adc = ADC('P6')
adc.read_u16()
DAC
from pyb import Pin, DAC
dac = DAC('P6')
dac.write(120) # output between 0 and 255
UART
from pyb import UART
uart = UART(3, 9600)
uart.write('hello')
uart.read(5) # read up to 5 bytes
UART 3 RX ->; P5 (PB11)\ UART 3 TX -> P4 (PB10)
Añadido en OpenMV3 M7 / OpenMV4 H7 / OpenMV4 H7 Plus:\ UART 1 RX -> P0 (PB15)\ UART 1 TX -> P1 (PB14)
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import UART
uart = UART(1, 9600)
uart.write('hello')
uart.read(5) # read up to 5 bytes
La OpenMV RT1062 solo tiene el puerto serie 1, correspondiente a los pines P4 P5. UART 1 RX -> P5 (PB11)\ UART 1 TX -> P4 (PB10)
SPI
from pyb import SPI
spi = SPI(2, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
spi.send(b'hello')
spi.recv(5) # receive 5 bytes on the bus
spi.send_recv(b'hello') # send a receive 5 bytes
El módulo pyb no se puede usar en OpenMV RT, solo se puede usar el siguiente módulo machine:
from machine import SPI
spi = SPI(1, baudrate=int(1000000000 / 66), polarity=0, phase=0)
spi.write(b'hello')
spi.read(5) # receive 5 bytes on the bus
txdata = b"12345678"
rxdata = bytearray(len(txdata))
spi.write_readinto(txdata, rxdata) # Simultaneously write and read bytes.
I2C
from machine import I2C, Pin
i2c = I2C(sda=Pin('P5'),scl=Pin('P4'))
i2c.scan()
i2c.writeto(0x42, b'123') # write 3 bytes to slave with 7-bit address 42
i2c.readfrom(0x42, 4) # read 4 bytes from slave with 7-bit address 42
i2c.readfrom_mem(0x42, 8, 3) # read 3 bytes from memory of slave 42,
# starting at memory-address 8 in the slave
i2c.writeto_mem(0x42, 2, b'\x10') # write 1 byte to memory of slave 42
# starting at address 2 in the slave
I2C 2 SCL (Reloj Serie) -> P4 (PB10)\ I2C 2 SDA (Datos Serie) -> P5 (PB11)
Añadido en OpenMV3 M7 / OpenMV4 H7 / OpenMV4 H7 Plus:\ I2C 4 SCL (Reloj Serie) -> P7 (PD13)\ I2C 4 SDA (Datos Serie) -> P8 (PD12)
La librería machine implementa un protocolo I2C emulado por software, por lo que se puede usar cualquier pin, pero se recomienda usar los pines mencionados anteriormente.
En OpenMV RT, puede llamarlo así:
from machine import I2C, Pin
i2c = I2C(1)
i2c.scan()
i2c.writeto(0x42, b'123') # write 3 bytes to slave with 7-bit address 42
i2c.readfrom(0x42, 4) # read 4 bytes from slave with 7-bit address 42
i2c.readfrom_mem(0x42, 8, 3) # read 3 bytes from memory of slave 42,
# starting at memory-address 8 in the slave
i2c.writeto_mem(0x42, 2, b'\x10') # write 1 byte to memory of slave 42
# starting at address 2 in the slave
Botón personalizado del pin Switch SW
# OpenMV Cam 上的用户开关可通过“SW”引脚读取。
# 该引脚通过 RC 电路在硬件中进行去抖。
# 因此,您只需读取引脚状态即可了解开关是否被按下。
import machine
sw = machine.Pin("SW", machine.Pin.IN)
r = machine.LED("LED_RED")
while True:
r.value(not sw.value())
print(sw.value())
Solo la OpenMV RT tiene botones personalizados SW; OpenMV4 H7 / OpenMV4 H7 Plus / OpenMV3 M7 no tienen botones personalizados.