// write to I2C2 address array wrptr of length and array wrptr1 of length1, if sendStop is 1 send a STOP at end
int writeI2C2array(const int address, const unsigned char *wrptr, int length,const unsigned char *wrptr1, int length1, int sendStop)
{
/* Baud rate is set for 100 Khz */
unsigned int i2cbrg=((SYSCLK/100000L)-(SYSCLK/1111111L))-1;
//This function will initialize the I2C(1) peripheral.
//First set the I2C(1) BRG Baud Rate.
I2C2BRG = i2cbrg;//0x004f;
//Now we will initialise the I2C peripheral for Master Mode, No Slew Rate
//Control,ACK enable and leave the peripheral switched off.
I2C2CON = 0x1210;//0x1200;
I2C2RCV = 0x0000;
I2C2TRN = 0x0000;
//Now we can enable the peripheral
I2C2CON = /*0x8210;*/0x9200;
//printf("\nwrite I2C2 %#x %0x I2C stat %0x \n", address,i2cbrg,I2C2STAT);
IdleI2C2();
StartI2C2(); /* send START */
while(I2C2CONbits.SEN ); /* Wait till Start sequence is completed */
if(MasterWriteI2C2(I2C2address=address) < 0)
{ printf("MasterWriteI2C2 Write Slave address fail!\n"); /*while(1);*/ return -1;} /* Write Slave address and set master for transmission */
if(MasterPutArrayI2C2(wrptr, length) < 0) return -1; /* Transmit string of data */
if(MasterPutArrayI2C2(wrptr1, length1) < 0) return -1;/* Transmit string of data */
if(!sendStop) return 0;
StopI2C2(); /* send STOP */
while(I2C2CONbits.PEN); /* Wait till stop sequence is completed */
CloseI2C2();
return 0;
}