EEPROM 24LC256

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

EEPROM 24LC256

Postby airilharith » Thu Sep 13, 2012 4:43 pm

Hi,

I use the external EEPROM 24LC256 (with resistors 10kohm at SDA and SCL line) with PIC16F877A. My problem is cannot sent/receive the data. Anyone can help me to solve this problem. Below is the program that I use:
CODE: SELECT_ALL_CODE
#include <pic.h>
__CONFIG ( 0x3F32 );

#define LED   RB6
void delay(unsigned long data);
void i2c_init(void);
void i2c_write(unsigned char slave_address, unsigned char addressH, unsigned char addressL, unsigned char uc_data);
unsigned char ee_data;
unsigned char b_i2c_check_error_flag(void);
unsigned char i2c_read(unsigned char slave_address, unsigned char addressH, unsigned char addressL);
unsigned char error_flag = 0;


void main(void)
{
   PORTC=0x00;
   PORTB=0x00;
   TRISB=0x00;
   TRISC=0xFF;
   i2c_init();

   i2c_write(10,0,1,8);
   delay(10000);
   i2c_read(10,0,1);
   delay(1000);   

   while(1)
   {
      if(ee_data==8)
      {
         LED=1;
         delay(10000);
         LED=0;
         delay(10000);
      }
      else
      LED=0;
   }
}

void delay(unsigned long data)
{
   for( ;data>0;data-=1);
}


void i2c_init(void)
{

   TRISC3=1;
   TRISC4=1;
   SSPCON=0b00101000;
   SSPCON2=0x00;
   SSPADD=12;
   SSPSTAT=0b11000000;               
}   


unsigned char b_i2c_check_error_flag(void)
{
   return error_flag;   
}   


// I2C READ
unsigned char i2c_read(unsigned char slave_address, unsigned char addressH, unsigned char addressL)
{

   error_flag = 0;
   
   SEN = 1;
   while (SEN == 1);

   SSPBUF = (slave_address << 1) & 0xfe;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return 0;
   }
   

   SSPBUF = addressH;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return 0;
   }
   
   SSPBUF = addressL;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return 0;
   }
   
   RSEN = 1;
   while (RSEN == 1);

   SSPBUF = (slave_address << 1) | 0x01;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return 0;
   }
   
   RCEN = 1;
   
   unsigned long count = 10000L;
   while (BF == 0)
   {
      if (--count == 0)
      {
         PEN = 1;
         while (PEN == 1);
         error_flag = 1;
         return 0;
      }
   }

   SSPBUF=ee_data;
   
   ACKDT = 1;
   ACKEN = 1;
   while (ACKEN == 1);
   
   PEN = 1;
   while (PEN == 1);
   
   error_flag = 0;
   return ee_data;
}



// I2C WRITE

void i2c_write(unsigned char slave_address, unsigned char addressH, unsigned char addressL, unsigned char uc_data)
{

   error_flag = 0;
   
   SEN = 1;
   while (SEN == 1);
   

   SSPBUF = (slave_address << 1) & 0xfe;
   
   while (RW == 1);

   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return;
   }
   

   SSPBUF = addressH;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return;
   }

   SSPBUF = addressL;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return;
   }   

   SSPBUF = uc_data;
   while (RW == 1);
   if (ACKSTAT == 1)
   {
      PEN = 1;
      while (PEN == 1);
      error_flag = 1;
      return;
   }
   
   PEN = 1;
   while (PEN == 1);
   
   error_flag = 0;
}
airilharith
Newbie
 
Posts: 8
Joined: Thu Sep 13, 2012 4:01 pm

Re: EEPROM 24LC256

Postby airilharith » Tue Sep 18, 2012 8:39 am

help... anyone can advice me.... pls :)
airilharith
Newbie
 
Posts: 8
Joined: Thu Sep 13, 2012 4:01 pm

Re: EEPROM 24LC256

Postby shahrul » Sun Sep 23, 2012 7:31 pm

I never use. I use 25LC1024.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: EEPROM 24LC256

Postby airilharith » Mon Sep 24, 2012 8:44 am

Hi Shahrul,

It look that 24LC1024 using SPI. Do u experience using I2C? I'm confuse to use the protocol when read the datasheet.
TQ
airilharith
Newbie
 
Posts: 8
Joined: Thu Sep 13, 2012 4:01 pm

Re: EEPROM 24LC256

Postby shahrul » Mon Sep 24, 2012 8:52 am

airilharith WROTE:Hi Shahrul,

It look that 24LC1024 using SPI. Do u experience using I2C? I'm confuse to use the protocol when read the datasheet.
TQ

I2C I use for DS1307.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: EEPROM 24LC256

Postby airilharith » Mon Sep 24, 2012 12:07 pm

Shahrul,

How to decide the resistor value at SDA and SCL line? Mine is 10kohm (I refer to some other project in internet). Why do u use 1kohm? Is it all the I2C IC has different resistor value?
airilharith
Newbie
 
Posts: 8
Joined: Thu Sep 13, 2012 4:01 pm

Re: EEPROM 24LC256

Postby yonghui » Mon Sep 24, 2012 3:57 pm

did u check the datasheet how much value resistor to pullup? the eeprom datasheet and pic datasheet? mayb u try to use 2.7kohm?
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: EEPROM 24LC256

Postby shahrul » Mon Sep 24, 2012 4:42 pm

for long time I use 1k for pull-up, it's working for DS1307. I'm not test for other or for multiple slave.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: EEPROM 24LC256

Postby sich » Mon Sep 24, 2012 11:44 pm

About the resistor value, please look at I2C serial EEPROM datasheet, page 5, 2.2.
If you are using 400kHz data transfer rate or above, choose 2Kohm.

About why your code is not working, I can't give much advice since I've never use one of these before. It can be hardware or software issue. May be you should read through the datasheet again thoroughly.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm

Re: EEPROM 24LC256

Postby sich » Tue Sep 25, 2012 3:20 pm

You can try Microchip Forum too.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests