What occasions are i2c's write, writeto, and write_mem respectively suitable for?[Copy link]
This post was last edited by 9zhmke on 2018-2-22 00:52 I am confused. I wrote a small program to use an infrared positioning sensor. I need to pass a string of initialization information first. It always reports an error OSError: [Errno 19] ENODEV, but sometimes it is correct. 1. I don’t know where it will write to when using i2c.write. Can it find it if I don’t specify the slave address first? 2. When using i2c.writeto, I don’t know how to write the parameter at the end. It always reports an error. i2c.writeto (device address, chr (my value), False), the stop=False or stop=True at the end can only be added as False or True, and the "stop=" at the beginning cannot be added. Even so, it will fail the second time it is used, and it doesn’t work without adding it. 3. i2cwrite_mem seems to be used for EPROM or something like that. Can it be used for ordinary sensors? Please give me more advice. I have searched on Baidu for a long time and tried repeatedly for two days but still can’t pass this level. Attached is the source code: The program I modified:
import time from machine import Pin,I2C i2c=I2C(-1, sda=Pin(4), scl=Pin(5), freq=100000) #scl=1-pin green line, sda=2-pin yellow line buf = buf_bak = bytes(16) #Prepare to put the infrared positioning sensor data x=[4] #Prepare to put the x-axis address y=[4] #Prepare to put the y-axis address tmp=0 #Very random variables i2c_init=[0x30,0x01,0x30,0x08,0x06,0x90,0x08,0xC0,0x1A,0x40,0x33,0x33] #It looks like we are going to pass this string to the sensor 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 measured address is 0x58, the same as the factory mark time.sleep_ms(5) i2c.write(chr(i2c_init[i+1])) #The official example Arduino program is sent in pairs, with an interval of 10ms time.sleep_ms(100) def Read_Ir(): global buf,buf_bak buf_bak=buf #Back up the relevant data first buf=bytes(16) #Clear, the next sentence will read out the same data as the previous one, empty, no data (xFF) or an error, and will be read again until the content different from the previous one is retrieved 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)) #It seems that 0x36 is the data pre-signal try: time.sleep_ms(5) buf=i2c.readfrom(i2c_address[0],16,True) except Exception as e: print("Err") return #============================Start============================ #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)
复制代码
Attached is the manufacturer's original Arduino program:
// 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 // For the adjustment of the position, please refer to the English version: [url=http://wiibrew.org/wiki/Wiimote/Pointing]http://wiibrew.org/wiki/Wiimote/Pointing[/url] // Wiki: [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); }
*, indicating that the number of parameters is variable. buf must be of bytearray type, similar to the array in C language. True and False can also be replaced by 1 or 0
Details
Published on 2018-2-23 23:24
The functions of I2C are actually similar, but they are packaged in different forms. They all send data to the bus. I2C.write() is the most basic method. Usually the first byte is the register address, followed by the data. I am used to packaging the I2C function into the following function for reading and writing registers. For example: # set reg def setReg(self, reg, dat): self.i2c.writeto(BMP280_I2C_ADDR, bytearray([reg, dat])) # get reg def getReg(self, reg): self.i2c.writeto(BMP280_I2C_ADDR, bytearray([reg])) t = self.i2c.readfrom(BMP280_I2C_ADDR, 1) return t[0] # get two reg def get2Reg(self, reg): self.i2c.writeto(BMP280_I2C_ADDR, bytearray([reg])) t = self.i2c.readfrom(BMP280_I2C_ADDR, 2) return t[0] + t[1]*256
I thought the data in writeto was of type str, but I found out it was actually of type bytearray. Let me ask you one more question: What is the reg after writeto? Is dat the data to be sent? I heard that writeto can have a flag at the end to indicate whether all the data has been sent, but it seems that it is not used in def?
Details
Published on 2018-2-22 11:28
[quote]dcexpert posted on 2018-2-22 09:40 The I2C functions are similar, but they are packaged in different forms. They all send data to the bus. I2C.write() is the most basic method. It is usually used to send data to the bus. I2C.write() is the most basic method. I thought the data in writeto was of str type, but I found out that it was of bytearray type. Let me ask you one more question: What is the reg after writeto? Is dat the data to be sent? I heard that writeto can have a flag at the end to express whether all data has been sent, but it seems that it is not used in def? How to use this flag after it?
9zhmke posted on 2018-2-22 11:28 I thought the data of writeto was of str type, but I found out it was of bytearray type. I would like to ask again: What is the reg after writeto?
When describing the special library on page 133 of MicroPython Getting Started, Shao Ziyang wrote about the basic operations of the I2C module in the machine (page 134): (2) Standard bus operations The following method performs standard I2C master device read and write functions. I2C.readfrom(addr,nbytes,stop=True)
Details
Published on 2018-2-23 11:26
When describing the special library on page 133 of the MicroPython Getting Started Guide, Mr. Shao Ziyang wrote about the basic operations of the I2C module in the machine (page 134): (2) Standard bus operations The following method performs the standard I2C master device read and write functions. I2C.readfrom(addr,nbytes,stop=True) Reads nbytes of data from the specified address and returns the data as a bytes object. If stop is true, a STOP signal will be sent at the end. I2C.readfrom_into(addr, buf,stop=True) Reads data from address addr to buffer buf, and the amount of data read is equal to the length of buf. If stop is true, a STOP signal will be sent at the end. This function has no return value. I2C.writeto(addr, buf, *, stop=True) Writes data from buffer buf to the device with address addr. If a NACK signal is received after the write operation, the remaining data will not be sent. If stop is True, a STOP signal will be generated at the end, and a STOP signal will be sent even if a NACK is received. The return value is the number of ACK signals received. ... In addition, you also mentioned about I2C.writeto(addr, buf, *, stop=True) here: https://bbs.eeworld.com.cn/thread-496449-1-1.html There is also a stop=True here.
I2C.writeto(addr, buf, *, stop=True) means that STOP will be sent automatically by default. If you do not want to send it, you can change it to False.
Details
Published on 2018-2-23 21:12
9zhmke posted on 2018-2-23 11:26 In the book MicroPython Getting Started, Mr. Shao Ziyang describes the special library on page 133, which describes the basic operations of the I2C module in the machine (page 134).
I2C.writeto(addr, buf, *, stop=True) means that STOP will be sent automatically by default. If you do not want to send it, you can change it to False.
I'm new to MicroPython and have a lot of questions. Thank you for your guidance, but there are still some details I don't understand: What does the third parameter * mean? The previous documentation didn't mention that the first parameter must be a numeric type. What are the types of the next two parameters? Do they have to be in the same array?
Details
Published on 2018-2-23 22:15
dcexpert posted on 2018-2-23 21:12 I2C.writeto(addr, buf, *, stop=True) means that STOP will be sent automatically by default. If you don't want to send it, you can change it to False.
I have just come into contact with MicroPython. I have a poor foundation and many questions. Thank you for the guidance of the moderator, but there are still some details that I don't understand: What does the third parameter * mean? The previous information did not mention that the first parameter must be a numeric type. What types are required for the next two parameters? Do they have to be in the same array? Which ASCII code is True and FALSE actually? Is it 0?
*, indicating that the number of parameters can be changed. buf must be of bytearray type, similar to the array in C language. True and False can also be replaced by 1 or 0
Details
Published on 2018-2-23 23:24
9zhmke posted on 2018-2-23 22:15 I just came into contact with MicroPython, and my foundation is poor and I have many questions. Thank you for the guidance of the moderator, but there are still some details that I don’t understand: What does the third parameter * mean...
*, indicating that the number of parameters is variable. buf must be of bytearray type, similar to the array in C language. True and False can also be replaced by 1 or 0