I2C code help

Discussion in 'Programmer's Corner' started by RoboticFan87, Nov 16, 2009.

  1. RoboticFan87

    Thread Starter Member

    Sep 12, 2009
    38
    0
    Hey guys, can any1 help me with the I2C send and receive code for the PIC32. Im a newbie with that any help is appreciated!
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    The code I sent is code for reading and writing to an I2C device, you just have to call the main I2C_Init(void) function to initialize the Pic for I2C comms, (don't forget to set which I2C you are using on the pic I2C1 or I2C2 in the OpenI2Cn line, right now it is coded for I2C1) and the I2C_Read_Register and I2C_Write_Register takes care of all the start and stop sequences required to communicate with the device......

    I will post the code here and see if anyone thinks or knows different, but this is the code I used in my B.U.M. System to communicate with an I2C Touch Pad controller (MPR084Q)....

    Here is the C code.... You might want to change this line to match where you are placing these files....

    Code ( (Unknown Language)):
    1. #include "C:\MDD File System\Microchip\Include\I2C.h"

    Code ( (Unknown Language)):
    1. /*
    2. ** I2C.c
    3. ** Brent Morse
    4. ** 08/08/2008
    5. */
    6. //##############################################################################
    7. #include <p32xxxx.h>                // Processor defs
    8. #include <plib.h>                    // Peripheral Library's
    9. #include "C:\MDD File System\Microchip\Include\I2C.h"                //
    10. //##############################################################################
    11. #define SYSCLK            (72000000)
    12. #define PBCLK              (SYSCLK/2)
    13. #define Fsck            50000
    14. #define BRG_VAL            ((PBCLK/2/Fsck)-2)
    15. //*****************************************************************************************************************************
    16. #define Nop() asm( "nop" )
    17. unsigned char SlaveAddress;
    18. char i2cDat[10];
    19. int DatSz;
    20. char i2cbyte;
    21. //*****************************************************************************************************************************
    22. void I2C_Init(void)
    23. {
    24.     OpenI2C1( I2C_EN | I2C_7BIT_ADD, BRG_VAL );      
    25. }
    26. //*****************************************************************************************************************************
    27. void I2C_Write_register(int mAddress,int mRegister,int mData)
    28. {
    29.     i2cDat[0] = (mAddress << 1) | 0;    //Device Address and WR Command
    30.     i2cDat[1] = mRegister;                //Register Address
    31.     i2cDat[2] = mData;                    //Config Data
    32.     DatSz = 3;
    33.        
    34.     StartI2C1();    //Send the Start Bit
    35.     IdleI2C1();        //Wait to complete
    36.     //
    37.     int Index = 0;
    38.     while( DatSz )
    39.     {
    40.         MasterWriteI2C1( i2cDat[Index++] );
    41.         IdleI2C1();        //Wait to complete
    42.  
    43.         DatSz--;
    44.  
    45.         //ACKSTAT is 0 when slave acknowledge. if 1 then slave has not acknowledge the data.
    46.         if( I2C1STATbits.ACKSTAT )
    47.             break;
    48.     }
    49.     //now send a start sequence again
    50.     RestartI2C1();    //Send the Restart condition
    51.     //wait for this bit to go back to zero
    52.     IdleI2C1();    //Wait to complete
    53.     Index = 0;
    54.     DatSz = 3;
    55.     while( DatSz )    //resend data.....
    56.     {
    57.         MasterWriteI2C1( i2cDat[Index++] );
    58.         IdleI2C1();        //Wait to complete
    59.         DatSz--;
    60.         //ACKSTAT is 0 when slave acknowledge. if 1 then slave has not acknowledge the data.
    61.         if( I2C1STATbits.ACKSTAT )
    62.             break;
    63.     }
    64.     StopI2C1();    //Send the Stop condition
    65.     IdleI2C1();    //Wait to complete
    66. }
    67. //*****************************************************************************************************************************
    68. int I2C_Read_Register(int mI2CslaveAddress, int mRegisterAddress)
    69. {
    70.     // Now Readback the data from the I2C Device
    71.     i2cDat[0] = (mI2CslaveAddress << 1) | 0;    //Device Address and WR Command
    72.     i2cDat[1] = mRegisterAddress;    //Register
    73.     DatSz = 2;
    74.  
    75.     StartI2C1();    //Send the Start Bit
    76.     IdleI2C1();        //Wait to complete
    77.  
    78.     //
    79.     int Index = 0;
    80.     while( DatSz )
    81.     {
    82.         MasterWriteI2C1( i2cDat[Index++] );
    83.         IdleI2C1();        //Wait to complete
    84.         DatSz--;
    85.         //ACKSTAT is 0 when slave acknowledge. if 1 then slave has not acknowledge the data.
    86.         if( I2C1STATbits.ACKSTAT )
    87.             break;
    88.     }
    89.  
    90.     //now send a start sequence again
    91.     RestartI2C1();    //Send the Restart condition
    92.     //wait for this bit to go back to zero
    93.     IdleI2C1();    //Wait to complete
    94.     MasterWriteI2C1( (mI2CslaveAddress << 1) | 1 ); //transmit read command
    95.     IdleI2C1();        //Wait to complete
    96.  
    97.     i2cbyte = MasterReadI2C1();
    98.     return (i2cbyte);
    99.     StopI2C1();    //Send the Stop condition
    100.     IdleI2C1();    //Wait to complete
    101.  
    102. }
    103. //*****************************************************************************************************************************
    104. int I2C_Wait(unsigned int cnt)
    105. {
    106.     while(--cnt)
    107.     {
    108.     Nop();
    109.     Nop();
    110.     }
    111. }//I2C Wait
    112. //*****************************************************************************************************************************
    113.  
    and here is the header file ...

    Code ( (Unknown Language)):
    1. /***********************************************************************************
    2. I2C.h - I2C routines header file
    3.  ***********************************************************************************
    4. By Brent Morse
    5.  ***********************************************************************************
    6.  * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
    7.  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
    8.  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    9.  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. BRENT MORSE SHALL NOT,
    10.  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
    11.  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
    12.  ***********************************************************************************
    13.  *
    14.  ***********************************************************************************
    15.  ***********************************************************************************/
    16. #ifndef _I2C_HEADER_FILE
    17. #define _I2C_HEADER_FILE
    18. //*******************************************************************************
    19. /**
    20.  * \brief   This function writes a register on the I2C Device
    21.  * \author  Brent Morse
    22.  * \param   RegisterAddress Address of the register to write
    23.  * \param   Data Byte to write on the register
    24.  * \return  void
    25.  */
    26. void I2C_Write_register(int mAddress,int mRegister,int mData);
    27. //------------------------------------------------------------------------------------------------------
    28. /**
    29.  * \brief   This function reads a register of the I2C Device
    30.  * \author  Brent Morse
    31.  * \param   RegisterAddress Address to read.
    32.  * \return  Read value
    33.  */
    34. int I2C_Read_Register(int mI2CslaveAddress, int mRegisterAddress);
    35. //------------------------------------------------------------------------------------------------------
    36. /**
    37.  * \brief   This function enables the I2C peripheral
    38.  * \author  Brent Morse
    39.  * \param   RegisterAddress Address to read.
    40.  * \return  Read value
    41.  */
    42. void I2C_Init(void);
    43. //------------------------------------------------------------------------------------------------------
    44.  
    45. #endif      //__I2C
    46.  
    What problems are you having?? Did you download the Microchip directory file I mentioned in the PM reply?? That should fix your "missing files" problem with the USB/HID stack.....

    Anyways if someone else has a better way.....

    My .02
     
  3. RoboticFan87

    Thread Starter Member

    Sep 12, 2009
    38
    0
    i called I2C_Init() in the main right above the UserInit(). So now the I2C comm. are initialized for the PIC.

    Where do i call the read and write functions of the I2C?
    Do i call them in the parse_SI_packet(void) and parse_RI_packet(void)?
    and in the parse_CI_packet(void) do i call I2C_init() again? or leave it blank?

    Thanks
     
  4. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    you can place them just about anywhere you want to read/write the data when appropriate.....
     
Loading...