Before learning I2C Drive, think about it should be a profound understanding of the I2C agreement. Personally feel that the best way to understand the I2C protocol is to practice, and the best practice is to use GPIO to simulate the I2C protocol test, intuitive and profound.
First look at the I2C sequence diagram:
According to the sequence diagram, the SDA and SCL can be simulated with two gpio respectively. Before that, it is necessary to configure the input and output of Gpio first. This process is described here only from the point of view of writing data. Reading data is equally known.
Configure Gpio first, including SDA, SCL, power supply Gpio, and other additional features Gpio.
Gpio_config (GPIO_SDA, gpio_output);
Gpio_config (GPIO_SCL, gpio_output);
Gpio_config (GPIO_VDD, gpio_output);
Udelay (3);
Gpio_set (GPIO_OLED_VDD, 1);
Start transmission
Gpio_set (gpio_sda,1);
Gpio_set (gpio_scl,1);
Udelay (1);
Gpio_set (gpio_sda,0);
Udelay (1);
Transfer data by character (8-bit):
For (i=0 i<8; i++)
{
gpio_set (GPIO_SCL, 0);
if (C & (1<< (7-i)))
Gpio_set (GPIO_SDA, 1);
else
Gpio_set (GPIO_SDA, 0);
Udelay (1);
Gpio_set (GPIO_SCL, 1);
Udelay (1);
}
End Transport:
Gpio_set (GPIO_SCL, 0);
Gpio_set (GPIO_SDA, 1);
Udelay (1);
Gpio_set (GPIO_SCL, 1);
I2C transmission will receive ACK, according to the ACK value to judge the success and failure of the data sent, in fact, did not write a character data to be judged by an ACK success or not. Read the ACK value, also using GPIO to simulate:
Accept data first configure SDA for input, SCL first high after low, read SDA value.
Gpio_config (GPIO_SDA, gpio_input);
Gpio_set (GPIO_SCL, 1);
Udelay (2);
Gpio_set (GPIO_SCL, 0);
Probably the process is so simple, understand the I2C protocol, and then to understand the I2C drive, of course, it is best to learn from the example.
To start the transmission: