I am currently trying to program the I2C to communicate with an EEPROM 24LC64F, with its pin A0, A1 and A2 to GND, 4.7k ohms on SDA and SCL pins, 5V on VCC.
I currently have these lines of code :
I2C.h
#define ControlRead 0xA0
#define ControlWrite 0xA1
#define AddrDebMoy_Low 0x00
#define AddrDebMoy_High 0x01
#define AddrHeureFct_Low 0x02
#define AddrHeureFct_High 0x03
void I2C_Initialize(void);
void I2CStart(void);
void I2CStop(void);
void I2CRestart(void);
void I2CAck(void);
void I2CNak(void);
void I2CWait(void);
void I2CSend(unsigned char dat);
unsigned char I2CRead(void);
unsigned char I2CAction(unsigned char data, unsigned char addr, bool read);
I2C.c
#include "system.h"
void I2C_Initialize()
{
SSP1ADD = 0x13; // 400Khz @32MHz
SSP1CON1 = 0x28; // I2C Enabled Host Mode
SSP1CON2 = 0x00; // Default Settings
SSP1CON3 = 0x00; // Default Settings
SSP1STAT = 0x00; // Default Settings
}
void I2CStart()
{
SSP1CON2bits.SEN = 1;
while(SSP1CON2bits.SEN);
}
void I2CStop()
{
SSP1CON2bits.PEN = 1;
while(SSP1CON2bits.PEN);
}
void I2CRestart()
{
SSP1CON2bits.RSEN = 1;
while(SSP1CON2bits.RSEN);
}
void I2CAck()
{
SSP1CON2bits.ACKDT = 0;
SSP1CON2bits.ACKEN = 1;
while(SSP1CON2bits.ACKEN);
}
void I2CNak()
{
SSP1CON2bits.ACKDT = 1;
SSP1CON2bits.ACKEN = 1;
while(SSP1CON2bits.ACKEN);
}
void I2CWait()
{
while ((SSP1CON2 & 0x1F ) || ( SSP1STAT & 0x04 ) );
}
void I2CSend(unsigned char dat)
{
SSP1BUF = dat;
while(SSP1STATbits.BF);
I2CWait();
}
unsigned char I2CRead()
{
unsigned char temp;
SSP1CON2bits.RCEN = 1;
while(!SSP1CON2.BF);
temp = SSP1BUF;
I2CWait();
return temp;
}
unsigned char I2CAction(unsigned char data, unsigned char addr, bool read)
{
I2CStart();
if(read){
I2CSend(ControlWrite);
I2CSend(0x00);
I2CSend(addr);
I2CRestart();
I2CSend(ControlRead);
data = I2CRead();
I2CNak();
} else {
I2CSend(ControlWrite);
I2CSend(0x00);
I2CSend(addr);
I2CSend(data);
}
I2CStop();
return data;
}
The problem I encounter is the following, when I launch the reading, I get stuck at the line "while(!SSP1CON2.BF);" and the microcontroller restarts ... it seems that I have no data in the buffer ... I think I have set up the I2C registers and the PPS as below :
/* RB4 <-> SDA1 */
RB4PPS = 0x16;
SSP1DATPPS = 0x0C;
/* RB6 <-> SCL1 */
RB6PPS = 0x15;
SSP1CLKPPS = 0x0E;
Do you have any idea where the problem might come from?
Bests regards.