HerbertMunch
New Member
Im trying to connect a pic18f4550 via i2c to a microchip 24fc64 EEprom.
Ive written two methods, one called WriteEEByte, and one called ReadEEBytes.
The write one should write 10 bytes to the eeprom, the read should read 10.
It SEEMS to work when i call my method WriteEEByte, but im not sure. I am trying to verify that the write was a success but code xecution gets stuck in the ReadI2C method.
It seems that the SSPIF bit never gets set to 1, so it just loops and loops.
Thanks for any help anyone can give.
Here is my I2C code.
Ive written two methods, one called WriteEEByte, and one called ReadEEBytes.
The write one should write 10 bytes to the eeprom, the read should read 10.
It SEEMS to work when i call my method WriteEEByte, but im not sure. I am trying to verify that the write was a success but code xecution gets stuck in the ReadI2C method.
It seems that the SSPIF bit never gets set to 1, so it just loops and loops.
Thanks for any help anyone can give.
Here is my I2C code.
Code:
//I2C
#include <p18f4550.h>
#define SSPIF PIR1bits.SSPIF //Transmittion/receive complete if 1
#define ACKSTAT SSPCON2bits.ACKSTAT //Acknowledgement, 1 if no acknowledge from slave
#define RCEN SSPCON2bits.RCEN //reception enabled --Master receive mode only
#define RSEN SSPCON2bits.RSEN //Restart enabled -- Cleared automatically
#define ACKEN SSPCON2bits.ACKEN //Transmit ack, ack or not ack depending on ACKDT bit
#define ACKDT SSPCON2bits.ACKDT //Determins whether (0) ACK or (1) NOT ack is sent to slave after receving data (MASTER TRANSMIT)
//Sending a not ack tells slave to stop sending
#define SEN SSPCON2bits.SEN //Creates a start condition (master) or holds the clock (slave)
#define PEN SSPCON2bits.PEN //Sends a stop bit
typedef char byte;
typedef char bool;
void IdleI2C( void );
//Check whether i2c bus is idle
void IdleI2C( void )
{
while ( ( SSPCON2 & 0x1F ) || ( SSPSTATbits.R_W ) )
continue;
}
bool WriteI2C(byte* sendByte);
void StopI2C();
void StartI2C();
void StartI2C()
{
IdleI2C();
SEN = 1;//Create start condition
while(SEN); //Wait while start condition sent
}
void StopI2C()
{
IdleI2C();
PEN =1;
while(PEN); //Wait while stop bit is sent
}
byte ReadI2C ();
void RestartI2C();
void RestartI2C()
{
IdleI2C();//Wait while i2c bus is idle
RSEN = 1;
while(RSEN);
}
byte ReadI2C ()
{
IdleI2C();//wait until idle
SSPIF =0;
RCEN = 1;
while(SSPIF==0);
SSPIF =0;
return SSPBUF;
}
bool WriteI2C(byte* sendByte)
{
IdleI2C();
SSPIF = 0; //Reset send complete interrupt
SSPBUF = *sendByte; //Move byte into buffer
while(SSPIF);//loop until sent
return !ACKSTAT;//ackstat is true if no ack for slave
}
void AckI2C(bool notAck);
//Sends an ack or a not ack depeding on the value of the parameter
void AckI2C(bool notAck)
{
ACKDT = notAck; //Set type of ack
ACKEN = 1; //Send acknowledge
while(ACKEN); //Wait until ack has been sent (bit cleared by hardware when done)
}
//Writes a byte to the specified location
bool WriteEEByte(byte address,byte* data)
{
byte i =0;
//Set address
SSPBUF = 0b10100000 ; //0 LSB = write
IdleI2C(); //wait until idle
WriteI2C(0x00);//Write higher address
WriteI2C(&address); //Write lower address
for (i=0;i<10;i++)
{
WriteI2C(data);//Write 10 bytes of data to the eeprom
}
StopI2C(); //Send stop
}
byte readBytes[10];
void ReadEEBytes(byte* address)
{
byte* dest=readBytes;
bool result=0;
byte i =0;
//Clear memory
for (i =0;i<10;i++)
*dest++ = 0;
dest =readBytes;
//Set address
SSPBUF = 0b10100000 ; //0 LSB = write
result=WriteI2C(0x00);//Write higher address
result=WriteI2C(&address); //Write lower address
RestartI2C();//Send start condition
SSPBUF = 0b10100001 ; //1 LSB = read
//now read 10 bytes
for (i=0;i<10;i++)
{
*dest++ = ReadI2C(); //Store byte in buffer
AckI2C(0); //Signal slave to send more
}
StopI2C(); //Send recieve
dest =0;
}
Last edited: