#define _XTAL_FREQ 8000000
void I2C_Initialize(const unsigned long feq_K) //Begin IIC as master
{
TRISC.F3 = 1; TRISC.F4 = 1; //Set SDA and SCL pins as input pins
SSPCON = 0b00101000; //pg84/234
SSPCON2 = 0b00000000; //pg85/234
SSPADD = (_XTAL_FREQ/(4*feq_K*100))-1; //Setting Clock Speed pg99/234
SSPSTAT = 0b00000000; //pg83/234
}
void I2C_Hold()
{
while ((SSPCON2 & 0b00011111)||(SSPSTAT & 0b00000100)) ; //check the this on registers to make sure the IIC is not in progress
}
void I2C_Begin()
{
I2C_Hold(); //Hold the program is I2C is busy
SSPCON2.SEN = 1; //Begin IIC pg85/234
}
unsigned short I2C_Rd(unsigned short ack)
{
unsigned short incoming;
I2C_Hold();
SSPCON2.RCEN = 1;
I2C_Hold();
incoming = SSPBUF; //get the data saved in SSPBUF
I2C_Hold();
SSPCON2.ACKDT = (ack)?0:1; //check if ack bit received
SSPCON2.ACKEN = 1; //pg 85/234
return incoming;
}
void I2C_End()
{
I2C_Hold(); //Hold the program is I2C is busy
SSPCON2.PEN = 1; //End IIC pg85/234
}
Void I2C_Wrt(unsigned int dat)
{
I2C_Hold(); //Hold the program is I2C is busy
SSPBUF=dat;//pg82/234
}
void main()
{
ADCON1.PCFG3=1;
ADCON1.PCFG2=1;
ADCON1.PCFG1=1 ;
ADCON1.PCFG0=0 ;
PORTB = 0;
TRISB = 0; // Configure PORTB as output
I2C_Initialize(100); //Initialize I2C Master with 100KHz clock
while(1)
{
I2C_Begin();
I2C_Wrt(0xD0);
I2C_Wrt(0x88);
I2C_Wrt(0xFF);
I2C_End();
delay_ms(1000);
}
}