The I2C serial bus generally has two signal lines, one is the bidirectional data line SDA, and the other is the clock line SCL. All serial data SDA connected to the I2C bus device is connected to the bus SDA, and the clock line SCL of each device is connected to the bus SCL. IO simulation IIC communication is introduced using 51 microcontrollers and AT24C02.
Start and stop
Start condition: must be sent before all commands. While the clock line remains at a high level, the data line level jumps from high to low as the start signal of the IIC bus.
Stop condition: while the clock line remains at a high level, the data line level jumps from low to high as the stop signal of the IIC bus. A stop condition must be sent at the end of the operation.
void startbit()
{
clrSCL();
setSDA();
setSCL(); //When the clock is high
clrSDA(); //SDA falling edge
clrSCL();
}
void stopbit()
{
clrSCL();
clrSDA();
setSCL(); //When the clock is high
setSDA(); //SDA rising edge
clrSCL();
}
Response signal
After each successful data transmission, the slave device sends an acknowledge signal. When the ninth clock signal is generated, the device that generates the acknowledge signal pulls SDA down to low to notify that 8 bits of data have been received.
void respond()
{
unsigned char i = 0;
setSDA(); //Release bus
setSCL(); //Clock
while(SDA != 0)
{
i++;
if(i > 200) break;
}
clrSCL();
}
Read and write byte operations
The IIC bus protocol is defined as follows:
1. Data transmission is allowed only when the bus is not busy. [page]
2. During data transmission, when the clock line is high, the data line must be in a fixed state and no jumps are allowed. When the clock line is high, any level change of the data line will be regarded as the start or stop condition of the bus
void writeByte(unsigned char dat)
{
unsigned char i = 0;
for(i = 0;i < 8;i++)
{
clrSCL(); //Pull down the clock line and change the level of the SDA line
if(dat & 0x80) setSDA();
else clrSDA();
setSCL(); //After the SDA level is stable, pull up the clock line
dat <<= 1;
}
clrSCL();
}
unsigned char readByte()
{
unsigned char i = 0,tmp = 0;
for(i = 0;i < 8;i++)
{
clrSCL(); //Pull down the clock line
tmp <<= 1; //Prepare to read data
setSCL(); //Pull up the clock line
if(SDA) tmp |= 0x01;
}
clrSCL();
return tmp;
}
AT24C02 read and write operations
void writeAT24XX(unsigned char addr,unsigned char dat)
{
startbit(); //start signal
writeByte(0xa0); //device address
respond();
writeByte(addr); //device internal address
respond();
writeByte(dat); //data
respond();
stopbit(); //stop
}
unsigned char readAT24XX(unsigned char addr)
{
unsigned char dat;
startbit(); //start signal
writeByte(0xa0); //device address
respond();
writeByte(addr); //device internal address
respond();
startbit(); //start signal
writeByte(0xa1); //device address
respond();
dat = readByte(); //data
stopbit(); //stop
return dat;
}
The main function content and program running effect:
void main()
{
unsigned char dat;
initUart();
sendString("UART INIT OK!!! "); //Serial port communication initialization
sendString("write 0x05 --> addr 0x00 ");//write 5 to internal address 0
writeAT24XX(0x00,0x05); //write data
sendString("read dat <-- addr 0x00 "); //read and write data
dat = readAT24XX(0x00); //Read data
sendString("dat-->"); //Print
sendByte(dat + '0');
while(1);
}
Previous article:Analysis of single bus data transmission in single chip microcomputer data communication
Next article:An example of microcontroller data communication: simulated SPI data transmission
- Popular Resources
- Popular amplifiers
- 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
- 28 "Wanli" Raspberry Pi car - car assembly
- This week's review information has arrived!
- [Xingkong Board Python Programming Learning Main Control Board] 1. Unboxing Hardware Appreciation and Mind+ Environment Construction
- EEWORLD University ---- Micro-Nano Processing (Semiconductor Manufacturing Technology) Swiss Federal Institute of Technology
- FPGA Design Code Cleanliness 2
- Overview of multi-string battery protection chip manufacturers
- Implementation of PID Control Algorithm Based on DSP
- Confused about measuring tantalum capacitors with a digital multimeter?
- This year, friends in the RF field, please come and discuss.
- 【National Technology N32G430】Review of lighting up WS2812 lamp