I am new to PIC. I know there are lot of posts in this forum on I2C and I have tried all the methods given but still doesn't solve my problem. I am using pic16f1516 and an external EEPROM CAT24C04TDI to store some data. I have used 4k7 ohm resistors for pull up and my code is :
void I2CWrite(void);
void WaitMSSP(void);
void I2CRead(void);
void i2c_init(void);
//void _delay_ms(unsigned int);
void main()
{
_delay_ms(100); // Give delay for power up
i2c_init(); // Initialize I2C
_delay_ms(20);
I2CWrite(); // Sends the data to I2C EEPROM
_delay_ms(50);
while(1)
{
I2CRead(); // Read back the data?s
TXREG='\n';
while(TXIF==0);
TXREG='\r';
_delay_ms(500);
}
}
void I2CWrite()
{
SSPCON2bits.SEN=1;
WaitMSSP(); // wait for the operation to be finished
SSPBUF=0xa0;//Send Slave address write command
WaitMSSP();
SSPBUF=0x00; // Send the starting address to write
WaitMSSP();
SSPBUF=0x34; // rough data to be written
WaitMSSP();
}
PEN=1; // Send stop bit
WaitMSSP();
}
void I2CRead()
{
SEN=1; //Send start bit
WaitMSSP(); //wait for the operation to be finished
SSPBUF=0xa0;//Send Slave address write command
WaitMSSP();
SSPBUF=0x00; // Send the starting address to write
WaitMSSP();
RSEN=1; // Send re-start bit
WaitMSSP();
SSPBUF=0xa1; // Slave address read command
WaitMSSP();
RCEN=1; // Enable receive
WaitMSSP();
ACKDT=1; // Acknowledge data 1: NACK, 0: ACK
ACKEN=1; // Enable ACK to send
PEN=1; // Stop condition
WaitMSSP();
putch(SSPBUF); // Send the received data to PC
_delay_ms(30);
}
PEN=1;
WaitMSSP();
}
void WaitMSSP()
{
while(!SSPIF); // while SSPIF=0 stay here else exit the loop
SSPIF=0; // operation completed clear the flag
}
void i2c_init()
{
TRISCbits.TRISC3=1; // Set up I2C lines by setting as input
TRISCbits.TRISC4=1;
SSPCON1 = 0b00101000;
// SSPADD=(FOSC / (4 * I2C_FREQ)) - 1; //clock 100khz
SSPADD = 0x18 ; //clock 100khz
SSPSTAT=80; // Slew rate control disabled
PIR1bits.SSPIF = 0; // Clear MSSP interrupt request flag
PIE1bits.SSPIE = 1; // Enable MSSP interrupt enable bit
}
When I connect it to oscilloscope, nothing happens at SDA and SCL ports after sending SEN=1. Please help me with this problem. I am stuck with this since long time.
PEN=1; WaitMSSP();}
(same both times) which sits outside the previous functions. – Weather Vane