How to read/write data from 24c512 interfacing with pic16f887?

Status
Not open for further replies.
8051 has open drain I/O with weak pull-up, PIC has push-pull I/O. Bit banging with I2C works respectively different with PIC, must operate TRIS register. Why don't you use PIC hardware I2C interface (MSSP)?
 

Read/write data with Interfacing 24c512 with pic16f887

Eeprom adress needs 16 bits (if range over 255) => Adresse HI , Adresse LO .. to send to EEPROM

mikroC and 16F877 example .. uses unsigned int Adr

Code:
void Write_Byte_To_24LC256(unsigned int Adr, unsigned char cx)
{

I2C1_Start();
I2C1_Wr(EEPROM_32K_ADR );      // write
I2C1_Wr(Adr>>8);    // send data (data to be written)
I2C1_Wr(Adr& 0x00FF);
I2C1_Wr(cx);    // send data (data to be written)
I2C1_Stop();
Delay_ms(5);
 }

unsigned char Read_Byte_from_24LC256(unsigned int Adr)
{
unsigned char c3;
I2C1_Start();
I2C1_Wr(EEPROM_32K_ADR );  // write
I2C1_Wr(Adr>>8);    // send data (data to be written)
I2C1_Wr(Adr&0x00FF);
I2C1_Stop();
I2C1_Start();    // I2C1_Repeated_Start();
Delay_ms(1);

I2C1_Wr(EEPROM_32K_ADR+1 );  // Read
c3=I2C1_Rd(0u);    // data to be read)
I2C1_Stop();
return c3;   
}
 

I did both of this but it is not working. Is there any thing i can do so that i can read/write data and also pages from c 24c512?

- - - Updated - - -

This code is also not working. This is for 8051 but i want to covert in to use for my application then use. Plz show me correction that i have to do in this.

Code:
sbit SDA = PC^4; // Serial Data Address Input/Output
sbit SCL = PC^3; //        Serial Clock

//=****************************************************************************

void send_to_mem(char s_address, char s_data)
 {
   
    start_EEPROM();             // sending start condition to eeprom 
    send_EEPROM(0XA0);     // A0 = 10100000 = sending device address word for write
    acknowledge();
    send_EEPROM(s_address); // sending data address
    acknowledge();
    send_EEPROM(s_data);   // sending data 
    acknowledge();
    stop_EEPROM();              // sending stop condition to eeprom
        acknowledge();
        wait(100);
          
 }
//============================================================================
char get_from_mem(char s_address)
 {
    char i = 0;
//-------dummy write seq----+ word address------------------------------------
    start_EEPROM();             // sending start condition to eeprom 
    send_EEPROM(0XA0);     // sending A0 = 10100000 = device address word for write
    acknowledge();
    send_EEPROM(s_address); // sending data address
    acknowledge();
//----------------dummy over----------------------------------------------------


    start_EEPROM();
    send_EEPROM(0XA1);     // sending A1 =10100001 = device adress word for read
    acknowledge();
    i = get_EEPROM(); // sending data 
    acknowledge();
    stop_EEPROM();              // sending stop condition to eeprom
        acknowledge();
    return(i); 
 }
//============================================================================
void send_EEPROM(char s_byte)
 {
    char temp = s_byte;
    char i ;

        
    for(i = 7 ; i >= 0 ; i--)
    {
 
        temp = s_byte;
        temp = temp >> i;
        temp = temp & 0X01;

     
        if(temp == 0)
            SDA =   0;
        else
            SDA =   1;

                SCL   =   1;
        wait(40);
        SCL   =   0;
           
    }
 }
//============================================================================

char get_EEPROM()
 {
    char temp, temp_h, i;
    temp = 0;
    temp_h = 1;        

    SDA = 1;    // making SDA as input pin for microcontroller
           SCL = 0;

    for(i = 7; i >=0 ; i--)
    {
        SCL = 1;
        if(SDA == 1)
        {
            temp = temp | temp_h<<i ;                      
        }
        wait(40);
                SCL = 0;
    }
    
    SCL = 0;
    return(temp);
 }
//============================================================================
void start_EEPROM()
 {
    SDA     =   1;
    SCL    =   1;
    wait(40);
        SDA     =   0;
        SCL    =   0;  

 }
//============================================================================
void stop_EEPROM()
 {
    SDA     =   0;
    SCL    =   1;
    wait(40);
    SDA     =   1;
    SCL    =   0;  
 }
//============================================================================
void acknowledge()
 {
         SCL = 1;
           wait(40);
        SCL = 0;
 }
//============================================================================
void wait(int i)
 {
    for(;i>=0;i--)
           {
           }
 }
 
Last edited by a moderator:

hello,

Adresse must be 16 bits wide ..

Code:
void send_to_mem(int s_address, char s_data)
 {
   
    start_EEPROM();             // sending start condition to eeprom 
    send_EEPROM(0XA0);     // A0 = 10100000 = sending device address word for write
    acknowledge();
    send_EEPROM(s_address>>8); // sending MSBaddress
    acknowledge();
     send_EEPROM(s_address & 0x0FF); // sending LSB address
    acknowledge();
    send_EEPROM(s_data);   // sending data 
    acknowledge();
    stop_EEPROM();              // sending stop condition to eeprom
        acknowledge();
        wait(100);
          
 }
 

Can you plz explain sendEEPROM() and getEEPROM() briefly as i have posted earlier in post4?
 

I am not able read data from get_EEPROM(). Every time it reads only 0XFE.
Code:
char get_EEPROM()
 {
    char temp, temp_h, i;
    temp = 0;
    temp_h = 1;        

    SDA = 1;    // making SDA as input pin for microcontroller
           SCL = 0;

    for(i = 7; i >=0 ; i--)
    {
        SCL = 1;
        if(SDA == 1)
        {
            temp = temp | temp_h<<i ;                      
        }
        wait(40);
                SCL = 0;
    }
    
    SCL = 0;
    return(temp);
 }

Kindly tell me the changes that i have to do in this.
 
Last edited by a moderator:

hello,

Datasheets explain all details..but you need to take some time to read it .
Others can help you, but don't do this elementary job, instead of you .

you simplify too much the EEPROM reading
have a look on page 9 of AT24C512 eeprom datasheet
fig 5 Random read and fig 6 sequential read ..
and work around this .
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…