I2C – Two-wire Serial Protocol
2024-10-21
This class is designed for the two-wire serial protocol for communication between devices.
Constructor
machine.I2C
class machine.I2C(I2Cn, MODE, [group])
Parameter:
I2Cn
- Integer type. I2C channel index number.I2C0
:0
- Channel 0I2C1
:1
- Channel 1I2C2
:2
- Channel 2MODE
- Integer type. I2C working mode.STANDARD_MODE
:0
- Standard modeFAST_MODE
:1
- Fast modegroup
Select to use I2C in different pins. Currently, only FCM362K supports Iics. The default value is 0
Example:
>>> from machine import I2C
>>> # Creates an I2C object
>>> i2c_obj = I2C(I2C.I2C0, I2C.STANDARD_MODE) # Returns an I2C object
Example:
>>> from machine import I2C
>>># Creates an I2C object
>>> i2c_obj = I2C(I2C.I2C0, I2C.STANDARD_MODE,0) # Returns an I2C object
I2C Pin Correspondences:
Module | Pin |
---|---|
EC600U | I2C0: SCL: pin11 SDA: pin12 I2C1: SCL: pin57 SDA: pin56 |
EC200U | I2C0: SCL: pin41 SDA: pin42 I2C1: SCL: pin141 SDA: pin142 |
EC200A/UC200A | I2C0: SCL: pin41 SDA: pin42 |
EC600S/EC600N | I2C1: SCL: pin57 SDA: pin56 |
EC100Y | I2C0: SCL: pin57 SDA: pin56 |
BC25 | I2C0: SCL: pin23 SDA: pin22 I2C1: SCL: pin20 SDA: pin21 |
EC800N | I2C0: SCL: pin67 SDA: pin66 |
BG95 | I2C0: SCL: pin18 SDA: pin19 I2C1: SCL: pin40 SDA: pin41 I2C2: SCL: pin26 SDA: pin25 |
EC600M | I2C0: SCL: pin9 SDA: pin64 I2C1: SCL: pin57 SDA: pin56 I2C2: SCL: pin67 SDA: pin65 |
EG915U | I2C0: SCL: pin103 SDA: pin114 I2C1: SCL: pin40 SDA: pin41 |
EC800M/EG810M | I2C0: SCL: pin67 SDA: pin66 I2C2: SCL: pin68 SDA: pin69 |
EG912N | I2C1: SCL: pin40 SDA: pin41 |
EG912U | I2C1: SCL: pin40 SDA: pin41 |
FCM362K | I2C0: SCL:pin30 SDA:pin29 |
BC32 | I2C0: SCL: pin12 SDA: pin26 I2C1: SCL:pin43 SDA:pin44 |
BC92 | I2C0: SCL: pin12 SDA: pin26 I2C1: SCL:pin43 SDA:pin44 |
EG915N | I2C1: SCL: pin40 SDA: pin41 |
Methods
I2C.read
I2C.read(slaveaddress, addr,addr_len, r_data, datalen, delay)
This method reads data to I2C bus.
Parameter:
slaveaddress
- Integer type. I2C device address.addr
- Bytearray type. I2C register address.addr_len
- Integer type. Register address length.r_data
- Bytearray type. Byte array for receiving data.datalen
- Integer type. Length of byte array.delay
- Integer type. Delay. Data conversion buffer time (unit: ms).
Return Value:
0
- Successful execution
-1
- Failed execution
I2C.write
I2C.write(slaveaddress, addr, addr_len, data, datalen)
This method writes data to I2C bus.
Parameter:
slaveaddress
- Integer type. I2C device address.addr
- Bytearray type. I2C register address.addr_len
- Integer type. Register address length.data
- Bytearray type. Data to be written.datalen
- Integer type. Length of data to be written.
Return Value:
0
- Successful execution
-1
- Failed execution
Example:
Please connect the device.
import log
from machine import I2C
import utime
'''
The following two global variables are necessary. You can modify the values of these two global variables based on project requirement.
'''
PROJECT_NAME = "QuecPython_I2C_example"
PROJECT_VERSION = "1.0.0"
'''
I2C usage example
'''
# Sets log output level
log.basicConfig(level=log.INFO)
i2c_log = log.getLogger("I2C")
if __name__ == '__main__':
I2C_SLAVE_ADDR = 0x1B # I2C device address
WHO_AM_I = bytearray([0x02, 0]) # I2C register address. It is passed in as a buff. Take the first value and calculate the length of a value
data = bytearray([0x12, 0]) # Inputs the corresponding command
i2c_obj = I2C(I2C.I2C0, I2C.STANDARD_MODE) # Returns an I2C object
i2c_obj.write(I2C_SLAVE_ADDR, WHO_AM_I, 1, data, 2) # Writes data
r_data = bytearray(2) # Creates a byte array with the length of 2 bytes for receiving
i2c_obj.read(I2C_SLAVE_ADDR, WHO_AM_I, 1, r_data, 2, 0) # read
i2c_log.info(r_data[0])
i2c_log.info(r_data[1])
Constants
Constant | Description | Module |
---|---|---|
I2C.I2C0 | I2C passage index number: 0 | EC100Y/EC600U/EC200U/EC200A/BC25/EC800N/BG95M3/EC600M/EG915U/EC800M/FCM362K/BC32/BC92 |
I2C.I2C1 | I2C passage index number: 1 | EC600S/EC600N/EC600U/EC200U/BC25/BG95M3/EC600M/EG915U/EC800M/EG912N/BC32/BC92 |
I2C.I2C2 | I2C passage index number: 2 | BG95M3/EC600M |
I2C.STANDARD_MODE | Standard mode | All modules |
I2C.FAST_MODE | Fast mode | All modules |
I2C.ENHANCED_FAST_MODE | Enhanced fast mode | FCM360W |