I²c Bus Protocol
Concept:
1. The I²c (inter-integratedcircuit) bus is a two-wire serial bus developed by Philips to connect microcontrollers and their peripherals (especially external memory parts), initially for audio and video development.
2. The I²c bus is a serial bus consisting of the data line SDA and the clock SCL, which can send and receive data.
3, I²c bus in the transmission of data in the process of a total of three special types of signals, they are: start signal, end signal and response signal.
4, I²c first pass high, and then transmit low data.
Timing:
The entire process sequence of data sent by the I²c bus
(1) The starting signal of the I²C
When the SCL line is high, the SDA line represents the start signal from a high-level to low-level change.
/************************************************************************** function name : I2cStart () * Functions : Start signal: The SDA signal produces a falling edge during the SCL clock signal at high level * Input : none * Output : None * Note : SDA and SCL are 0************************************ after starting /void I2cstart () {sda=1;delay10us (); SCL=1;DELAY10US ();//setup time is SDA hold time >4.7ussda=0;delay10us ();//Hold Time is >4USSCL=0;DELAY10US ();}
(2) I²c stop signal
When the SCL line is high, the SDA line represents a stop signal from a low-level to high-level change.
/************************************************************************** function name : I2cStop () * Functions: Stop signal: The SDA signal produces a rising edge during the SCL clock signal high * input : none * Output : None * Note : The SDA and SCL are 1 after the end, indicating bus idle **************************** /void I2cstop () {sda=0;delay10us (); SCL=1;DELAY10US ();//build time greater than 4USSDA=1;DELAY10US ();//sda duration greater than 4.7us}
(4) Response signal
The process of the response signal is that the microcontroller after the completion of 8BIT data after the SDA pull high->I2C chip to the SDA pull-down microcontroller received an ACK signal, and not the response signal will not be pulled low.
Send a byte/************************************************************ function name: i2csendbyte (unsigned char num) * Function function : Sends a byte via I²c. During the SCL clock signal high, keep the send signal SDA stable * Input: num* output: 0 or 1. Send successful return 1, send failed return 0* Note: send a byte scl=0,sda=1************************************************************/unsigned ch Ar i2csendbyte (unsigned char dat) {unsigned char a=0,b=0;//max 255, one machine cycle is 1us, maximum delay 255us. for (a=0;a<8;a++)//To send 8-bit, starting from the highest bit {sda=dat>>7;//Right 7-bit, because the first pass the highest bit dat=dat<<1;// Move left one delay10us (); SCL=1;DELAY10US ();//Build Time >4.7usscl=0;delay10us ();//time greater than 4us}sda=1; Release the data line, wait for the answer signal DELAY10US (); Scl=1;while (SDA)//waiting to be answered, that is, waiting for the SDA to be pulled from the device {b++;if (b>200)//If more than 2000US does not answer the send failed, or for non-reply, means receive end {SCL=0;DELAY10US (); return 0;}} SCL=0;DELAY10US (); return 1;} Receives a byte unsigned char i2creadbyte () {unsigned char a=0,dat=0; sda=1;//releases this line, starting and sending a byte after the SCL is 0delay10us (); for (a=0;a<8;a++)//Receive 8 bytes {scl=1;delay10us ();d at<<=1;dat|= SDA;DELAY10US (); Scl=0;delay10us ();} RetUrn dat;}
Included: Data transfer timing (including read and write)
I²c Bus Usage Instructions