Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

need help with i2c eeprom pic controller

Status
Not open for further replies.

ep.hobbyiest

Full Member level 4
Joined
Jul 24, 2014
Messages
212
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Activity points
1,487
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.

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.
 

Hi,

What did you do for debugging?
Scope pictures of SCL and SD..
What does the ACK do?

Klaus
 

I rechecked the hardware connection. It seems to be fine.
my oscilloscope is not working anymore.

Is there other method to debug?
 

1. Why do you need to write your own library if there a millions of examples available in google?
2. Why do you first wait the flag and then set it?
3. Briefly reviewed microchip's manual confused me - did you wrote your library according it?
 

yeah, first i wrote mine but that one was not working. So, i googled for i2c driver. I got above one.
Turns out that one also not working.

- - - Updated - - -

Hi,
Here is proteus simulated snapshot.



I could see, i am getting ACK. I can't fugure out how come i am receiving 0xFF?
 

Did you get ACK or NACK after the start condition? Are the address pins driven properly?
 

Problem was my sda pin got short to other pin which was causing to get 0xFF value.

But Now, Hardware problems are clear now and now i am able to read EEPROM but now RTC is behaving weird. Sometimes time is received and sometime it returns different value.

Code:
void readRTC(void)
{
    
    I2C_Master_Start();
    I2C_Master_Write(0xD0);
    I2C_Master_Write(0);
    I2C_Master_Restart();
    I2C_Master_Write(0xD1);
    rtcRaw.bytes[0] = I2C_Master_Read();
    rtcRaw.bytes[1] = I2C_Master_Read();
    rtcRaw.bytes[2] = I2C_Master_Read();
    rtcRaw.bytes[3] = I2C_Master_Read();
    rtcRaw.bytes[4] = I2C_Master_Read();
    rtcRaw.bytes[5] = I2C_Master_Read();
    rtcRaw.bytes[6] = I2C_Master_Read();
    I2C_Master_Stop();
}


void writeRTC(void)
{
    I2C_Master_Start();
    I2C_Master_Write(0xD0);
    I2C_Master_Write(0x00);
    I2C_Master_Write(rtcRaw.bytes[0]);
    I2C_Master_Write(rtcRaw.bytes[1]);
    I2C_Master_Write(rtcRaw.bytes[2]);
    I2C_Master_Write(rtcRaw.bytes[3]);
    I2C_Master_Write(rtcRaw.bytes[4]);
    I2C_Master_Write(rtcRaw.bytes[5]);
    I2C_Master_Write(rtcRaw.bytes[6]);
    I2C_Master_Stop();
}
 

In RTC read when the 5th byte is read you should request for NACK.
 

Yes, actually i was giving NACK after last read byte and ACK for all bytes before last read byte read. But then also it was not working.
I post the code which one i was modifying after that.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top