Gpio Simulation I2C

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.