MicroPythonLib

Quick reference for the openmvcam

引脚图-OpenMV Cam

OpenMV Cam pinout

基本控制-General board control

See pyb.

import pyb

pyb.delay(50)     # wait 50 milliseconds
               # 等待50ms
pyb.millis()     # number of milliseconds since bootup
                 # boot(开机)之后, 过去的秒数 
pyb.repl_uart(pyb.UART(3, 9600)) # duplicate REPL on UART(3)
                 # 将交互式环境(read–eval–print loop - REPL)
                 # 绑定在串口UART3 上
pyb.wfi()         # pause CPU, waiting for interrupt
                # 暂停CPU, 等待中断
pyb.stop()         # stop CPU, waiting for external interrupt
                # 停止CPU, 等待外部中断

LEDs

See pyb.LED.

from pyb import LED

led = LED(1)     # 构造一个led对象, 1是其编号, 代表红色
led.toggle()     # 关闭/开启led
led.on()         # led开启
led.off()         # led关闭

LED 映射表

LEDs 含义
LED(1) 红色 LED
LED(2) 绿色 LED
LED(3) 蓝色 LED
LED(4) 红外LED (补光LED)

云江科技研发的OpenMV3升级版, OpenMV3 R2 将IR LED 改为白色LED, 可以用于补光

引脚-Pins and GPIO

See pyb.Pin.

from pyb import Pin

p_out = Pin('P7', Pin.OUT_PP) # 引脚设定为输出模式
p_out.high() #引脚输出高电平
p_out.low()  #引脚输出低电平

p_in = Pin('P7', Pin.IN, Pin.PULL_UP) # 引脚设置为上拉输入
p_in.value() # 读入数据, 0 or 1

Here’s the GPIO Pinout:

参照引脚图.

OpenMV Cam pinout

  • Pin(‘P0’) -> P0 (PB15) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P1’) -> P1 (PB14) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P2’) -> P2 (PB13) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P3’) -> P3 (PB12) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P4’) -> P4 (PB10) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P5’) -> P5 (PB11) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P6’) -> P6 (PA5) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P7’) -> P7 (PD12) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P8’) -> P8 (PD13) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.
  • Pin(‘P9’) -> P9 (PD14) - 5V Tolerant w/ 3.3V output at up to 25 mA drive.

Do not draw more than 120 mA in total across all I/O pins.

不要驱动所有引脚电流总额大于120mA.

舵机控制-Servo control

See pyb.Servo.

from pyb import Servo

s1 = Servo(1) # servo on position 1 (P7)
              # 创建一个Servo(舵机)对象
s1.angle(45) # move to 45 degrees
             # 移动到45度角
s1.angle(-60, 1500) # move to -60 degrees in 1500ms
             # 从当前角度(45度角) 在1500ms内移动到-60度角的位置
s1.speed(50) # for continuous rotation servos
             # 以速度为50的速度持续

对应Servo对象的引脚映射:

  • Servo(1) -> P7 (PD12)
  • Servo(2) -> P8 (PD13)

下列引脚只在OpenMV3中

  • Servo(3) -> P9 (PD14)

外部中断-External interrupts

See pyb.ExtInt.

from pyb import Pin, ExtInt

