Jean-Francois Bilodeau
New Member
I'm facing a weird issue. I've always used bit bangin I2C functions on my PIC16F1459, but now I want to use the MSSP (SPI,I2C Master Slave Peripheral). So I've started writing the functions according to the datasheet, Start, Stop, etc. The problem I have is my PIC won't ACK the data I send to the I2C EEPROM. It clearly says in the datasheet that the ACK status can be found at SSPCON2.ACKSTAT. So my guess was to poll this bit until the slave responds to my data, but the program hangs in the while Loop.
void vReadACK (void)
{
while(SSPCON2.ACKSTAT !=0);
}
And here's my write function, my I2CCheck function and I2C Master Initialization function
Just so you know, 24LC32A WriteProtect tied to VSS, A2-A1-A0 tied to GND, so adress 0xA0. 4k7 pull-ups are on I2C line. PIC16F1459 at 16MHz INTOSC. Writing a byte works, same for start and stop.
I'm completely stuck. I've went through the MSSP datasheet 5 to 6 times without finding any issue. Can you guys help?
void vReadACK (void)
{
while(SSPCON2.ACKSTAT !=0);
}
void vI2CEcrireOctet (UC ucData, UC ucRW)
{
vI2CCheck();
switch(ucRW)
{
case READ:
SSPBUF = ucData +1;
break;
case WRITE:
SSPBUF = ucData +0;
break;
}
vReadACK();
}
void vI2CCheck (void)
{
while(SSPCON2.ACKEN);//ACKEN not cleared, wait
while(SSPCON2.RCEN);//RCEN not cleared, wait
while(SSPCON2.PEN);//STOP not cleared, wait
while(SSPCON2.SEN);//Start not cleared, wait
while(SSPCON2.RSEN);//Rep start not cleared, wait
while(SSP1STAT.R_NOT_W);//TX not done wait
}
void vInitI2CMaster (void)
{
TRISB4_bit =1;//SDA IN
TRISB6_bit =1;//SCL IN
SSP1STAT.SMP =1;//No slew rate
SSP1STAT.CKE =0;//Disable SMBus inputs
SSPADD =0x27;//100 KHz
SSPCON1 =0b00101000;//I2C Master mode
SSPCON3 =0b00000000;//Nothing slave
}
{
vI2CCheck();
switch(ucRW)
{
case READ:
SSPBUF = ucData +1;
break;
case WRITE:
SSPBUF = ucData +0;
break;
}
vReadACK();
}
void vI2CCheck (void)
{
while(SSPCON2.ACKEN);//ACKEN not cleared, wait
while(SSPCON2.RCEN);//RCEN not cleared, wait
while(SSPCON2.PEN);//STOP not cleared, wait
while(SSPCON2.SEN);//Start not cleared, wait
while(SSPCON2.RSEN);//Rep start not cleared, wait
while(SSP1STAT.R_NOT_W);//TX not done wait
}
void vInitI2CMaster (void)
{
TRISB4_bit =1;//SDA IN
TRISB6_bit =1;//SCL IN
SSP1STAT.SMP =1;//No slew rate
SSP1STAT.CKE =0;//Disable SMBus inputs
SSPADD =0x27;//100 KHz
SSPCON1 =0b00101000;//I2C Master mode
SSPCON3 =0b00000000;//Nothing slave
}
Just so you know, 24LC32A WriteProtect tied to VSS, A2-A1-A0 tied to GND, so adress 0xA0. 4k7 pull-ups are on I2C line. PIC16F1459 at 16MHz INTOSC. Writing a byte works, same for start and stop.
I'm completely stuck. I've went through the MSSP datasheet 5 to 6 times without finding any issue. Can you guys help?