The main advantages of the I2C bus are its simplicity and effectiveness. Since the interface is directly on the components, the I2C bus takes up very little space, reducing the space on the circuit board and the number of chip pins, reducing the cost of interconnection. The bus can be up to 25 feet long and can support 40 components at a maximum transmission rate of 10Kbps. Another advantage of the I2C bus is that it supports multimastering, in which any device that can send and receive can become the master bus. A master can control the transmission of signals and the clock frequency. Of course, there can only be one master at any point in time.
2 Working Principle of I2C Bus
The data stability rule on the I2C bus is that the data on SDA remains stable when SCL is high, and SDA is allowed to change when SCL is low. If a falling edge is generated on SDA when SCL is high, it is considered a start bit, and a rising edge on SDA is considered a stop bit. The communication rate is divided into normal mode (clock frequency 100kHz) and fast mode (clock frequency 400kHz). Multiple devices with I2C interfaces can be connected to the same bus. Each device has a unique address. It can be a single-receive device or a device that can receive and send.
Each data transmission starts with a start bit and ends with a stop bit. There is no limit to the number of bytes transmitted. The most significant bit will be transmitted first, and the receiver will send an acknowledge bit after receiving the 8th bit of data. Data transmission is usually divided into two types: master device sends and slave device receives and slave device sends and master device receives. Both modes require the host to send the start bit and stop bit, and the acknowledge bit is generated by the receiver. The slave device address is generally 1 or 2 bytes, which is used to distinguish different devices connected to the same I2C.
There are three types of signals in the I2C bus during data transmission, namely: start signal, end signal and response signal.
Start signal: When SCL is high, SDA jumps from high to low to start data transmission.
End signal: When SCL is low, SDA jumps from low to high to end data transmission.
Response signal: After receiving 8-bit data, the IC receiving data sends a specific low-level pulse to the IC sending data, indicating that the data has been received. After the CPU sends a signal to the controlled unit, it waits for the controlled unit to send a response signal. After receiving the response signal, the CPU determines whether to continue to transmit the signal based on the actual situation. If no response signal is received, it is determined that the controlled unit has a fault.
There are only two operation modes in the I2C bus: master transmission and master reception. When the system is initialized, the CPU is controlled by instructions to send relevant data, which is sent to the I2C register through the interface. By initializing these registers, the master mode control of the I2C bus can be realized, and the slave device reading and writing on the I2C bus can be realized.
When the master device exchanges data with one of the slave devices, the master device first sends a Start signal, which is received by all the slave devices. That is, the slave device is ready to receive the CPU signal, and then the master device sends the slave device address it wants to communicate with. Next, all the slave devices compare the received address with their own addresses.
If the received address is different from their own address, they do nothing but wait for the master device to send a stop signal; if the received address is the same as their own address, they send a signal to the master device, which is called an acknowledgement signal. When the master device receives the acknowledgement signal, it starts to send data to the slave device or receive data from the slave device. When all operations are completed, the master device sends a Stop signal, the communication is completed, and the I2C bus is released; then all slave devices wait for the next Start signal to arrive.
3 Basic Bus Operations
The I2C protocol uses master/slave bidirectional communication. A device that sends data to the bus is defined as a transmitter, and a device that receives data is defined as a receiver. Both the master and slave devices can operate in the receiving and transmitting states. The bus must be controlled by a master device (usually a microcontroller), which generates a serial clock (SCL) to control the transmission direction of the bus and generates start and stop conditions. The data state on the SDA line can only change during the period when SCL is low. During the period when SCL is high, the change of the SDA state is used to indicate the start and stop conditions.
3.1 Control Byte
After the start condition, there must be a control byte for the device, of which the upper four bits are the device type identifier (different chip types have different definitions, EEPROM should generally be 1010), followed by three bits for chip select, and the last bit is the read/write bit, which is 1 for read operation and 0 for write operation.
1. Writing process
(1) Wait for a delay (1ms) after power-on.
(2) Device addressing, giving a start signal (SDA gives a falling edge when SCL is high). Send the slave device address, the upper 5 bits are 10110, and then perform read/write control (O for read) according to A1/A0 (if the address is the same as the device address, the device will respond).
(3) Response: The device gives a low level on SDA during the 9th cycle of SCL as a response signal.
(4) There are two modes for starting writing: byte write mode and page write mode.
Byte mode: Give A15~A8 response, give A7~A0 response; then give DATA and stop signals (when SCL is high level, SDA gives a rising edge), and then wait for an erase time.
Page write mode: After the address is given, 64 data are given continuously. If there are more than 64 data, the address counter will automatically roll over. (If there are less than 64 data, it is estimated that there is no problem, but it needs to be verified experimentally.)
(5) A method for determining whether the erase operation is completed (response query). If the device is still in the erase state, it will not respond to the device addressing; if there is a response, it means that the erase operation is completed.
2. Reading process
(1) Wait for a delay (lms) after power-on.
(2) Device addressing.
(3)Response.
(4) There are three modes for starting to read: immediate current address read, selective/random read, and continuous read.
Immediate current address read: If the last read/write operation address is N, now it is N+1. No ACK is required, but a Stop signal is required.
Selective/random read: First dummy write (to give an address), then start again to read the data.
·Continuous read: After reading one, give a response, so that the device will give the data content of the next address.
(5) After the data transmission starts and before the data transmission stops, during the period when SCL is high, SDA contains valid data.
/*******************************************************************
1. Program description:
1. The device address of 24LC02 is 1010000R/W.
2. The array is written into 24LC02 in page write mode.
3. The array code is read out from 24LC02 in free read mode.
4. 4.00M crystal is used.
5. Software I2C is used.
2. Hardware connection:
1. SDA------->23 pin. (Of course you can choose any pin for RC4)
2. SCL------->18 Pin. (Of course you can choose any pin for RC3)
3. PORTC----->8 external LEDs to display the read data. Here, the data read is just a flashing running light state.
//Application of I2C
//★★★★★★★★★I2C★★★★★★★★★★★\\
//MCU model: PIC16F877A, EEPROM 24LC02B
//Purpose of the experiment: Understand the general steps, methods and protocols of I2C (SPI serial communication) communication.
//Function description: I2C communication, when RD0 is pressed, the user-defined array is written to 24LC04B, and the RB1 port light is on after writing.
//When RD1 is pressed, data is read from 24LC04B and sent to PORTB for display.
*************************************************** *******************/
#include
#define scl TRISC3 //define clock line
#define sda TRISC4 //define data line
#define nop() asm("nop")
//#define nop() asm("asm")
#define uchar unsigned char
__CONFIG(0xf73a);
uchar no,ack,c,data;
uchar code[]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff}; //The data sent to 24LC04 is also the data to be displayed on the PD port.
//void start_i2c();
//void send_byte(uchar c);
//uchar receive_byte(void);
//void i_send_str(uchar sal,uchar suba,uchar *s,uchar no);
//void i_receive_str()
//=======================================================================
//Function description: Initialization subroutine
//Function name: init();
void init()
{
TRISC=0xff; //Port C is set as input, RC3 is SCL line, RC4 is SDA line
PORTC=0x00;
TRISB=0x00; //Port B is set as output to display the content read from IC24LC02
PORTB=0x00;
TRISD=0xff; //Port D is set as input, when RD0 port is low level, start writing, when RD1 port is low level, start reading.
PORTD=0xff;
}
//=========================================================================
//Function description: delay subroutine
//Function name: delay_250ms();
void delay_250ms()
{
unsigned int d=24999;
while(--d);
}
//=======================================================================
//Function description: I2C startup procedure
//Function name: start_i2c();
void start_i2c()
{
sda=1; //Send the data signal of the start condition
nop();
scl=1;
nop();nop();nop();nop();nop(); //Setup time required by 24LC02
sda=0; //Send the start signal
nop();nop();nop();nop();
scl=0; //Clamp the I2C bus, prepare to send or receive data
nop();nop();
}
//========================================================================
//Function description: I2C stop program
//Function name: stop_i2c();
void stop_i2c()
{
sda=0; //Send the data signal of the end condition
nop();
scl=1;
nop();nop();nop();nop();nop();
sda=1;
nop();nop();nop();nop();
}
{sda=1;} #define scl TRISC3 //define the clock line #define sda TRISC4 //define data line #define nop() asm("nop") //#define nop() asm("asm") #define uchar unsigned char #define uint unsigned int __CONFIG(0xf73a); uchar no,ack,c,data; uchar code[]={0xff,0x7f,0x5f,0x3f,0x1f}; //Data sent to 24LC04, also the data to be displayed at the PD port //================================================ ======================== //Function description: Initialization subroutine //Function name: init(); void init() { TRISC=0xff; //Port C is set as input, RC3 is SCL line, RC4 is SDA line PORTC=0x00; TRISB=0x00; //Port B is set as output to display the contents read from IC24LC02 PORTB=0x00; TRISD=0xff; // Port D is set as input. When port RD0 is at a low level, writing starts. When port RD1 is at a low level, reading starts. PORTD=0xff; } //================================================ ======================== //Function description: Delay subroutine //Function name: delay_250ms(); void delay() { unsigned int d=24999; while(--d); } //================================================ ======================== //Function description: I2C startup program //Function name: start_i2c(); void start_i2c() { sda=1; //Send the data signal of the start condition nop(); scl=1; nop();nop();nop();nop();nop(); //24LC02 required setup time sda=0; //Send start signal nop();nop();nop();nop(); scl=0; //Clamp the I2C bus and prepare to send or receive data nop();nop(); } //================================================ ======================== //Function description: I2C stop program //Function name: stop_i2c(); void stop_i2c() { sda=0; //Send the data signal of the end condition nop(); scl=1; nop();nop();nop();nop(); sda=1; nop();nop();nop();nop(); } //================================================ ======================== //Function description: Byte transfer program // Function: Send data C, which can be an address or data, wait for a response after sending, and report the status //bit operation (no response or non-response makes ack=0), sending data is normal, ack=1; ack=0 //Indicates that the controlled device has no response or is damaged //Function name: send_byte(uchar c); void send_byte(uchar c) { uchar bit_count; for(bit_count=0;bit_count<8;bit_count++) { if((c< else {sda=0;} nop(); scl=1; nop();nop();nop();nop(); scl=0; } nop();nop(); sda=1; nop();nop(); scl=1; nop();nop(); if(RC4==1) ack=0; //Use ACK=1 as a response signal else ack=1; scl=0; nop();nop(); } //================================================ ======================== //Function description: Byte data receiving program //FUNCTION: Used to receive data from the device and determine bus errors (no response signal is sent). //Please use the response function after sending. //Function name: receive_byte(void); uchar receive_byte() { uchar retc,bit_count; retc=0; sda=1; for(bit_count=0;bit_count<8;bit_count++) { nop(); scl=0; nop();nop();nop();nop(); scl=1; nop();nop(); retc=retc<<1; if(RC4==1) retc=retc+1; nop();nop(); } scl=0; nop();nop(); return(retc); } //================================================ ======================== //Function description: error reporting program //Function name: i2c_error(); void i2c_error() { uchar i; for(i=0;i<8;i++) { PORTB=0x00; delay(); PORTB=0x20; delay(); } } //================================================ ======================== //Function description: Send multi-byte program to a sub-address device //Function: The whole process from starting the bus to sending the address, data, and ending the bus, slave device address sla. //Return 1 if the operation is successful, otherwise the operation is wrong //Function name: i_send_str(uchar sla,uchar shba,uchar *s,uchar no); void i_send_str(uchar sla,uchar shba,uchar *s,uchar no) { uchar i; start_i2c(); send_byte(sla); //Send device address if(ack==0) i2c_error(); send_byte(shba); //Send subaddress if(ack==0) i2c_error(); for(i=0;i { send_byte(*s); if(ack==0) i2c_error(); s++; } stop_i2c(); //return(1); } //================================================ ======================== //Function description: Receive multi-byte program from the device //Function: 1. Send device address. 2. Send word address. 3. Send read command and device address. //Function name: i_reveive_str(); void i_receive_str() { uchar i; for(i=0;i<5;i++) { start_i2c(); send_byte(0xa0); //Send device address if(ack==0) i2c_error(); //If no response, enter I2C_error error mode send_byte(i); //send word address if(ack==0) i2c_error(); start_i2c(); //Restart the bus send_byte(0xa1); //Send read command and device address if(ack==0) i2c_error(); data = receive_byte(); stop_i2c(); PORTB=data; delay(); } } //================================================ ======================== //Function description: Main program //Function name: main(); main() { init(); while(1) { if(RD0&&RD1) { i_send_str(0xa0,0x00,code,5); //Send multiple bytes PORTB=0x01; } delay(); if(!RD0&&!RD1) { i_receive_str();} } }
//======================================================================
//Function description: byte transfer program
// Function: Send data C, which can be an address or data. After sending, wait for a response and
operate on this status //bit (make ack=0 for no response or non-response). Sending data is normal, ack=1; ack=0
//Indicates that the controlled device has no response or is damaged.
//Function name: send_byte(uchar c);
void send_byte(uchar c)
{
uchar bit_count;
for(bit_count=0;bit_count<8;bit_count++)
{
if((c<
else {sda=0;}
nop();
scl=1;
nop();nop();nop();nop();
scl=0;
}
nop();nop();
sda=1;
nop();nop();
scl=1;
nop();nop();
if(RC4==1) ack=0; //Use ACK=1 as the response signal
else ack=1;
scl=0;
nop();nop();
}
//========================================================================
//Function description: Byte data receiving program
//FUNCTION: Used to receive data from the device and judge bus errors (no response signal is sent).
//Please use the response function after sending.
//Function name: receive_byte(void);
uchar receive_byte()
{
uchar retc,bit_count;
retc=0;
sda=1;
for(bit_count=0;bit_count<8;bit_count++)
{
nop();
scl=0;
nop();nop();nop();nop();
scl=1;
nop();nop();
retc=retc<<1;
(RC4==1) retc=retc+1;
nop();nop();
}
scl=0;
nop();nop();
return(retc);
}
//=========================================================================
//Function description: error reporting program
//Function name: i2c_error();
void i2c_error()
{
uchar i;
for(i=0;i<8;i++)
{
PORTB=0x00;
delay_250ms();
PORTB=0x20;
delay_250ms();
}
}
//=======================================================================
//Function description: Send a multi-byte program to a device with sub-address.
//Function: The whole process from starting the bus to sending address, data, and ending the bus, slave device address sla. If
//return 1, it means the operation is successful, otherwise the operation is wrong
//Function name: i_send_str(uchar sla,uchar shba,uchar *s,uchar no);
void i_send_str(uchar sla,uchar shba,uchar *s,uchar no)
{
uchar i;
start_i2c();
send_byte(sla); //Send device addressif
(ack==0) i2c_error();
send_byte(shba); //Send sub-addressif
(ack==0) i2c_error();
for(i=0;i
send_byte(*s);
if(ack==0) i2c_error();
s++;
}
stop_i2c();
//return(1);
}
//=========================================================================
//Function description: Receive multi-byte program from device
//Function: 1. Send device address. 2. Send word address. 3. Send read command and device address.
//Function name: i_reveive_str();
void i_receive_str()
{
uchar i;
for(i=0;i<9;i++)
{
start_i2c();
send_byte(0xa0); //Send device addressif
(ack==0) i2c_error(); //If no response, enter I2C_error error mode
send_byte(i); //Send word addressif
(ack==0) i2c_error();
start_i2c(); //Restart the bus
send_byte(0xa1); //Send read command and device addressif
(ack==0) i2c_error();
data=receive_byte();
stop_i2c();
PORTB=data;
delay_250ms();
}
}
//==========================================================================
//Function description: main program
//Function name: main();
#include
Previous article:How PIC generates sound
Next article:A/D Conversion of PIC Microcontroller
Recommended ReadingLatest update time:2024-11-15 15:10
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- What should we pay attention to as we enter the 5G era?
- [Raspberry Pi Pico Review] Unboxing
- The joys and sorrows of UWB (Ultra-Wideband)
- EEWORLD University Hall ---- Computer Control Technology Harbin Institute of Technology Guo Ben
- Type-C dual battery fast charging solution
- About circuit power management
- DIY nucleic acid sampling registration system——lugl43138200
- [ST MEMS waterproof pressure sensor LPS27HHW review] + analysis and use of routines
- Free RF design tools.
- HuaDa Semiconductor MCU M0+ Series Product IAP Reference