Easy-Python  1.00.00Change to: 中文
I2C

Functions

 init (i2c_id, speed)
 Make new I2C object. More...
 
 deinit ()
 Turn off the I2C bus. More...
 
 read (i2c_id, slave_addr, data_buffer_addr, buffer_len, stop)
 Read a number of bytes specified by nbytes. I2C.read(i2c_id,slave_addr,data_buffer_addr, buffer_len,stop) More...
 
 write (i2c_id, slave_addr, data_buffer_addr, buffer_len, stop)
 Write data to slave by i2c bus. I2C.write(i2c_id,slave_addr,data_buffer_addr, buffer_len,stop) More...
 

Detailed Description

A I2C object is used to read and write data by I2C.

KEY Example of Micropython
Click here
Example Code of usage:
from machine import I2C
buf=bytearray(8)
buf1=[0x01,0x02,0x03,0x04,0x05,0x6,0x7,0x8]
tmp=I2C(1,100000)
tmp.read(1,0x40,buf,8,1)
tmp.write(1,0x40,buf1,8,1)
tmp.read(1,0x40,buf,8,1)
buf

Function Documentation

init ( i2c_id  ,
speed   
)

Make new I2C object.

Access the I2C peripheral (I2C pin) associated with the given id. If additional arguments are given in the constructor then they are used to initialise the pin. Any settings that are not specified will remain in their previous state.

Parameters
[in]i2c_id: id is the ID number,
  • 1 : select I2C1
  • 2 : select I2C2
[in]speed: speed is the I2C clock rate, which can be one of
  • 100000 : 100 KHz
  • 400000 : 400 KHz
  • 1000000: 1 MHz
Returns
a I2C object
Example of usage:
from machine import I2C
tmp=I2C(1,100000)
deinit ( )

Turn off the I2C bus.

Parameters
no
Returns
(no)
Example of usage:
from machine import I2C
tmp=I2C(1,100000) # Create a I2C
tmp.deinit() # release I2C Bus
read ( i2c_id  ,
slave_addr  ,
data_buffer_addr  ,
buffer_len  ,
stop   
)

Read a number of bytes specified by nbytes. I2C.read(i2c_id,slave_addr,data_buffer_addr, buffer_len,stop)

Parameters
[in]i2c,:i2c_id is the ID number,
  • 1 : select I2C1
  • 2 : select I2C2
[in]slave_addr,:set salve address
[in]data_buffer_addr: set data buffer address
[in]buffer_len: set buffer_len
[in]stop: set stop condition
Returns
(a bytes object)
Example of usage:
from machine import I2C
buf=bytearray(8)
tmp=I2C(1,100000)
tmp.read(1,0x40,buf,8,1)
write ( i2c_id  ,
slave_addr  ,
data_buffer_addr  ,
buffer_len  ,
stop   
)

Write data to slave by i2c bus. I2C.write(i2c_id,slave_addr,data_buffer_addr, buffer_len,stop)

Parameters
[in]i2c: i2c_id is the ID number,
  • 1 : select I2C1
  • 2 : select I2C2
[in]slave_addr,:set salve address
[in]data_buffer_addr: set data buffer address
[in]buffer_len: set buffer_len
[in]stop: set stop condition
Returns
(a bytes object)
Example of usage:
from machine import I2C
buf1=[0x01,0x02,0x03,0x04,0x05,0x6,0x7,0x8]
tmp=I2C(1,100000)
tmp.write(1,0x40,buf1,8,1)