ep.hobbyiest
Full Member level 4
Hi,
I have connected eeprom via i2c.(RC3 and RC4), pulled up with 1.8Kohm resistor. but driver is not working. I am getting 0xFF while reading.
I initilized with I2C_Master_Init(100000);
I am using 16f877 and XC8 compiler.
EEPROM - 24c512
what could be the reason.
I have connected eeprom via i2c.(RC3 and RC4), pulled up with 1.8Kohm resistor. but driver is not working. I am getting 0xFF while reading.
Code:
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
extern uint8_t readeee;
void I2C_Master_Init(const unsigned long c)
{
SSPCON2 = 0;
SSPADD = (_XTAL_FREQ/(4*c))-1;
SSPSTAT = 0;
TRISC3 = 1; //Setting as input as given in datasheet
TRISC4 = 1; //Setting as input as given in datasheet
SSPCON = 0b00111000;
NOP();
NOP();
}
void I2C_Master_Wait()
{
while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));
}
void I2C_Master_Start()
{
I2C_Master_Wait();
SEN = 1;
}
void I2C_Master_RepeatedStart()
{
I2C_Master_Wait();
RSEN = 1;
}
void I2C_Master_Stop()
{
I2C_Master_Wait();
PEN = 1;
}
void I2C_Master_Write(unsigned char d)
{
I2C_Master_Wait();
SSPBUF = d;
}
unsigned char I2C_Master_Read(unsigned short a)
{
unsigned short temp;
I2C_Master_Wait();
RCEN = 1;
I2C_Master_Wait();
temp = SSPBUF;
I2C_Master_Wait();
ACKDT = (a)?0:1;
ACKEN = 1;
return temp;
}
void ewrite24c512(void)
{
I2C_Master_Start();
I2C_Master_Write(0xA0);
I2C_Master_Write(0);
I2C_Master_Write(0);
I2C_Master_Write('Z');
I2C_Master_Stop();
}
void eread24c512(void)
{
I2C_Master_Start();
I2C_Master_Write(0xA0);
I2C_Master_Write(0);
I2C_Master_Write(0);
I2C_Master_Start();
I2C_Master_Write(0xA1);
readeee = I2C_Master_Read(0);
I2C_Master_Stop();
}
I initilized with I2C_Master_Init(100000);
I am using 16f877 and XC8 compiler.
EEPROM - 24c512
what could be the reason.