I2C Bus Timing Simulation (Part 2) - Deepen your understanding of the bus protocol

Publisher:不懂之人Latest update time:2016-03-01 Source: eefocusKeywords:I2C Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
[cpp]  view plain  copy
 
 print ?
  1. /****************************************************** ******************* 
  2. This program is the underlying C subroutine of the I2C operating platform (master mode software platform), such as sending data 
  3. And receive data, send the response bit, and provide several operating functions directly facing the device, it is very convenient 
  4. Connect with user programs and expand...   
  5.    
  6.     Note: This function uses software delay to generate SCL pulses.  
  7. Some modifications....(This example is 1us machine cycle, that is, the crystal frequency must be less than 12MHZ) 
  8. *************************************************** ******************/                  
  9. #include /*Include header file*/  
  10. #include   
  11.   
  12. #define uchar unsigned char /*macro definition*/  
  13. #define uint unsigned int  
  14.   
  15. #define _Nop() _nop_() /*define null instruction*/  
  16.   
  17. /* Constant, variable definition area */  
  18.    
  19.                                                  /*Port bit definition*/  
  20. sbit SDA=P1^3; /*Simulate I2C data transmission bit*/  
  21. sbit SCL=P1^2; /*Simulated I2C clock control bit*/  
  22.   
  23.                                                  /*Status flag*/  
  24. bit ack; /*Response flag*/  
  25.      
  26.   
  27. /****************************************************** ****************** 
  28.                      Start bus function                
  29. Function prototype: void Start_I2c();   
  30. Function: Start the I2C bus, that is, send the I2C start condition. 
  31.    
  32. *************************************************** ******************/  
  33. void Start_I2c()  
  34. {  
  35.   SDA=1; /*Send the data signal of the start condition*/  
  36.   _Nop();  
  37.   SCL=1;  
  38.   _Nop(); /*Start condition establishment time is greater than 4.7us, delay*/  
  39.   _Nop();  
  40.   _Nop();  
  41.   _Nop();  
  42.   _Nop();      
  43.   SDA=0; /*Send start signal*/  
  44.   _Nop(); /* Start condition lock time is greater than 4μs*/  
  45.   _Nop();  
  46.   _Nop();  
  47.   _Nop();  
  48.   _Nop();         
  49.   SCL=0; /*Clamp the I2C bus and prepare to send or receive data */  
  50.   _Nop();  
  51.   _Nop();  
  52. }  
  53.   
  54. /****************************************************** ****************** 
  55.                       End bus function                
  56. Function prototype: void Stop_I2c();   
  57. Function: End the I2C bus, that is, send the I2C end condition. 
  58.    
  59. *************************************************** ******************/  
  60. void Stop_I2c()  
  61. {  
  62.   SDA=0; /*Send data signal of end condition*/  
  63.   _Nop(); /*Send the clock signal of the end condition*/  
  64.   SCL=1; /*End condition establishment time is greater than 4μs*/  
  65.   _Nop();  
  66.   _Nop();  
  67.   _Nop();  
  68.   _Nop();  
  69.   _Nop();  
  70.   SDA=1; /*Send I2C bus end signal*/  
  71.   _Nop();  
  72.   _Nop();  
  73.   _Nop();  
  74.   _Nop();  
  75. }  
  76.   
  77. /****************************************************** ****************** 
  78.                  Byte data transfer function                
  79. Function prototype: void SendByte(uchar c); 
  80. Function: Send data c, which can be an address or data, wait for a response after sending, and 
  81.      This status bit operates. (No response or non-response makes ack=0 false)      
  82.      If data is sent normally, ack=1; ack=0 means the controlled device has no response or is damaged. 
  83. *************************************************** ******************/  
  84. void SendByte(uchar c)  
  85. {  
  86.  uchar BitCnt;  
  87.    
  88.  for(BitCnt=0;BitCnt<8;BitCnt++) /*The length of the data to be transmitted is 8 bits*/  
  89.     {  
  90.      if ((c<
  91. ; /*Judge the sending position*/  
  92.        else SDA=0;                  
  93.      _Nop();  
  94.      SCL=1; /*Set the clock line high to notify the controlled device to start receiving data bits*/  
  95.       _Nop();   
  96.       _Nop(); /*Ensure that the clock high level period is greater than 4μs*/  
  97.       _Nop();  
  98.       _Nop();  
  99.       _Nop();           
  100.      SCL=0;   
  101.     }  
  102.       
  103.     _Nop();  
  104.     _Nop();  
  105.     SDA=1; /*Release the data line after sending 8 bits, and prepare to receive the response bit*/  
  106.     _Nop();  
  107.     _Nop();     
  108.     SCL=1;  
  109.     _Nop();  
  110.     _Nop();  
  111.     _Nop();  
  112.     if(SDA==1)ack=0;       
  113.        else ack=1; /*Judge whether the response signal is received*/  
  114.     SCL=0;  
  115.     _Nop();  
  116.     _Nop();  
  117. }  
  118.   
  119. /****************************************************** ****************** 
  120.                  Byte data transfer function                
  121. Function prototype: uchar RcvByte(); 
  122. Function: Used to receive data from the device and determine bus errors (no response signal is sent). 
  123.      Please use the reply function after sending.   
  124. *************************************************** ******************/     
  125. uchar RcvByte()  
  126. {  
  127.   uchar retc;  
  128.   uchar BitCnt;  
  129.     
  130.   retc=0;   
  131.   SDA=1; /*Set the data line to input mode*/  
  132.   for(BitCnt=0;BitCnt<8;BitCnt++)  
  133.       {  
  134.         _Nop();             
  135.         SCL=0; /*Set the clock line low and prepare to receive data bits*/  
  136.         _Nop();  
  137.         _Nop(); /*Clock low level period is greater than 4.7μs*/  
  138.         _Nop();  
  139.         _Nop();  
  140.         _Nop();  
  141.         SCL=1; /*Set the clock line high to make the data on the data line valid*/  
  142.         _Nop();  
  143.         _Nop();  
  144.         retc=retc<<1;  
  145.         if(SDA==1)retc=retc+1; /*Read data bit, and put the received data bit into retc */  
  146.         _Nop();  
  147.         _Nop();   
  148.       }  
  149.   SCL=0;      
  150.   _Nop();  
  151.   _Nop();  
  152.   return(retc);  
  153. }  
  154.   
  155. /****************************************************** ******************* 
  156.                      Response subfunction 
  157. Prototype: void Ack_I2c(bit a); 
  158.   
  159. Function: The master controller sends a response signal (which can be a response or non-response signal) 
  160. *************************************************** ******************/  
  161. void Ack_I2c(bit a)  
  162. {  
  163.     
  164.   if(a==0)SDA=0; /*Send a response or non-response signal here */  
  165.         else SDA=1;  
  166.   _Nop();  
  167.   _Nop();  
  168.   _Nop();        
  169.   SCL=1;  
  170.     _Nop();  
  171.     _Nop(); /*Clock low level period is greater than 4μs*/  
  172.     _Nop();  
  173.     _Nop();  
  174.     _Nop();    
  175.  SCL=0; /*Clear the clock line and clamp the I2C bus to continue receiving*/  
  176.     _Nop();  
  177.     _Nop();      
  178. }  
  179.   
  180. /****************************************************** ****************** 
  181.                     Send byte data function to a device without subaddress                
  182. Function prototype: bit ISendByte(uchar sla,ucahr c);   
  183. Function: The whole process from starting the bus to sending address, data, and ending the bus, slave device address sla. 
  184.            If 1 is returned, the operation is successful, otherwise the operation is wrong. 
  185. Note: The bus must be terminated before use. 
  186. *************************************************** ******************/  
  187. bit ISendByte(uchar sla,uchar c)  
  188. {  
  189.    Start_I2c(); /*Start the bus*/  
  190.    SendByte(sla); /*Send device address*/  
  191.      if(ack==0)return(0);  
  192.    SendByte(c); /*send data*/  
  193.      if(ack==0)return(0);  
  194.   Stop_I2c(); /*End the bus*/   
  195.   return(1);  
  196. }  
  197.   
  198. /****************************************************** ****************** 
  199.                     Send multi-byte data function to a sub-address device                
  200. Function prototype: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);   
  201. Function: From starting the bus to sending address, sub-address, data, and ending the bus, the slave device 
  202.           Address sla, subaddress suba, the content to be sent is the content pointed to by s, and no bytes are sent. 
  203.            If 1 is returned, the operation is successful, otherwise the operation is wrong. 
  204. Note: The bus must be terminated before use. 
  205. *************************************************** ******************/  
  206. bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)  
  207. {  
  208.    uchar i;  
  209.   
  210.    Start_I2c(); /*Start the bus*/  
  211.    SendByte(sla); /*Send device address*/  
  212.      if(ack==0)return(0);  
  213.    SendByte(suba); /*Send device subaddress*/  
  214.      if(ack==0)return(0);  
  215.   
  216.    for(i=0;i
  217.     {     
  218.      SendByte(*s); /*send data*/  
  219.        if(ack==0)return(0);  
  220.      s++;  
  221.     }   
  222.  Stop_I2c(); /*End the bus*/   
  223.   return(1);  
  224. }  
  225. /****************************************************** ****************** 
  226.                     Read byte data function to a device without subaddress                
  227. Function prototype: bit IRcvByte(uchar sla,ucahr *c);   
  228. Function: From starting the bus to sending the address, reading the data, and ending the bus, the whole process is from the device ground 
  229.           Address sla, return value is in c. 
  230.            If 1 is returned, the operation is successful, otherwise the operation is wrong. 
  231. Note: The bus must be terminated before use. 
  232. *************************************************** ******************/  
  233. bit IRcvByte(uchar sla,uchar *c)  
  234. {  
  235.    Start_I2c(); /*Start the bus*/  
  236.    SendByte(sla+1); /*Send device address*/  
  237.      if(ack==0)return(0);  
  238.    *c=RcvByte(); /*Read data*/  
  239.      Ack_I2c(1); /*Send not-acknowledge bit*/  
  240.   Stop_I2c(); /*End the bus*/   
  241.   return(1);  
  242. }  
  243.   
  244. /****************************************************** ****************** 
  245.                     Function to read multi-byte data from a sub-address device                
  246. Function prototype: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);   
  247. Function: From starting the bus to sending address, sub-address, reading data, and ending the bus, the slave device 
  248.           Address sla, subaddress suba, the read content is placed in the storage area pointed to by s, and no bytes are read. 
  249.            If 1 is returned, the operation is successful, otherwise the operation is wrong. 
  250. Note: The bus must be terminated before use. 
  251. *************************************************** ******************/  
  252. bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)  
  253. {  
  254.    uchar i;  
  255.   
  256.    Start_I2c(); /*Start the bus*/  
  257.    SendByte(sla); /*Send device address*/  
  258.      if(ack==0)return(0);  
  259.    SendByte(suba); /*Send device subaddress*/  
  260.      if(ack==0)return(0);  
  261.   
  262.    Start_I2c();  
  263.    SendByte(sla+1);  
  264.       if(ack==0)return(0);  
  265.   
  266.    for(i=0;i
  267.     {     
  268.      *s=RcvByte(); /*Send data*/  
  269.       Ack_I2c(0); /*send the answer bit*/    
  270.      s++;  
  271.     }   
  272.    *s=RcvByte();  
  273.     Ack_I2c(1); /*Send no response bit*/  
  274.  Stop_I2c(); /*End the bus*/   
  275.   return(1);  
  276. }  
  277.                         /* Done */  
Keywords:I2C Reference address:I2C Bus Timing Simulation (Part 2) - Deepen your understanding of the bus protocol

Previous article:I2C bus learning ends, SPI bus learning begins
Next article:I2C bus serial input and output structure

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号