Files

79 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2024-04-10 12:21:46 +08:00
## I2C
2024-05-27 16:58:34 +08:00
I2C (Inter-Integrated Circuit) bus, this is a common serial communication protocol used to connect multiple microcontrollers, sensors, chips, and other external devices.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
Here are important concepts about I2C that beginners need to understand:
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
1. **Serial Communication:** I2C is a serial communication protocol that allows multiple devices to communicate on the same line, greatly reducing the number of connecting lines.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
2. **Two-Wire System:** I2C uses two lines for communication: SCL (Serial Clock Line) and SDA (Serial Data Line). SCL is used to transmit clock signals, while SDA is used to transmit data.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
3. **Master-Slave Structure:** I2C communication typically involves two types of devices: master devices and slave devices. The master device generates clock signals and controls communication, while the slave devices respond to instructions from the master device.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
4. **Address Allocation:** Each slave device has a unique 7-bit address on the bus, and the master device uses these addresses to select specific slave devices for communication.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
5. **Frame Format:** I2C communication consists of a series of data frames. Each frame typically includes a start bit, an address byte, data bytes, and a stop bit.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
6. **Clock Synchronization:** The I2C protocol uses clock signals generated by the master device to synchronize communication, ensuring data is transmitted at the correct time.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
7. **Speed:** The I2C communication speed can be set as needed. Standard mode (100 Kbps) and fast mode (400 Kbps) are common speeds.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
8. **Applications:** I2C is widely used in embedded systems, sensors, storage devices, displays, electronic chips, and other fields.
2024-04-10 12:21:46 +08:00
2024-05-27 16:58:34 +08:00
examples:
2024-04-10 12:21:46 +08:00
```c
#include <stdio.h>
#include <linux_i2c/linuxi2c.h>
#include <assert.h>
// Define a bus object
LINUXI2CDevice i2cdev;
int main(int argc,char *argv[])
{
char i2c_bus[20] = "/dev/i2c-1";
int ret;
// init bus object
linuxi2c_init_device(&i2cdev);
// open bus
i2cdev.bus = linuxi2c_open(i2c_bus);
if(i2cdev.bus <= 0)
{
printf("i2c bus: %s open fail!\b", i2c_bus);
abort();
}
char data = 0x01;
// write data
ret = linuxi2c_write(&i2cdev, 0x3c, &data, 1);
if(ret < 0)
{
goto end;
}
data = 0;
// read data
ret = linuxi2c_read(&i2cdev, 0x3c, &data, 1);
if(ret < 0)
{
goto end;
}
printf("get i2c bus:%s addr:0x3c val:%x\n", i2c_bus, data);
end:
// Release the i2c bus
linuxi2c_close(i2cdev.bus);
return 0;
}
```
2024-05-27 16:58:34 +08:00
When compiling the above program, you can compile and run it in the examples/linux_i2c directory of [M5Stack_Linux_Libs]().
Enter the Linux device:
2024-04-10 12:21:46 +08:00
``` bash
2024-05-27 16:58:34 +08:00
# Clone repository
2024-04-10 12:21:46 +08:00
git clone https://github.com/M5STACK/M5Stack_Linux_Libs.git
2024-05-27 16:58:34 +08:00
# Enter directory
2024-04-10 12:21:46 +08:00
cd M5Stack_Linux_Libs/examples/linux_i2c
2024-05-27 16:58:34 +08:00
# Compile
2024-04-26 09:45:12 +08:00
scons
2024-04-10 12:21:46 +08:00
```