51 MCU——I2C bus

Publisher:TranquilBreezeLatest update time:2016-12-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

UART is an asynchronous communication. For example, when a computer sends data to a microcontroller, the computer is only responsible for sending the data through TXD, and receiving the data is the responsibility of the microcontroller. I2C is a synchronous communication. The SCL clock line is responsible for the clock beat of both the sender and the receiver, and the SDA data line is responsible for transmitting data. The sender and receiver of I2C both use the SCL clock beat as the benchmark for sending and receiving data.

        The I2C bus includes two signal lines, SCL and SDA, where SCL is the clock line and SDA is the data line.



1. Start signal

UART communication starts with a low level signal after a continuous high level. The start signal of I2C communication is defined as a falling edge when SDA changes from a high level to a low level during the period when SCL is at a high level, indicating a start signal.

 

2. Data transmission

UART has low bits first and high bits last, while I2C communication has high bits first and low bits last. UART communication data bits have a fixed length, which is one-tenth of the baud rate, and can be sent one bit at a fixed time. I2C does not have a fixed baud rate, but has timing requirements, requiring that when SCL is at a low level, SDA is allowed to change.

 

3. Stop signal

The stop bit of UART communication is a fixed high-level signal; while the definition of the I2C communication stop signal is that when SCL is at a high level, SDA changes from a low level to a high level to generate a rising edge, indicating the end signal.

 

4. After writing to the slave device, wait for the slave device's response

When the master device completes the write operation to the slave device (one byte of data each time), the master device will wait for the slave device to send an indication signal. This indication signal means that the slave device has received the data from the master device. This is done by the slave device's hardware and does not require the master device to perform software operations, only wait;

 

5. After the master device finishes reading the data, it sends a response signal to the slave device

This actually includes two situations. One is that the master device wants to continue reading after finishing reading, so it sends a continue reading signal (actually sending 0). The other is that it does not want to continue reading, so it sends a stop reading signal (actually sending 1).

 

6. I2C addressing mode

After the start signal (Start) of I2C communication, the slave address must be sent first. This address has 7 bits in total. The 8th bit is the data direction bit (R/W). "0" means that data will be sent next (write), and "1" means that data will be requested next (read). The ninth bit is ACK response. 


  1. #include  

  2. #include   

  3.   

  4. #define I2CDelay() {_nop_();_nop_();_nop_();_nop_();}  

  5. sbit I2C_SCL = P3^7;  

  6. sbit I2C_SDA = P3^6;  

  7.   

  8. /* Generate bus start signal */  

  9. void I2CStart()  

  10. {  

  11.   I2C_SDA = 1; //First make sure SDA and SCL are both high  

  12.   I2C_SCL = 1;  

  13.   I2CDelay();  

  14.   I2C_SDA = 0; // Pull SDA low first  

  15.   I2CDelay();  

  16.   I2C_SCL = 0; //Pull SCL low again  

  17. }  

  18. /* Generate bus stop signal */  

  19. void I2CStop()  

  20. {  

  21.   I2C_SCL = 0; //First make sure that SDA and SCL are both low  

  22.   I2C_SDA = 0;  

  23.   I2CDelay();  

  24.   I2C_SCL = 1; //Pull SCL high first  

  25.   I2CDelay();  

  26.   I2C_SDA = 1; //Pull SDA high again  

  27.   I2CDelay();  

  28. }  

  29. /* I2C bus write operation, dat-byte to be written, return value-slave response bit value */  

  30. bit I2CWrite(unsigned char dat)  

  31. {  

  32.   bit ack; //Used to temporarily store the value of the response bit  

  33.   unsigned char mask; //Mask variable used to detect a certain bit value in a byte  

  34.   

  35.   for (mask = 0x80; mask != 0; mask >>= 1) //From high to low  

  36.   {  

  37.     if ((mask & dat) == 0) //The value of this bit is output to SDA  

  38.     {  

  39.       I2C_SDA = 0;  

  40.     }  

  41.     else  

  42.     {  

  43.       I2C_SDA = 1;  

  44.     }  

  45.     I2CDelay();  

  46.     I2C_SCL = 1; //Pull SCL high  

  47.     I2CDelay();  

  48.     I2C_SCL = 0; //Pull SCL low again to complete a bit cycle  

  49.   }  

  50.   I2C_SDA = 1; //After sending 8 bits of data, the host releases SDA to detect the slave's response  

  51.   I2CDelay();  

  52.   I2C_SCL = 1; //Pull SCL high  

  53.   ack = I2C_SDA; //Read the SDA value at this time, which is the response value of the slave  

  54.   I2CDelay();  

  55.   I2C_SCL = 0; //Pull SCL low again to complete the response bit and hold the bus  

  56.   

  57.   return (~ack); //The response value is inverted to conform to the usual logic:  

  58.   //0 = does not exist or is busy or write failed, 1 = exists and is idle or write succeeded  

  59. }  

  60. /* I2C bus read operation, and send a non-response signal, return value - the read byte */  

  61. unsigned char I2CReadNAK()  

  62. {  

  63.   unsigned char mask;  

  64.   unsigned char dat;  

  65.   

  66.   I2C_SDA = 1; //First make sure the host releases SDA  

  67.   for (mask = 0x80; mask != 0; mask >>= 1) //From high to low  

  68.   {  

  69.     I2CDelay();  

  70.     I2C_SCL = 1; //Pull SCL high  

  71.     if(I2C_SDA == 0) //Read the value of SDA  

  72.     {  

  73.       dat &= ~mask; //When it is 0, the corresponding bit in dat is cleared  

  74.     }  

  75.     else  

  76.     {  

  77.       dat |= mask; //When it is 1, the corresponding position in dat is 1  

  78.     }  

  79.     I2CDelay();  

  80.     I2C_SCL = 0; //Pull SCL low again to allow the slave to send the next bit  

  81.   }  

  82.   I2C_SDA = 1; //After sending 8 bits of data, pull up SDA to send a non-response signal  

  83.   I2CDelay();  

  84.   I2C_SCL = 1; //Pull SCL high  

  85.   I2CDelay();  

  86.   I2C_SCL = 0; //Pull SCL low again to complete the non-acknowledge bit and hold the bus  

  87.   

  88.   return dat;  

  89. }  

  90. /* I2C bus read operation, send response signal, return value - read byte */  

  91. unsigned char I2CReadACK()  

  92. {  

  93.   unsigned char mask;  

  94.   unsigned char dat;  

  95.   

  96.   I2C_SDA = 1; //First make sure the host releases SDA  

  97.   for (mask = 0x80; mask != 0; mask >>= 1) //From high to low  

  98.   {  

  99.     I2CDelay();  

  100.     I2C_SCL = 1; //Pull SCL high  

  101.     if(I2C_SDA == 0) //Read the value of SDA  

  102.     {  

  103.       dat &= ~mask; //When it is 0, the corresponding bit in dat is cleared  

  104.     }  

  105.     else  

  106.     {  

  107.       dat |= mask; //When it is 1, the corresponding position in dat is 1  

  108.     }  

  109.     I2CDelay();  

  110.     I2C_SCL = 0; //Pull SCL low again to allow the slave to send the next bit  

  111.   }  

  112.   I2C_SDA = 0; //After sending 8 bits of data, pull SDA low to send a response signal  

  113.   I2CDelay();  

  114.   I2C_SCL = 1; //Pull SCL high  

  115.   I2CDelay();  

  116.   I2C_SCL = 0; //Pull SCL low again to complete the response bit and hold the bus  

  117.   

  118.   return dat;  

  119. }  



Reference address:51 MCU——I2C bus

Previous article:Application of transistors in single chip microcomputers
Next article:51 MCU——UART

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号