10 Replies Latest reply on Oct 29, 2012 12:30 PM by srihari.s

    Working with I2C

    srihari.s

       This is my first I2C project. Seems like something going wrong. Slave is not receiving correct data. The code is provided below for both master and slave. Can  anyone debug

         

      Master code

         

       

         

      */

         

      #include <device.h>

         

      uint8 wrData[2]={0xAA, 0xBB};

         

      extern uint8 flag;

         

       

         

      void main()

         

      {

         

          /* Place your initialization/startup code here (e.g. MyInst_Start()) */

         

          CyGlobalIntEnable; 

         

          isr_Tx_Start();

         

          LCD_Start();

         

          I2C_Start();

         

          /* Uncomment this line to enable global interrupts. */

         

          for(;;)

         

          {

         

              if(flag==1)

         

              {

         

                  flag=0;

         

                 I2C_MasterClearStatus(); /* Clear any previous status */

         

                  I2C_MasterWriteBuf(0x08, (uint8 *) wrData, 2, I2C_MODE_COMPLETE_XFER);

         

                  for(;;)

         

                  {

         

                     if(0u != (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT))

         

                      {

         

                      /* Transfer complete. Check Master status to make sure that transfer

         

                      completed without errors. */

         

                      break;

         

                      }

         

                  } 

         

                    LCD_PrintInt8(wrData[0]);  

         

                

         

              }

         

              /* Place your application code here. */

         

          }

         

      }

         

       

         

      /* [] END OF FILE */

         

       

         

      Slave code

         

       

         

       

         

      #include <device.h>

         

       

         

      void main()

         

      {

         

      uint8 i;

         

      uint8 wrBuf[10];

         

      uint8 userArray[10];

         

      uint8 byteCnt;

         

          CyGlobalIntEnable;

         

          /* Initialize write buffer before call I2C_Start */

         

          I2C_SlaveInitWriteBuf((uint8 *) wrBuf, 10);

         

          LCD_Start();

         

          /* Start I2C Slave operation */

         

          I2C_Start();

         

          /* Wait for I2C master to complete a write */

         

          for(;;) /* loop forever */

         

          {

         

              /* Wait for I2C master to complete a write */

         

              if(0u != (I2C_SlaveStatus() & I2C_SSTAT_WR_CMPLT))

         

              {

         

                  byteCnt = I2C_SlaveGetWriteBufSize();

         

                  I2C_SlaveClearWriteStatus();

         

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

         

                  {

         

                       userArray[i] = wrBuf[i]; /* Transfer data */

         

                  }

         

                  I2C_SlaveClearWriteBuf();

         

                  LCD_PrintInt8(userArray[0]);

         

              }

         

          }

         

      }

         

      /* [] END OF FILE */