The system seemed to loose my reply, this is my 2nd attempt.
/////////////////////////////////////////////////////////////
// Init_I2C
// sets up the iic port according to the state
// selfAddress = slave address
//
void Init_I2C (unsigned char selfAddress)
{
SOPT2_IICPS = 1; //Set the IICPS bit to redirect IIc pins
IICC_IICEN = 1;//Enable IIC module
IICC_TXAK = 1;// SEND ACK
IICC_MST = 0;//SLAVE MODE
IICA = selfAddress << 1;//set slave address in IICA //PTB6=SDA PTB7=SCL
//set baud rate = BUSCLK/(2xMULTx(SCL Divider))
// MUX | ICR|
// \\ /////
IICF = 0b10010000;//MUX=2,ICR=0x16 Speed=100Khz
IICS_SRW = 0;//R/W bit = 0
IICC_IICIE = 0;//Disable IIC interrupt
}
//////////////////////////////////////////////////////////////////////////////////
// IIC_write_byte
// Writes command and data to slave
// returns 0 if success
// 1 if error
byte IIC_write_byte (byte slave, byte command, byte data)
{
byte temp;
byte timeout = 0xFF;
temp = (slave)<<1; // Shift 7 bit slave address to the left 1 bit
// Bit 0 = 0 = Write
// 0 = 1 = Read
IICC_TXAK = 0; // RX/TX = 1; MS/SL = 1; TXAK = 0;
IICC |= 0x30; // Generate START & set TX = 1
IICD = temp; // select slave & set up for master transmit
while (!IICS_IICIF); // wait until flag is set
IICS_IICIF=1; // Clear the flag
while((IICS_RXAK)&&(timeout > 0))
timeout --; // check for RXAK
if(timeout == 0) return 1;
IICD = command; // Send byte address
while (!IICS_IICIF); // wait until flag is set
IICS_IICIF=1; // Clear the flag
//while(IICS_RXAK); // check for RXAK
timeout = 0xFF;
while((IICS_RXAK)&&(timeout > 0))
timeout --; // check for RXAK
if(timeout == 0) return 1;
IICD = data;
while (!IICS_IICIF); // wait until flag is set
IICS_IICIF=1; // Clear the flag
//while(IICS_RXAK); // check for RXAK
timeout = 0xFF;
while((IICS_RXAK)&&(timeout > 0))
timeout --; // check for RXAK
if(timeout == 0) return 1;
IICS_IICIF=1; // Clear the flag
IICC_MST = 0; // Generate STOP
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
// IIC_read_byte
// Writes command to slave and returns data from slave
// returns 0 if success
// 1 if error
byte IIC_read_byte (byte slave, byte command)
{
byte RD_data;
byte temp;
temp = (slave)<<1; // Shift 7 bit slave address to the left 1 bit
// Lsb Bit = 0 = Write
// = 1 = Read
IICC_TXAK = 0;
IICC |= 0x30; // Generate START
IICD = temp; // Address slave | (RW = 0), write to slave
while (!IICS_IICIF); // wait until flag is set
IICS_IICIF=1; // Clear the int flag
while(IICS_RXAK); // Check for RXAK
IICD = command; // Send command to slave
while (!IICS_IICIF); // wait until flag is set
IICS_IICIF=1; // Clear the int flag
while(IICS_RXAK); // Check for RXAK
IICC_RSTA = 1; // setup repeated start
IICD = temp|1; // (slave_address)|(RW = 1), read from slave
while (!IICS_IICIF); // wait until flag is set
IICS_IICIF=1; // Clear the int flag
while(IICS_RXAK); // Check for RXAK
IICC_TX = 0; // Setup to receive
IICC_TXAK = 1; // Acknowledge disable
RD_data = IICD; // Dummy read
while (!IICS_IICIF); // Wait until IBIF flag is set
IICS_IICIF=1; // Clear the flag
IICC_MST = 0; // Generate STOP
RD_data = IICD; // Read data
return RD_data;
}
[[[Below is ione of my attempts at the slave read routine]]]
/////////////////////////////////////////////////////////////
// LL_Read()
byte LL_Read()
{
//ToggleNum(0x33);
//return 0x33;
//IICC_TX = 0;
//IICC_TXAK = 0; // Acknowledge enable
//IICS_IICIF=1;
//IICC_MST = 0;//SLAVE MODE
//while(IICS_TCF == 0)
// ToggleNum(IICS);
if(IICS_IAAS)
{
IICC_TX = IICS_SRW; //SRW 0= slave receive, 1=slave transmit
IICS_IAAS = 0; // Setup to receive
buffer[0] = IICD; // Dummy read to initiate the read data byte
buffer[1] = IICD;
return 1;
}
//RD_data2 = IICD; //read data
//IICS_IICIF=1; //Clear the flag
//IICC_MST = 0; //Generate STOP
return 0;
}