51 MCU I/O simulation I2C program

Publisher:快乐的成长Latest update time:2016-05-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/

                    
  This program is the bottom-level C subroutine of the I2C operating platform (master mode software platform), such as sending data
and receiving data, sending response bits, and providing several operating functions directly facing the device. It is very convenient
to connect and expand with user programs...  
  
After my own experience, it is absolutely easy to use!

/
  
bit ack; /*Response flag*/
  

/*
                     Start bus function               
Function prototype: void Start_I2c();  
Function: Start I2C bus, that is, send I2C start condition.
  
/
void Start_I2c()
{
  SDA=1; /*Send data signal of start condition*/
  _Nop();
  SCL=1;
  _Nop(); /*Start condition establishment time is greater than 4.7us, delay*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();    
  SDA=0; /*Send start signal*/
  _Nop(); /*Start condition locking time is greater than 4μs*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();       
  SCL=0; /*Clamp I2C bus, prepare to send or receive data*/
  _Nop();
  _Nop();
}

 


/*
                      End bus function               
Function prototype: void Stop_I2c();  
Function: End I2C bus, that is, send I2C end condition.
  
/
void Stop_I2c()
{
  SDA=0; /*Send data signal of end condition*/
  _Nop(); /*Send clock signal of end condition*/
  SCL=1; /*End condition establishment time is greater than 4μs*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();
  _Nop();
  SDA=1; /*Send I2C bus end signal*/
  _Nop();
  _Nop();
  _Nop();
  _Nop();
}


/*
                 Byte data transmission function               
prototype: void SendByte(uchar c);
Function: Send data c, which can be an address or data. After sending, wait for a response and operate
     on this      status bit. (No response or non-response makes ack=0 false)
     Sending data is normal, ack=1; ack=0 means that the controlled device has no response or is damaged.
/
void SendByte(uchar c)
{
 uchar BitCnt;
 
 for(BitCnt=0;BitCnt<8;BitCnt++) /*The length of the data to be transmitted is 8 bits*/
    {
     if((c<        else SDA=0;                
     _Nop();
     SCL=1; /*Set the clock line high to notify the controlled device to start receiving data bits*/
      _Nop(); 
      _Nop(); /*Ensure that the clock high level period is greater than 4μs*/
      _Nop();
      _Nop()
      ; _Nop();         
     SCL=0; 
    }
    
    _Nop();
    _Nop();
    SDA=1; /*Release the data line after 8 bits are sent and prepare to receive the acknowledgement bit*/
    _Nop();
    _Nop();   
    SCL=1;
    _Nop();
    _Nop();
    _Nop();
    if(SDA==1)ack=0;     
       else ack=1; /*Judge whether the response signal is received*/
    SCL=0;
    _Nop();
    _Nop();
}

 

 


/*
                 Byte data transfer               
function prototype: uchar RcvByte();
Function: Used to receive data from the device and judge bus error (no response signal is sent).
     Please use the response function after sending.  

uchar RcvByte()
{
  uchar retc;
  uchar BitCnt;
  
  retc=0; 
  SDA=1; /*Set the data line to input mode*/
  for(BitCnt=0;BitCnt<8;BitCnt++)
      {
        _Nop();           
        SCL=0; /*Set the clock line low to prepare for receiving data bits*/
        _Nop();
        _Nop(); /*Clock low level period is greater than 4.7μs*/
        _Nop();
        _Nop();
        _Nop();
        SCL=1; /*Set the clock line high to make the data on the data line valid*/
        _Nop();
        _Nop();
        retc=retc<<1;
        if(SDA==1)retc=retc+1; /*Read the data bit, and put the received data bit into retc*/
        _Nop();
        _Nop(); 
      }
  SCL=0;    
  _Nop();
  _Nop();
  return(retc);
}

 


/
                     Response subfunction
Prototype: void Ack_I2c(bit a);
 
Function: The master controller sends a response signal (can be a response or non-response signal)
/
void Ack_I2c(bit a)
{
  
  if(a==0)SDA=0; /*Send a response or non-response signal here*/
        else SDA=1;
  _Nop(); _Nop()
  ;
  _Nop();      
  SCL=1;
    _Nop();
    _Nop(); /*Clock low level period is greater than 4μs*/
    _Nop();
    _Nop();
    _Nop();  
 SCL=0; /*Clear the clock line and clamp the I2C bus to continue receiving*/
    _Nop();
    _Nop();    
}

 

 


/*
                    Send byte data to the device without sub-address Function               
prototype: bit ISendByte(uchar sla,ucahr c);  
Function: The whole process from starting the bus to sending address, data, and ending the bus, from the device address sla.
           If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit ISendByte(uchar sla,uchar c)
{
   Start_I2c(); /*Start the bus*/
   SendByte(sla); /*Send device address*/
     if(ack==0)return(0);
   SendByte(c); /*Send data*/
     if(ack==0)return(0);
  Stop_I2c(); /*End the bus*/ 
  return(1);
}

 


/*
                    Send multi-byte data to a sub-address device Function               
prototype: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);  
Function: The whole process from starting the bus to sending address, sub-address, data, and ending the bus, from the device
          address sla, sub-address suba, the content to be sent is the content pointed to by s, and no bytes are sent.
           If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
{
   uchar i;

   Start_I2c(); /*Start bus*/
   SendByte(sla); /*Send device address*/
     if(ack==0)return(0);
   SendByte(suba); /*Send device subaddress*/
     if(ack==0)return(0);

   for(i=0;i     {   
     SendByte(*s); /*Send data*/
       if(ack==0)return(0);
     s++;
    } 
 Stop_I2c(); /*End the bus*/ 
  return(1);
}

 

 


/*
                    Read byte data from a device without subaddress Function               
prototype: bit IRcvByte(uchar sla,ucahr *c);  
Function: The whole process from starting the bus to sending the address, reading data, and ending the bus, from the device
          address sla, the return value is in c.
           If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit IRcvByte(uchar sla,uchar *c)
{
   Start_I2c(); /*Start the bus*/
   SendByte(sla+1); /*Send the device address*/
     if(ack==0)return(0);
   *c=RcvByte(); /*Read data*/
     Ack_I2c(1); /*Send the non-answer bit*/
  Stop_I2c(); /*End the bus*/ 
  return(1);
}

 

/*
                    Function to read multi-byte data from a sub-address device Function               
prototype: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);  
Function: From starting the bus to sending the address, sub-address, reading data, and ending the bus, from the device
          address sla, sub-address suba, the read content is put into the storage area pointed to by s, and no bytes are read.
           If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
{
   uchar i;

   Start_I2c(); /*Start bus*/
   SendByte(sla); /*Send device address*/
     if(ack==0)return(0);
   SendByte(suba); /*Send device subaddress*/
     if(ack==0)return(0);

   Start_I2c();
   SendByte(sla+1);
      if(ack==0)return(0);

   for(i=0;i     {   
     *s=RcvByte(); /*Send data*/
      Ack_I2c(0); /*Send acknowledge bit*/  
     s++;
    } 
   *s=RcvByte();
    Ack_I2c(1); /*Send no acknowledge bit*/
 Stop_I2c(); /*End the bus*/ 
  return(1);
}

 

Reference address:51 MCU I/O simulation I2C program

Previous article:51 MCU combined with NRF24L01 to wirelessly control the servo
Next article:51 MCU interrupt 1 interrupt overall introduction

Latest Microcontroller Articles
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号