title: Interfacing issue LPC1769 and MPU6050
body: <p>I'm trying to interface LPC1769 and MPU6050 And i am using <strong>mcb1700.code.bundle.lpc1769.lpcxpresso</strong> libraries. And I'm using LpcXpresso as IDE. </p>
  
  <p>As specified in the MPU6050 datasheet I coded for the read sequence, it's working first time only, if i try to read second time, it returns zero values. ( i.e., it works only once after the reset )</p>
  
  <p>I tried with many possible ways, No results So please give me some solution to fix this Issue.</p>
  
  <p><strong>Edited..</strong>
  Code I've shown below. Both read and burstRead code working only once</p>
  
  <p>i2c.c</p>
  
  <pre><code>    #include "lpc17xx.h"
      #include "type.h"
      #include "i2c.h"
  
      volatile uint32_t I2CMasterState[I2C_PORT_NUM] = {I2C_IDLE,I2C_IDLE,I2C_IDLE};
      volatile uint32_t timeout[I2C_PORT_NUM] = {0, 0, 0};
  
      volatile uint8_t I2CMasterBuffer[I2C_PORT_NUM][BUFSIZE];
      volatile uint8_t I2CSlaveBuffer[I2C_PORT_NUM][BUFSIZE];
      volatile uint32_t I2CCount[I2C_PORT_NUM] = {0, 0, 0};
      volatile uint32_t I2CReadLength[I2C_PORT_NUM];
      volatile uint32_t I2CWriteLength[I2C_PORT_NUM];
  
      volatile uint32_t RdIndex0 = 0, RdIndex1 = 0, RdIndex2 = 0;
      volatile uint32_t WrIndex0 = 0, WrIndex1 = 0, WrIndex2 = 0;
  
      /* 
      From device to device, the I2C communication protocol may vary, 
      in the example below, the protocol uses repeated start to read data from or 
      write to the device:
      For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO 
      for master write: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO
      Thus, in state 8, the address is always WRITE. in state 10, the address could 
      be READ or WRITE depending on the I2C command.
      */   
  
      /*****************************************************************************
      ** Function name:       I2C_IRQHandler
      **
      ** Descriptions:        I2C interrupt handler, deal with master mode only.
      **
      ** parameters:          None
      ** Returned value:      None
      ** 
      *****************************************************************************/
      void I2C0_IRQHandler(void) 
      {
        uint8_t StatValue;
  
        timeout[0] = 0;
        /* this handler deals with master read and master write only */
        StatValue = LPC_I2C0-&gt;STAT;
        switch ( StatValue )
        {
          case 0x08:          /* A Start condition is issued. */
          WrIndex0 = 0;
          LPC_I2C0-&gt;DAT = I2CMasterBuffer[0][WrIndex0++];
          LPC_I2C0-&gt;CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
          break;
  
          case 0x10:          /* A repeated started is issued */
          RdIndex0 = 0;
          /* Send SLA with R bit set, */
          LPC_I2C0-&gt;DAT = I2CMasterBuffer[0][WrIndex0++];
          LPC_I2C0-&gt;CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
          break;
  
          case 0x18:          /* Regardless, it's a ACK */
          if ( I2CWriteLength[0] == 1 )
          {
            LPC_I2C0-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
            I2CMasterState[0] = I2C_NO_DATA;
          }
          else
          {
            LPC_I2C0-&gt;DAT = I2CMasterBuffer[0][WrIndex0++];
          }
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x28:  /* Data byte has been transmitted, regardless ACK or NACK */
          if ( WrIndex0 &lt; I2CWriteLength[0] )
          {   
            LPC_I2C0-&gt;DAT = I2CMasterBuffer[0][WrIndex0++]; /* this should be the last one */
          }
          else
          {
            if ( I2CReadLength[0] != 0 )
            {
              LPC_I2C0-&gt;CONSET = I2CONSET_STA;    /* Set Repeated-start flag */
            }
            else
            {
              LPC_I2C0-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
              I2CMasterState[0] = I2C_OK;
            }
          }
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x30:
          LPC_I2C0-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState[0] = I2C_NACK_ON_DATA;
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x40:  /* Master Receive, SLA_R has been sent */
          if ( (RdIndex0 + 1) &lt; I2CReadLength[0] )
          {
            /* Will go to State 0x50 */
            LPC_I2C0-&gt;CONSET = I2CONSET_AA;   /* assert ACK after data is received */
          }
          else
          {
            /* Will go to State 0x58 */
            LPC_I2C0-&gt;CONCLR = I2CONCLR_AAC;  /* assert NACK after data is received */
          }
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x50:  /* Data byte has been received, regardless following ACK or NACK */
          I2CSlaveBuffer[0][RdIndex0++] = LPC_I2C0-&gt;DAT;
          if ( (RdIndex0 + 1) &lt; I2CReadLength[0] )
          {   
            LPC_I2C0-&gt;CONSET = I2CONSET_AA;   /* assert ACK after data is received */
          }
          else
          {
            LPC_I2C0-&gt;CONCLR = I2CONCLR_AAC;  /* assert NACK on last byte */
          }
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x58:
          I2CSlaveBuffer[0][RdIndex0++] = LPC_I2C0-&gt;DAT;
          I2CMasterState[0] = I2C_OK;
          LPC_I2C0-&gt;CONSET = I2CONSET_STO;    /* Set Stop flag */ 
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;    /* Clear SI flag */
          break;
  
          case 0x20:      /* regardless, it's a NACK */
          case 0x48:
          LPC_I2C0-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState[0] = I2C_NACK_ON_ADDRESS;
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x38:      /* Arbitration lost, in this example, we don't
                          deal with multiple master situation */
          default:
          I2CMasterState[0] = I2C_ARBITRATION_LOST;
          LPC_I2C0-&gt;CONCLR = I2CONCLR_SIC;    
          break;
        }
        return;
      }
  
      /*****************************************************************************
      ** Function name:       I2C_IRQHandler
      **
      ** Descriptions:        I2C interrupt handler, deal with master mode only.
      **
      ** parameters:          None
      ** Returned value:      None
      ** 
      *****************************************************************************/
      void I2C1_IRQHandler(void) 
      {
        uint8_t StatValue;
  
        timeout[1] = 0;
        /* this handler deals with master read and master write only */
        StatValue = LPC_I2C1-&gt;STAT;
        switch ( StatValue )
        {
          case 0x08:          /* A Start condition is issued. */
          WrIndex1 = 0;
          LPC_I2C1-&gt;DAT = I2CMasterBuffer[1][WrIndex1++];
          LPC_I2C1-&gt;CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
          break;
  
          case 0x10:          /* A repeated started is issued */
          RdIndex1 = 0;
          /* Send SLA with R bit set, */
          LPC_I2C1-&gt;DAT = I2CMasterBuffer[1][WrIndex1++];
          LPC_I2C1-&gt;CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
          break;
  
          case 0x18:          /* Regardless, it's a ACK */
          if ( I2CWriteLength[1] == 1 )
          {
            LPC_I2C1-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
            I2CMasterState[1] = I2C_NO_DATA;
          }
          else
          {
            LPC_I2C1-&gt;DAT = I2CMasterBuffer[1][WrIndex1++];
          }
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x28:  /* Data byte has been transmitted, regardless ACK or NACK */
          if ( WrIndex1 &lt; I2CWriteLength[1] )
          {   
            LPC_I2C1-&gt;DAT = I2CMasterBuffer[1][WrIndex1++]; /* this should be the last one */
          }
          else
          {
            if ( I2CReadLength[1] != 0 )
            {
              LPC_I2C1-&gt;CONSET = I2CONSET_STA;    /* Set Repeated-start flag */
            }
            else
            {
              LPC_I2C1-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
              I2CMasterState[1] = I2C_OK;
            }
          }
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x30:
          LPC_I2C1-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState[1] = I2C_NACK_ON_DATA;
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x40:  /* Master Receive, SLA_R has been sent */
          if ( (RdIndex1 + 1) &lt; I2CReadLength[1] )
          {
            /* Will go to State 0x50 */
            LPC_I2C1-&gt;CONSET = I2CONSET_AA;   /* assert ACK after data is received */
          }
          else
          {
            /* Will go to State 0x58 */
            LPC_I2C1-&gt;CONCLR = I2CONCLR_AAC;  /* assert NACK after data is received */
          }
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x50:  /* Data byte has been received, regardless following ACK or NACK */
          I2CSlaveBuffer[1][RdIndex1++] = LPC_I2C1-&gt;DAT;
          if ( (RdIndex1 + 1) &lt; I2CReadLength[1] )
          {   
            LPC_I2C1-&gt;CONSET = I2CONSET_AA;   /* assert ACK after data is received */
          }
          else
          {
            LPC_I2C1-&gt;CONCLR = I2CONCLR_AAC;  /* assert NACK on last byte */
          }
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x58:
          I2CSlaveBuffer[1][RdIndex1++] = LPC_I2C1-&gt;DAT;
          I2CMasterState[1] = I2C_OK;
          LPC_I2C1-&gt;CONSET = I2CONSET_STO;    /* Set Stop flag */ 
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;    /* Clear SI flag */
          break;
  
          case 0x20:      /* regardless, it's a NACK */
          case 0x48:
          LPC_I2C1-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState[1] = I2C_NACK_ON_ADDRESS;
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x38:      /* Arbitration lost, in this example, we don't
                          deal with multiple master situation */
          default:
          I2CMasterState[1] = I2C_ARBITRATION_LOST;
          LPC_I2C1-&gt;CONCLR = I2CONCLR_SIC;    
          break;
        }
        return;
      }
  
      /*****************************************************************************
      ** Function name:       I2C_IRQHandler
      **
      ** Descriptions:        I2C interrupt handler, deal with master mode only.
      **
      ** parameters:          None
      ** Returned value:      None
      ** 
      *****************************************************************************/
      void I2C2_IRQHandler(void) 
      {
        uint8_t StatValue;
  
        timeout[2] = 0;
        /* this handler deals with master read and master write only */
        StatValue = LPC_I2C2-&gt;STAT;
        switch ( StatValue )
        {
          case 0x08:          /* A Start condition is issued. */
          WrIndex2 = 0;
          LPC_I2C2-&gt;DAT = I2CMasterBuffer[2][WrIndex2++];
          LPC_I2C2-&gt;CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
          break;
  
          case 0x10:          /* A repeated started is issued */
          RdIndex2 = 0;
          /* Send SLA with R bit set, */
          LPC_I2C2-&gt;DAT = I2CMasterBuffer[2][WrIndex2++];
          LPC_I2C2-&gt;CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
          break;
  
          case 0x18:          /* Regardless, it's a ACK */
          if ( I2CWriteLength[2] == 1 )
          {
            LPC_I2C2-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
            I2CMasterState[2] = I2C_NO_DATA;
          }
          else
          {
            LPC_I2C2-&gt;DAT = I2CMasterBuffer[2][WrIndex2++];
          }
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x28:  /* Data byte has been transmitted, regardless ACK or NACK */
          if ( WrIndex2 &lt; I2CWriteLength[2] )
          {   
            LPC_I2C2-&gt;DAT = I2CMasterBuffer[2][WrIndex2++]; /* this should be the last one */
          }
          else
          {
            if ( I2CReadLength[2] != 0 )
            {
              LPC_I2C2-&gt;CONSET = I2CONSET_STA;    /* Set Repeated-start flag */
            }
            else
            {
              LPC_I2C2-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
              I2CMasterState[2] = I2C_OK;
            }
          }
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x30:
          LPC_I2C2-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState[2] = I2C_NACK_ON_DATA;
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x40:  /* Master Receive, SLA_R has been sent */
          if ( (RdIndex2 + 1) &lt; I2CReadLength[2] )
          {
            /* Will go to State 0x50 */
            LPC_I2C2-&gt;CONSET = I2CONSET_AA;   /* assert ACK after data is received */
          }
          else
          {
            /* Will go to State 0x58 */
            LPC_I2C2-&gt;CONCLR = I2CONCLR_AAC;  /* assert NACK after data is received */
          }
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x50:  /* Data byte has been received, regardless following ACK or NACK */
          I2CSlaveBuffer[2][RdIndex2++] = LPC_I2C2-&gt;DAT;
          if ( (RdIndex2 + 1) &lt; I2CReadLength[2] )
          {   
            LPC_I2C2-&gt;CONSET = I2CONSET_AA;   /* assert ACK after data is received */
          }
          else
          {
            LPC_I2C2-&gt;CONCLR = I2CONCLR_AAC;  /* assert NACK on last byte */
          }
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x58:
          I2CSlaveBuffer[2][RdIndex2++] = LPC_I2C2-&gt;DAT;
          I2CMasterState[2] = I2C_OK;
          LPC_I2C2-&gt;CONSET = I2CONSET_STO;    /* Set Stop flag */ 
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;    /* Clear SI flag */
          break;
  
          case 0x20:      /* regardless, it's a NACK */
          case 0x48:
          LPC_I2C2-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState[2] = I2C_NACK_ON_ADDRESS;
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;
          break;
  
          case 0x38:      /* Arbitration lost, in this example, we don't
                          deal with multiple master situation */
          default:
          I2CMasterState[2] = I2C_ARBITRATION_LOST;
          LPC_I2C2-&gt;CONCLR = I2CONCLR_SIC;    
          break;
        }
        return;
      }
  
      /*****************************************************************************
      ** Function name:       I2CStart
      **
      ** Descriptions:        Create I2C start condition, a timeout
      **              value is set if the I2C never gets started,
      **              and timed out. It's a fatal error. 
      **
      ** parameters:          None
      ** Returned value:      true or false, return false if timed out
      ** 
      *****************************************************************************/
      uint32_t I2CStart( uint32_t portNum )
      {
        uint32_t retVal = FALSE;
  
        timeout[portNum] = 0;
        /*--- Issue a start condition ---*/
        LPC_I2C[portNum]-&gt;CONSET = I2CONSET_STA;  /* Set Start flag */
  
        /*--- Wait until START transmitted ---*/
        while( 1 )
        {
          if ( I2CMasterState[portNum] == I2C_STARTED )
          {
            retVal = TRUE;
            break;    
          }
          if ( timeout[portNum] &gt;= MAX_TIMEOUT )
          {
            retVal = FALSE;
            break;
          }
          timeout[portNum]++;
        }
        return( retVal );
      }
  
      /*****************************************************************************
      ** Function name:       I2CStop
      **
      ** Descriptions:        Set the I2C stop condition, if the routine
      **              never exit, it's a fatal bus error.
      **
      ** parameters:          None
      ** Returned value:      true or never return
      ** 
      *****************************************************************************/
      uint32_t I2CStop( uint32_t portNum )
      {
        LPC_I2C[portNum]-&gt;CONSET = I2CONSET_STO;      /* Set Stop flag */ 
        LPC_I2C[portNum]-&gt;CONCLR = I2CONCLR_SIC;  /* Clear SI flag */ 
  
        /*--- Wait for STOP detected ---*/
        while( LPC_I2C[portNum]-&gt;CONSET &amp; I2CONSET_STO );
        return TRUE;
      }
  
      /*****************************************************************************
      ** Function name:       I2CInit
      **
      ** Descriptions:        Initialize I2C controller as a master
      **
      ** parameters:          None
      ** Returned value:      None
      ** 
      *****************************************************************************/
      void I2C0Init( void ) 
      {
        LPC_SC-&gt;PCONP |= (1 &lt;&lt; 7);
  
        /* set PIO0.27 and PIO0.28 to I2C0 SDA and SCL */
        /* function to 01 on both SDA and SCL. */
        LPC_PINCON-&gt;PINSEL1 &amp;= ~((0x03&lt;&lt;22)|(0x03&lt;&lt;24));
        LPC_PINCON-&gt;PINSEL1 |= ((0x01&lt;&lt;22)|(0x01&lt;&lt;24));   
  
        /*--- Clear flags ---*/
        LPC_I2C0-&gt;CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;    
  
        /*--- Reset registers ---*/
      #if FAST_MODE_PLUS
        LPC_PINCON-&gt;I2CPADCFG |= ((0x1&lt;&lt;0)|(0x1&lt;&lt;2));
        LPC_I2C0-&gt;SCLL   = I2SCLL_HS_SCLL;
        LPC_I2C0-&gt;SCLH   = I2SCLH_HS_SCLH;
      #else
        LPC_PINCON-&gt;I2CPADCFG &amp;= ~((0x1&lt;&lt;0)|(0x1&lt;&lt;2));
        LPC_I2C0-&gt;SCLL   = I2SCLL_SCLL;
        LPC_I2C0-&gt;SCLH   = I2SCLH_SCLH;
      #endif
  
        /* Install interrupt handler */
        NVIC_EnableIRQ(I2C0_IRQn);
  
        LPC_I2C0-&gt;CONSET = I2CONSET_I2EN;
        return;
      }
  
      /*****************************************************************************
      ** Function name:       I2C1Init
      **
      ** Descriptions:        Initialize I2C controller as a master
      **
      ** parameters:          None
      ** Returned value:      None
      ** 
      *****************************************************************************/
      void I2C1Init( void ) 
      {
        LPC_SC-&gt;PCONP |= (1 &lt;&lt; 19);
  
      #if 0
        /* set PIO0.0 and PIO0.1 to I2C1 SDA and SCL */
        /* function to 11 on both SDA and SCL. */
        LPC_PINCON-&gt;PINSEL0 &amp;= ~((0x3&lt;&lt;0)|(0x3&lt;&lt;2));
        LPC_PINCON-&gt;PINSEL0 |= ((0x3&lt;&lt;0)|(0x3&lt;&lt;2));
        LPC_PINCON-&gt;PINMODE0 &amp;= ~((0x3&lt;&lt;0)|(0x3&lt;&lt;2));
        LPC_PINCON-&gt;PINMODE0 |= ((0x2&lt;&lt;0)|(0x2&lt;&lt;2));      /* No pull-up no pull-down */
        LPC_PINCON-&gt;PINMODE_OD0 |= ((0x01&lt;&lt;0)|(0x1&lt;&lt;1));  /* Open drain */    
      #endif
      #if 1
        /* set PIO0.19 and PIO0.20 to I2C1 SDA and SCL */
        /* function to 11 on both SDA and SCL. */
        LPC_PINCON-&gt;PINSEL1 &amp;= ~((0x3&lt;&lt;6)|(0x3&lt;&lt;8));
        LPC_PINCON-&gt;PINSEL1 |= ((0x3&lt;&lt;6)|(0x3&lt;&lt;8));
        LPC_PINCON-&gt;PINMODE1 &amp;= ~((0x3&lt;&lt;6)|(0x3&lt;&lt;8));
        LPC_PINCON-&gt;PINMODE1 |= ((0x2&lt;&lt;6)|(0x2&lt;&lt;8));  /* No pull-up no pull-down */
        LPC_PINCON-&gt;PINMODE_OD0 |= ((0x1&lt;&lt;19)|(0x1&lt;&lt;20)); 
      #endif
  
        /*--- Clear flags ---*/
        LPC_I2C1-&gt;CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;    
  
        /*--- Reset registers ---*/
        LPC_I2C1-&gt;SCLL   = I2SCLL_SCLL;
        LPC_I2C1-&gt;SCLH   = I2SCLH_SCLH;
  
        /* Install interrupt handler */
        NVIC_EnableIRQ(I2C1_IRQn);
  
        LPC_I2C1-&gt;CONSET = I2CONSET_I2EN;
        return;
      }
  
      /*****************************************************************************
      ** Function name:       I2C2Init
      **
      ** Descriptions:        Initialize I2C controller as a master
      **
      ** parameters:          None
      ** Returned value:      None
      ** 
      *****************************************************************************/
      void I2C2Init( void ) 
      {
        LPC_SC-&gt;PCONP |= (1 &lt;&lt; 26);
  
        /* set PIO0.10 and PIO0.11 to I2C2 SDA and SCL */
        /* function to 10 on both SDA and SCL. */
        LPC_PINCON-&gt;PINSEL0 &amp;= ~((0x03&lt;&lt;20)|(0x03&lt;&lt;22));
        LPC_PINCON-&gt;PINSEL0 |= ((0x02&lt;&lt;20)|(0x02&lt;&lt;22));
        LPC_PINCON-&gt;PINMODE0 &amp;= ~((0x03&lt;&lt;20)|(0x03&lt;&lt;22));
        LPC_PINCON-&gt;PINMODE0 |= ((0x02&lt;&lt;20)|(0x2&lt;&lt;22));   /* No pull-up no pull-down */
        LPC_PINCON-&gt;PINMODE_OD0 |= ((0x01&lt;&lt;10)|(0x1&lt;&lt;11));    
  
        /*--- Clear flags ---*/
        LPC_I2C2-&gt;CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;    
  
        /*--- Reset registers ---*/
        LPC_I2C2-&gt;SCLL   = I2SCLL_SCLL;
        LPC_I2C2-&gt;SCLH   = I2SCLH_SCLH;
  
        /* Install interrupt handler */
        NVIC_EnableIRQ(I2C2_IRQn);
  
        LPC_I2C2-&gt;CONSET = I2CONSET_I2EN;
        return;
      }
  
      /*****************************************************************************
      ** Function name:       I2CEngine
      **
      ** Descriptions:        The routine to complete a I2C transaction
      **                      from start to stop. All the intermitten
      **                      steps are handled in the interrupt handler.
      **                      Before this routine is called, the read
      **                      length, write length, I2C master buffer,
      **                      and I2C command fields need to be filled.
      **                      see i2cmst.c for more details. 
      **
      ** parameters:          I2C port number
      ** Returned value:      master state of current I2C port.
      ** 
      *****************************************************************************/
      uint32_t I2CEngine( uint32_t portNum ) 
      {
        /*--- Issue a start condition ---*/
        LPC_I2C[portNum]-&gt;CONSET = I2CONSET_STA;  /* Set Start flag */
  
        I2CMasterState[portNum] = I2C_BUSY;   
  
        while ( I2CMasterState[portNum] == I2C_BUSY )
        {
          if ( timeout[portNum] &gt;= MAX_TIMEOUT )
          {
            I2CMasterState[portNum] = I2C_TIME_OUT;
            break;
          }
          timeout[portNum]++;
        }
        LPC_I2C[portNum]-&gt;CONCLR = I2CONCLR_STAC;
  
        return ( I2CMasterState[portNum] );
      }
  
      /******************************************************************************
      **                            End Of File
      ******************************************************************************/
  </code></pre>
  
  <p>MPU6050 functions </p>
  
  <pre><code>    void mpu6050BurstRead(uint8_t startRegAdd, uint8_t length, uint8_t *data) {
          // Clear buffers
          uint32_t i;
          for (i = 0; i &lt; BUFSIZE; i++) {
              I2CMasterBuffer[I2C_PORT][i] = 0x00;
              I2CSlaveBuffer[I2C_PORT][i] = 0x00;
          }
  
          I2CWriteLength[I2C_PORT] = 2;
          I2CReadLength[I2C_PORT] = 0;
          I2CMasterBuffer[I2C_PORT][0] = MPU6050_ADDRESS;
          I2CMasterBuffer[I2C_PORT][1] = startRegAdd;
          I2CEngine(I2C_PORT);
  
          for (i = 0; i &lt; BUFSIZE; i++) {
              I2CMasterBuffer[I2C_PORT][i] = 0x00;
              I2CSlaveBuffer[I2C_PORT][i] = 0x00;
          }
  
          I2CWriteLength[I2C_PORT] = 1;
          I2CReadLength[I2C_PORT] = length;
          I2CMasterBuffer[I2C_PORT][0] = MPU6050_ADDRESS|MPU6050_READ;
          I2CEngine(I2C_PORT);
  
          memcpy(data,I2CSlaveBuffer,length);
  
      }
  
      void mpu6050Write(uint8_t regAdd,uint8_t regValue) {
          // Clear buffers
          uint32_t i;
          for (i = 0; i &lt; BUFSIZE; i++) {
              I2CMasterBuffer[I2C_PORT][i] = 0x00;
              I2CSlaveBuffer[I2C_PORT][i] = 0x00;
          }
  
          I2CWriteLength[I2C_PORT] = 3;
          I2CReadLength[I2C_PORT] = 0;
          I2CMasterBuffer[I2C_PORT][0] = MPU6050_ADDRESS;
          I2CMasterBuffer[I2C_PORT][1] = regAdd;
          I2CMasterBuffer[I2C_PORT][2] = regValue;
          I2CEngine(I2C_PORT);
      }
  </code></pre>