i2c的write、writeto、write_mem分别适用于什么场合?
[复制链接]
本帖最后由 9zhmke 于 2018-2-22 00:52 编辑
绕晕了,编个小程序来使用个红外定位传感器,需要先传一串初使化信息过去,老出错报OSError: [Errno 19] ENODEV,偶尔又是对的。
1、用i2c.write我都不知道它会写到哪里去,我没有先指定从机地址的时候它能找到吗?
2、用i2c.writeto则是后面那个参数不知道怎么写老报错,i2c.writeto(设备地址,chr(我的数值),False),后面那个stop=False或者stop=True,只能加成False或者True,不能加前面的“stop=”字样,即便如此,第二次用就出错,不加又不行。
3、i2cwrite_mem看上去应该是EPROM之类用的吧,普通传感器能用么?
请大师多指点下,百度了很久,反复试了两天仍然没有过这一关。附上写的源程序:
我改的程序:
import time
from machine import Pin,I2C
i2c=I2C(-1, sda=Pin(4), scl=Pin(5), freq=100000) #scl=1脚绿线,sda=2脚黄线
buf = buf_bak = bytes(16) #准备放取出的红外定位传感器数据
x=[4] #准备放x轴地址
y=[4] #准备放y轴地址
tmp=0 #很随意个变量
i2c_init=[0x30,0x01,0x30,0x08,0x06,0x90,0x08,0xC0,0x1A,0x40,0x33,0x33] #看上去要传这一串到传感器去
i2c.start()
for i in range(0,12,2):
time.sleep_ms(10)
i2c.write(chr(i2c_init[i])) #i2c.writeto(0x58,chr(i2c_init[i]),False) #i2c.scan实测地址为0x58,与厂标相同
time.sleep_ms(5)
i2c.write(chr(i2c_init[i+1])) #官方示例Arduino程序是一对一对发送的,间隔10ms
time.sleep_ms(100)
def Read_Ir():
global buf,buf_bak
buf_bak=buf #先备份之关数据
buf=bytes(16) #清空,下一句读出与之前数据相同、为空、没数据(xFF)或者出错均重新再取,直到取出与之前不相同的内容
while(buf==buf_bak or buf==bytes(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') or buf==bytes(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')):
time.sleep_ms(3)
i2c.write(chr(0x36)) #看上去0x36是取数据前置信号
try:
time.sleep_ms(5)
buf=i2c.readfrom(i2c_address[0],16,True)
except Exception as e:
print("Err")
return
#==========================开始==========================
#while(True):
for i in range(0,10):
Read_Ir()
print(i,end=" ")
print("buf",end=":")
print (buf)
print(i,end=" ")
print ("Bak",end=":")
print (buf_bak)
复制代码
再附上厂家原版的Arduino程序:
// Wii Remote IR sensor test sample code by kako [url=http://www.kako.com]http://www.kako.com[/url]
// modified output for Wii-BlobTrack program by RobotFreak [url=http://www.letsmakerobots.com/user/1433]http://www.letsmakerobots.com/user/1433[/url]
// modified for [url=http://DFRobot.com]http://DFRobot.com[/url] by Lumi, Jan. 2014
// 对位置的调整可参考英文版:[url=http://wiibrew.org/wiki/Wiimote/Pointing]http://wiibrew.org/wiki/Wiimote/Pointing[/url]
// 维客:[url=http://wiibrew.org/wiki/Wiimote#IR_Camera]http://wiibrew.org/wiki/Wiimote#IR_Camera[/url]
#include <Wire.h>
int IRsensorAddress = 0xB0;
//int IRsensorAddress = 0x58;
int slaveAddress;
int ledPin = 13;
boolean ledState = false;
byte data_buf[16];
int i;
int Ix[4];
int Iy[4];
int s;
void Write_2bytes(byte d1, byte d2)
{
Wire.beginTransmission(slaveAddress);
Wire.write(d1); Wire.write(d2);
Wire.endTransmission();
}
void setup()
{
slaveAddress = IRsensorAddress >> 1; // This results in 0x21 as the address to pass to TWI
Serial.begin(19200);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Wire.begin();
// IR sensor initialize
Write_2bytes(0x30,0x01); delay(10);
Write_2bytes(0x30,0x08); delay(10);
Write_2bytes(0x06,0x90); delay(10);
Write_2bytes(0x08,0xC0); delay(10);
Write_2bytes(0x1A,0x40); delay(10);
Write_2bytes(0x33,0x33); delay(10);
delay(100);
}
void loop()
{
ledState = !ledState;
if (ledState) { digitalWrite(ledPin,HIGH); } else { digitalWrite(ledPin,LOW); }
//IR sensor read
Wire.beginTransmission(slaveAddress);
Wire.write(0x36);
Wire.endTransmission();
Wire.requestFrom(slaveAddress, 16); // Request the 2 byte heading (MSB comes first)
for (i=0;i<16;i++) { data_buf[i]=0; }
i=0;
while(Wire.available() && i < 16) {
data_buf[i] = Wire.read();
i++;
}
Ix[0] = data_buf[1];
Iy[0] = data_buf[2];
s = data_buf[3];
Ix[0] += (s & 0x30) <<4;
Iy[0] += (s & 0xC0) <<2;
Ix[1] = data_buf[4];
Iy[1] = data_buf[5];
s = data_buf[6];
Ix[1] += (s & 0x30) <<4;
Iy[1] += (s & 0xC0) <<2;
Ix[2] = data_buf[7];
Iy[2] = data_buf[8];
s = data_buf[9];
Ix[2] += (s & 0x30) <<4;
Iy[2] += (s & 0xC0) <<2;
Ix[3] = data_buf[10];
Iy[3] = data_buf[11];
s = data_buf[12];
Ix[3] += (s & 0x30) <<4;
Iy[3] += (s & 0xC0) <<2;
for(i=0; i<4; i++)
{
if (Ix[i] < 1000)
Serial.print("");
if (Ix[i] < 100)
Serial.print("");
if (Ix[i] < 10)
Serial.print("");
Serial.print( int(Ix[i]) );
Serial.print(",");
if (Iy[i] < 1000)
Serial.print("");
if (Iy[i] < 100)
Serial.print("");
if (Iy[i] < 10)
Serial.print("");
Serial.print( int(Iy[i]) );
if (i<3)
Serial.print(",");
}
Serial.println("");
delay(15);
}
复制代码