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!
#include "C:\MDD File System\Microchip\Include\I2C.h"
/*
** I2C.c
** Brent Morse
** 08/08/2008
*/
//##############################################################################
#include <p32xxxx.h> // Processor defs
#include <plib.h> // Peripheral Library's
#include "C:\MDD File System\Microchip\Include\I2C.h" //
//##############################################################################
#define SYSCLK (72000000)
#define PBCLK (SYSCLK/2)
#define Fsck 50000
#define BRG_VAL ((PBCLK/2/Fsck)-2)
//*****************************************************************************************************************************
#define Nop() asm( "nop" )
unsigned char SlaveAddress;
char i2cDat[10];
int DatSz;
char i2cbyte;
//*****************************************************************************************************************************
void I2C_Init(void)
{
OpenI2C1( I2C_EN | I2C_7BIT_ADD, BRG_VAL );
}
//*****************************************************************************************************************************
void I2C_Write_register(int mAddress,int mRegister,int mData)
{
i2cDat[0] = (mAddress << 1) | 0; //Device Address and WR Command
i2cDat[1] = mRegister; //Register Address
i2cDat[2] = mData; //Config Data
DatSz = 3;
StartI2C1(); //Send the Start Bit
IdleI2C1(); //Wait to complete
//
int Index = 0;
while( DatSz )
{
MasterWriteI2C1( i2cDat[Index++] );
IdleI2C1(); //Wait to complete
DatSz--;
//ACKSTAT is 0 when slave acknowledge. if 1 then slave has not acknowledge the data.
if( I2C1STATbits.ACKSTAT )
break;
}
//now send a start sequence again
RestartI2C1(); //Send the Restart condition
//wait for this bit to go back to zero
IdleI2C1(); //Wait to complete
Index = 0;
DatSz = 3;
while( DatSz ) //resend data.....
{
MasterWriteI2C1( i2cDat[Index++] );
IdleI2C1(); //Wait to complete
DatSz--;
//ACKSTAT is 0 when slave acknowledge. if 1 then slave has not acknowledge the data.
if( I2C1STATbits.ACKSTAT )
break;
}
StopI2C1(); //Send the Stop condition
IdleI2C1(); //Wait to complete
}
//*****************************************************************************************************************************
int I2C_Read_Register(int mI2CslaveAddress, int mRegisterAddress)
{
// Now Readback the data from the I2C Device
i2cDat[0] = (mI2CslaveAddress << 1) | 0; //Device Address and WR Command
i2cDat[1] = mRegisterAddress; //Register
DatSz = 2;
StartI2C1(); //Send the Start Bit
IdleI2C1(); //Wait to complete
//
int Index = 0;
while( DatSz )
{
MasterWriteI2C1( i2cDat[Index++] );
IdleI2C1(); //Wait to complete
DatSz--;
//ACKSTAT is 0 when slave acknowledge. if 1 then slave has not acknowledge the data.
if( I2C1STATbits.ACKSTAT )
break;
}
//now send a start sequence again
RestartI2C1(); //Send the Restart condition
//wait for this bit to go back to zero
IdleI2C1(); //Wait to complete
MasterWriteI2C1( (mI2CslaveAddress << 1) | 1 ); //transmit read command
IdleI2C1(); //Wait to complete
i2cbyte = MasterReadI2C1();
return (i2cbyte);
StopI2C1(); //Send the Stop condition
IdleI2C1(); //Wait to complete
}
//*****************************************************************************************************************************
int I2C_Wait(unsigned int cnt)
{
while(--cnt)
{
Nop();
Nop();
}
}//I2C Wait
//*****************************************************************************************************************************
/***********************************************************************************
I2C.h - I2C routines header file
***********************************************************************************
By Brent Morse
***********************************************************************************
* THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. BRENT MORSE SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
***********************************************************************************
*
***********************************************************************************
***********************************************************************************/
#ifndef _I2C_HEADER_FILE
#define _I2C_HEADER_FILE
//*******************************************************************************
/**
* \brief This function writes a register on the I2C Device
* \author Brent Morse
* \param RegisterAddress Address of the register to write
* \param Data Byte to write on the register
* \return void
*/
void I2C_Write_register(int mAddress,int mRegister,int mData);
//------------------------------------------------------------------------------------------------------
/**
* \brief This function reads a register of the I2C Device
* \author Brent Morse
* \param RegisterAddress Address to read.
* \return Read value
*/
int I2C_Read_Register(int mI2CslaveAddress, int mRegisterAddress);
//------------------------------------------------------------------------------------------------------
/**
* \brief This function enables the I2C peripheral
* \author Brent Morse
* \param RegisterAddress Address to read.
* \return Read value
*/
void I2C_Init(void);
//------------------------------------------------------------------------------------------------------
#endif //__I2C