Standard 80C51 MCU emulates I2C bus host program

Publisher:AngelicWhisperLatest update time:2017-11-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I2C bus protocol program

When I was making the program, I found Zhou Ligong's program on the Internet. I felt it was pretty good, so I reposted it. When using it, you must pay attention to the issues of sequence and time.

 

"i2c.h file"

//I2C bus integrated sending function, sending multiple bytes of data to the slave

bit I2C_Puts(

unsigned char SlaveAddr,

unsigned int SubAddr,

unsigned char SubMod,

char *that,

unsigned int Size

);

//I2C bus integrated receiving function, receiving multiple bytes of data from the slave

bit I2C_Gets

(

unsigned char SlaveAddr,

unsigned int SubAddr,

unsigned char SubMod,

char *that,

unsigned int Size

);

#endif

================================================================================================

"i2c.c file"

#include "I2C.h"

//Define delay variables for use in macro I2C_Delay()

unsigned char I2C_Delay_t;

 

#define I2C_Delay()  {I2C_Delay_t = (I2C_DELAY_VALUE);while ( --I2C_Delay_t != 0 );}

 

void I2C_Start()

{

  I2C_SDA = 1; I2C_Delay();

  I2C_SCL = 1;   I2C_Delay();

  I2C_SDA = 0; I2C_Delay();

  I2C_SCL = 0;   I2C_Delay();

}

 

void I2C_Write(char dat)

{

  unsigned char t = 8;

  do

  {

     I2C_SDA = (bit)(dat & 0x80);

     dat <<= 1;

     I2C_SCL = 1;   I2C_Delay();

     I2C_SCL = 0;   I2C_Delay();

  } while ( --t != 0 );

}

 

char I2C_Read()

{

  charm that;

  unsigned char t = 8;

  I2C_SDA = 1; //Before reading data, pull SDA high

  do

  {

    I2C_SCL = 1;

    I2C_Delay();

    dat <<= 1;

    if ( I2C_SDA ) dat |= 0x01;

    I2C_SCL = 0;

    I2C_Delay();

  } while ( --t != 0 );

return that;

}

 

bit I2C_GetAck()

{

  bit ack;

  //Bus ready, accept response

  I2C_SDA = 1; I2C_Delay();

  I2C_SCL = 1;  I2C_Delay();

  ack = I2C_SDA;

  I2C_SCL = 0;

  I2C_Delay();

  return ack;

}


/******************************************************************************

Function: I2C_PutAck()

Function: The host generates an acknowledge bit or a non-acknowledge bit

parameter:

ack=0: the host generates an acknowledge bit

ack=1: the host generates a non-acknowledge bit

illustrate:

The host should generate an acknowledge bit after receiving each byte of data.

After receiving the last byte of data, the host should generate a non-acknowledge bit.

******************************************************************************/

void I2C_PutAck(bit ack)

{

  I2C_SDA = ack;  I2C_Delay();

  I2C_SCL = 1;    I2C_Delay();

  I2C_SCL = 0;    I2C_Delay();

}

/******************************************************************************

Function: I2C_Stop()

Function: Generate the stop state of the I2C bus

illustrate:

When a rising edge appears on SDA while SCL is at a high level, the I2C bus is stopped.

Regardless of the level of SDA and SCL, this function can always correctly generate the stop state

After this function is executed, the I2C bus is in idle state

******************************************************************************/

void I2C_Stop()

{

  unsigned int t = I2C_STOP_WAIT_VALUE;

  I2C_SDA = 0; I2C_Delay();

  I2C_SCL = 1;  I2C_Delay();

  I2C_SDA = 1 I2C_Delay();

  while ( --t != 0 ); //Add a certain delay before the next Start is generated

}


/******************************************************************************

Function: I2C_Puts()

Function: I2C bus integrated sending function, sending multiple bytes of data to the slave

parameter:

SlaveAddr: slave address (7-bit pure address, excluding read/write bit)

SubAddr: slave subaddress

SubMod: Subaddress mode, 0 - no subaddress, 1 - single-byte subaddress, 2 - double-byte subaddress

*dat: data to be sent

Size: The number of bytes of data

return:

0: Send successfully

1: An exception occurred during the sending process

illustrate:

This function works well with all common I2C devices, whether or not they have subaddresses.

When the slave has no subaddress, the parameter SubAddr is arbitrary, and SubMod should be 0

******************************************************************************/