'''
python的lambada表达式 ,等同于
def callback(e):
  print("intr")
'''
callback = lambda e: print("intr")
'''
    Pin('P7') : 绑定P7引脚
    ExtInt.IRQ_RISING : 上升沿触发
    Pin.PULL_NONE: 引脚模式为PULL_NONE 无上拉/下拉电阻
    callback: 目标回调函数(callback中不能申请任何内存, 也就是不能声明新的变量)
'''
ext = ExtInt(Pin('P7'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)

GPIO引脚映射

  • Pin(‘P0’) -> P0 (PB15)
  • Pin(‘P1’) -> P1 (PB14)
  • Pin(‘P2’) -> P2 (PB13)
  • Pin(‘P3’) -> P3 (PB12)
  • Pin(‘P4’) -> P4 (PB10)
  • Pin(‘P5’) -> P5 (PB11)
  • Pin(‘P6’) -> P6 (PA5)
  • Pin(‘P7’) -> P7 (PD12)
  • Pin(‘P8’) -> P8 (PD13)
  • Pin(‘P9’) -> P9 (PD14)

定时器-Timers

See pyb.Timer.

from pyb import Timer
# 使用Timer4, 频率为1000HZ
tim = Timer(4, freq=100)
tim.counter() # 获取计数器的值
tim.counter(100) #设定计数器的值
tim.freq(0.5) # 设定频率0.5 Hz
tim.callback(lambda t: pyb.LED(1).toggle()) #设定Timer的回调函数
# 注意: 回调参数t有特殊含义, 不可变更, 即便函数体用不到.

Timer引脚映射

  • Timer 1 Channel 3 Negative -> P0
  • Timer 1 Channel 2 Negative -> P1
  • Timer 1 Channel 1 Negative -> P2
  • Timer 2 Channel 3 Positive -> P4
  • Timer 2 Channel 4 Positive -> P5
  • Timer 2 Channel 1 Positive -> P6
  • Timer 4 Channel 1 Negative -> P7
  • Timer 4 Channel 2 Negative -> P8
  • Timer 4 Channel 3 Positive -> P9

脉宽调制-PWM (pulse width modulation)

See pyb.Pin and pyb.Timer.

from pyb import Pin, Timer

p = Pin('P7') # P7 has TIM4, CH1
tim = Timer(4, freq=1000)

# 创建一个channel 设定为Timer.PWM模式
ch = tim.channel(1, Timer.PWM, pin=p)
# 设定占空比为50%
ch.pulse_width_percent(50)

Here’s the Timer Pinout:

  • Timer 1 Channel 3 Negative -> P0
  • Timer 1 Channel 2 Negative -> P1
  • Timer 1 Channel 1 Negative -> P2
  • Timer 2 Channel 3 Positive -> P4
  • Timer 2 Channel 4 Positive -> P5
  • Timer 2 Channel 1 Positive -> P6
  • Timer 4 Channel 1 Negative -> P7
  • Timer 4 Channel 2 Negative -> P8
  • Timer 4 Channel 3 Positive -> P9

模拟转数字-ADC (analog to digital conversion)

See pyb.Pin and pyb.ADC.

from pyb import Pin, ADC

adc = ADC('P6') #P6采样口
adc.read()     # 读取数值 范围0-4095
            # read value, 0-4095

Here’s the ADC Pinout:

注意电压值不要超过3.3V

  • ADC(‘P6’) -> P6 (PA5) - ONLY 3.3V (NOT 5V) TOLERANT IN THIS MODE!

数字转模拟-DAC (digital to analog conversion)

See pyb.Pin and pyb.DAC.

from pyb import Pin, DAC

dac = DAC('P6')
dac.write(120)     # 写入120的值
                # output between 0 and 255

Here’s the ADC Pinout:

注意电压值不要超过3.3V

  • DAC(‘P6’) -> P6 (PA5) - ONLY 3.3V (NOT 5V) TOLERANT IN THIS MODE!

串口-UART (serial bus)

See pyb.UART.

from pyb import UART

uart = UART(3, 9600) # 3号串口,9600波特率
uart.write('hello')  # 写入信息: hello
uart.read(5)          # 读入5个字节(bytes)

串口引脚映射

UART 1

  • UART 1 RX -> P0 (PB15)
  • UART 1 TX -> P1 (PB14)

UART 3

  • UART 3 RX -> P5 (PB11)
  • UART 3 TX -> P4 (PB10)

SPI总线-SPI bus

See pyb.SPI.

from pyb import SPI

spi = SPI(2, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
spi.send('hello')
spi.recv(5) # receive 5 bytes on the bus
spi.send_recv('hello') # send a receive 5 bytes

SPI引脚映射

  • SPI 2 MOSI (Master-Out-Slave-In) -> P0 (PB15)
  • SPI 2 MISO (Master-In-Slave-Out) -> P1 (PB14)
  • SPI 2 SCLK (Serial Clock) -> P2 (PB13)
  • SPI 2 SS (Serial Select) -> P3 (PB12)

I2C总线-I2C bus

See pyb.I2C.

from pyb import I2C

i2c = I2C(2, I2C.MASTER, baudrate=100000)
i2c.scan() # returns list of slave addresses
i2c.send('hello', 0x42) # send 5 bytes to slave with address 0x42
i2c.recv(5, 0x42) # receive 5 bytes from slave
i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0x42, slave memory 0x10
i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slave 0x42, slave memory 0x10

I2C 引脚映射:

I2C 2

  • I2C 2 SCL (Serial Clock) -> P4 (PB10)
  • I2C 2 SDA (Serial Data) -> P5 (PB11)

I2C 4

  • I2C 4 SCL (Serial Clock) -> P7 (PD13)
  • I2C 4 SDA (Serial Data) -> P8 (PD12)
Copyright 杭州云江科技有限公司 2017 all right reserved,powered by Gitbook该文件修订时间: 2018-04-02 09:53:12

results matching ""

    No results matching ""