AN3291 do not works. I don´t know why.
void Init_I2C_c (void)
{
//IIC1C_IIC1EN = 1; // Enable I2C;
IIC1C_IICEN = 1;
IIC1C_TXAK = 1; // not generate ACK by master after transfer;
IIC1C_MST = 0; // Slave mode actually;
IIC1F = 0x59; // Set speed to 50kHz for Bus = 18.8743MHz;
// 12.5k->0x39; 50k->0x99; 100k->0x59;
IIC1S_SRW = 0; // R/W bit = 0;
}
void IIC_write_byte_c(byte data, word addr)
{
Address = addr; // load address;
temp = (Addr_H & 0x07) << 1;
IIC1C_TXAK = 0; // RX/TX = 1; MS/SL = 1; TXAK = 0;
IIC1C |= 0x30; // And generate START condition;
//-------start of transmit first byte to IIC bus-----
IIC1D = IIC_SLAVE | temp; // Address the slave and set up for master transmit;
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
while(IIC1S_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC1D = Addr_L; // Send low byte of the word address;
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
while(IIC1S_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC1D = data;
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
while(IIC1S_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC1S_IICIF=1; // clear the interrupt event flag;
IIC1C_MST = 0; // generate STOP condition;
}
byte IIC_read_byte_c(word addr)
{
Address = addr;
temp = (Addr_H & 0x07) << 1;
IIC1C_TXAK = 0; // RX/TX = 1; MS/SL = 1; TXAK = 0;
IIC1C |= 0x30; // And generate START condition;
IIC1D = IIC_SLAVE | temp; // Address the slave and set up for master transmit;
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
while(IIC1S_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC1D = Addr_L; // Send low byte of word address;
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
while(IIC1S_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC1C_RSTA = 1; // set up repeated start;
IIC1D = IIC_SLAVE | temp | 1; // (slave_address) | (RW = 1);
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
while (IIC1S_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IIC1C_TX = 0; // set up to receive;
IIC1C_TXAK = 1; // acknowledge disable;
RD_data = IIC1D; // dummy read;
while (!IIC1S_IICIF); // wait until IBIF;
IIC1S_IICIF=1; // clear the interrupt event flag;
IIC1C_MST = 0; // generate stop signal;
RD_data = IIC1D; // read right data;
return RD_data;
}
void main( void )
{
/* Init MCU internal peripherals */
byte dato = 10;
byte index = 0;
MCU_Init();
RPMRatio=2500000;
//Delay( 100 );
//Delay( 100 );
Band.stop=1;
Estado=SAVE;
InitSCI();
SendChar('O') ;
SendChar('K') ;
SendChar(CR) ;
SendChar(LF) ;
Addr_H = 0;
Addr_L = 0;
Init_I2C_c();
/*IIC_write_byte('2',2);
SendChar('W') ;
SendChar('R') ;
SendChar(CR) ;
SendChar(LF) ; */
for(index = 0;index < 16;index++) {
DBUF[index] = 'a' + index;
}
IIC_write_byte_c('s',0);
for(index = 0;index < 16;index++) {
DBUF[index] = 0;
}
dato = IIC_read_byte_c(0) ;
SendChar(dato);
for(index =0;index < 16;index++) {
SendChar(DBUF[index] + 0x30);
}
SendChar('R') ;
SendChar('D') ;
SendChar(CR) ;
SendChar(LF) ;
asm cli
//****************************************
for(;
{
}
}