bit I2C_Puts

(  unsigned char SlaveAddr , unsigned int SubAddr , unsigned char SubMod , 

  char *dat , unsigned int Size  )

{

  //Define temporary variables

  unsigned char i;

  char a[3];

  if ( Size == 0 ) return 0; // Check length

  a[0] = (SlaveAddr << 1); //Prepare slave address

  if ( SubMod > 2 ) SubMod = 2; // Check subaddress mode

 

  //Determine the sub-address

  switch ( SubMod )

  {

  case 0:

    break;

  case 1:

    a[1] = (char)(SubAddr);

    break;

  case 2:

    a[1] = (char)(SubAddr >> 8);

    a[2] = (char)(SubAddr);

    break;

  default:

    break;

  }


  //Send the slave address (a[0]), then send the sub-address (if there is a sub-address) (a[1], a[2])

  I2C_Start();

  for ( i=0; i<=SubMod; i++ )

  {

    I2C_Write(a[i]);

    if ( I2C_GetAck() )

    {

      I2C_Stop();

      return 1;

    }

  }

  //send data

  do

  {

    I2C_Write(*dat++);

    if ( I2C_GetAck() ) break;

  } while ( --Size != 0 );

  //Sending completed, stop I2C bus, and return the result

  I2C_Stop();

  if ( Size == 0 )

  {

    return 0; //Send successfully

  }

  else

  {

    return 1; //Exception occurred during sending

  }

}


/******************************************************************************

Function: I2C_Gets()

Function: I2C bus integrated receiving function, receiving multiple bytes of data from the slave

parameter:

SlaveAddr: slave address (7-bit pure address, excluding read/write bit)

SubAddr: slave subaddress

SubMod: Subaddress mode, 0 - no subaddress, 1 - single-byte subaddress, 2 - double-byte subaddress

*dat: save the received data

Size: The number of bytes of data

return:

0: Received successfully

1: An exception occurred during the receiving process

illustrate:

This function works well with all common I2C devices, whether or not they have subaddresses.

When the slave has no subaddress, the parameter SubAddr is arbitrary, and SubMod should be 0

******************************************************************************/

bit I2C_Gets

(  unsigned char SlaveAddr , unsigned int SubAddr , unsigned char SubMod , 

  char *dat , unsigned int Size  )

{

  //Define temporary variables

  unsigned char i;

  char a[3];

  if ( Size == 0 ) return 0; // Check length, received successfully

  a[0] = (SlaveAddr << 1); //Prepare slave address

  if ( SubMod > 2 ) SubMod = 2; // Check subaddress mode

  

 //If it is a slave with a sub-address, send the slave address and sub-address first

  if ( SubMod != 0 )

  {

    //Determine the sub-address

    if ( SubMod == 1 )

    {

      a[1] = (char)(SubAddr);

    }

    else

    {

      a[1] = (char)(SubAddr >> 8);

      a[2] = (char)(SubAddr);

    }

   

   //Send the slave address write, then send the sub address

   I2C_Start();

   for ( i=0; i<=SubMod; i++ )

   {

     I2C_Write(a[i]);

     if ( I2C_GetAck() )

     {

       I2C_Stop();

       return 1;

     }

   }

}

 

  //The I2C_Start() here is a repeated start state for the slave with a sub-address

  //For a slave without a sub-address, it is a normal starting state

  I2C_Start();

  //Send slave address to read

  I2C_Write(a[0]+1);

  if ( I2C_GetAck() )

  {

    I2C_Stop();

    return 1;

  }

  

  //Receive data

  for (;;)

  {

    *dat++ = I2C_Read();

    if ( --Size == 0 )

    {

      I2C_PutAck(1);

      break;

    }

    I2C_PutAck(0);

  }

 

  //Receiving completed, stop I2C bus, and return the result

  I2C_Stop();

  return 0;

}

 

 

I2C read and write EEPROM flow chart


Standard 80C51 MCU emulates I2C bus host program


Reference address:Standard 80C51 MCU emulates I2C bus host program

Previous article:STM32 FLASH erase, write and prevent accidental erasure program code
Next article:C51 queue mode interrupt receive query send

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号