查看“MicroMV IIC通信”的源代码
←
MicroMV IIC通信
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
<p style="font-size:135%">'''MicroMV上的IIC通讯'''</p> *'''做为IIC主机''' :扫描设备地址 :<source lang="py"> from pyb import I2C i2c = I2C(2, I2C.MASTER) # The i2c bus must always be 2. address=i2c.scan() for addr in address: print ("Hex = 0x%2x" % (addr)) </source> [[File:microMVIIC1.png||600px|center]] *'''做为IIC从机''' :<source lang="py"> # I2C with the Arduino as the master device and the OpenMV Cam as the slave. # # Please wire up your OpenMV Cam to your Arduino like this: # # OpenMV Cam Master I2C Data (P5) - Arduino Uno Data (A4) # OpenMV Cam Master I2C Clock (P4) - Arduino Uno Clock (A5) # OpenMV Cam Ground - Arduino Ground import pyb, ustruct text = "Hello World!\n" data = ustruct.pack("<%ds" % len(text), text) # Use "ustruct" to build data packets to send. # "<" puts the data in the struct in little endian order. # "%ds" puts a string in the data stream. E.g. "13s" for "Hello World!\n" (13 chars). # See https://docs.python.org/3/library/struct.html # READ ME!!! # # Please understand that when your OpenMV Cam is not the I2C master it may miss responding to # sending data as a I2C slave no matter if you call "i2c.send()" in an interupt callback or in the # main loop below. When this happens the Arduino will get a NAK and have to try reading from the # OpenMV Cam again. Note that both the Arduino and OpenMV Cam I2C drivers are not good at getting # unstuck after encountering any I2C errors. On the OpenMV Cam and Arduino you can recover by # de-initing and then re-initing the I2C peripherals. # The hardware I2C bus for your OpenMV Cam is always I2C bus 2. bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # Fully reset I2C device... bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # Send the len first (16-bits). try: bus.send(data, timeout=10000) # Send the data second. print("Sent Data!") # Only reached on no error. except OSError as err: pass # Don't care about errors - so pass. # Note that there are 3 possible errors. A timeout error, a general purpose error, or # a busy error. The error codes are 116, 5, 16 respectively for "err.arg[0]". except OSError as err: pass # Don't care about errors - so pass. # Note that there are 3 possible errors. A timeout error, a general purpose error, or # a busy error. The error codes are 116, 5, 16 respectively for "err.arg[0]". ################################################################################################### # Arduino Code ################################################################################################### # # #include <Wire.h> # #define BAUD_RATE 19200 # #define CHAR_BUF 128 # # void setup() { # Serial.begin(BAUD_RATE); # Wire.begin(); # delay(1000); // Give the OpenMV Cam time to bootup. # } # # void loop() { # int32_t temp = 0; # char buff[CHAR_BUF] = {0}; # # Wire.requestFrom(0x12, 2); # if(Wire.available() == 2) { // got length? # # temp = Wire.read() | (Wire.read() << 8); # delay(1); // Give some setup time... # # Wire.requestFrom(0x12, temp); # if(Wire.available() == temp) { // got full message? # # temp = 0; # while(Wire.available()) buff[temp++] = Wire.read(); # # } else { # while(Wire.available()) Wire.read(); // Toss garbage bytes. # } # } else { # while(Wire.available()) Wire.read(); // Toss garbage bytes. # } # # Serial.print(buff); # delay(1); // Don't loop to quickly. # } </source> :更多IIC通讯参考:http://docs.openmv.io/library/pyb.I2C.html?highlight=i2c [[MicroMV目录|返回MicroMV目录页面]]
返回至
MicroMV IIC通信
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息