0
votes

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.

1
At a quick glance, you are enabling interrupts but you have no interrupt handlers. Your code appears to work by polling the interrupt flag (which is fine) but if that is the case you should not enable the interrupts in the INTCON register.Jon
I tried with removing INTCON register but still does not work..PicFan57
Your code won't compile due to mismatching braces. At lines 33 and 57 you have the code PEN=1; WaitMSSP();} (same both times) which sits outside the previous functions.Weather Vane
@WeatherVane: Thank you for your reply. I corrected it and I face the problem in the WaitMSSP() function. The compiler is stuck at line while(!SSPIF); . It seems that SSPIF is never getting set and it goes into infinite loop.PicFan57

1 Answers

0
votes

SSPIFis not a register: the data sheet shows it to be bit 3 in register PIR1 so instead of the line

while(!SSPIF); // while SSPIF=0 stay here else exit the loop
SSPIF = 0;     // operation completed clear the flag

this means you should to test and clear it with

while ((PIR1 & 0x08) == 0);
PIR1 = 0;

The assembler instruction to clear the status is

BCF PIR1,SSPIF ;I2C done, clear flag

and more detail of the code is in this forum also please read the data sheet for more information.