RTC EEPROM Read write

I am trying to read & write in the I2C EEPROM on DS3231 RTC module. Only single bytes are to be written but the readout data is incorrect.

#include "mbed.h"
 
I2C i2c(PB_11, PB_10);
Serial RS232(USBTX, USBRX);
int address = 0x57;

#include "ds3231.h"
#include "_24LCXXX.h"

//rtc object
Ds3231 rtc(PB_11, PB_10); //SDA,SCL
_24LCXXX eeprom(&rtc, 0x57);

int ee_memory[20],read_data[20];

int main()
{
    int i,add,data;   
  
    for(i=0; i<10; i++)
    {
        eeprom.byte_write(ee_memory[i], i); 
        RS232.printf("Write Data = %d\n",i);
    }        
    
    for(i=0; i<10; i++)
    {
        data = eeprom.nbyte_read(ee_memory[i], &read_data[i],  1); 
        RS232.printf("Read Data = %d\n",read_data[i]);
    }
    
    while(1)
    {
    
    }
}  

Can anybody will like to help me out?

Hello,

I don’t know anything about that, but maybe take a look to what the methods for read/write return.
From my point of view, both failing cuz addresses not exist and that is because you have your array of addresses ee_memory empty.

BR, Jan

Thanks Jan for the prompt reply. The module has address 0x68 for RTC & 0x57 for EEPROM.

If you can share other way to read & write to I2C EEPROM for single byte it will be helpful.

I did not write about I2C addresses but about internal memory addresses.
Just try to print out the address what you are sending to EPROM. You will see only a ballast I think.

You declared

int ee_memory[20]...

and then use is like this

eeprom.byte_write(ee_memory[i], i); 

and where are the values of array ee_memory?

Try this and same for read

    for(i=0; i<10; i++)
    {
        eeprom.byte_write(i, i); 
        RS232.printf("Write Data = %d\n",i);
    }        
    

EDIT:
For demonstration

    int arrSize = 20;
    int ee_memory[arrSize];
    for (int i = 0; i < arrSize; i++) {
        printf("MemAddress%02d:  %d\n",i,ee_memory[i]);
        thread_sleep_for(100);
    }

Result:

MemAddress00: 0
MemAddress01: 0
MemAddress02: 0
MemAddress03: 134225199
MemAddress04: 536871712
MemAddress05: 134225069
MemAddress06: 536871712
MemAddress07: 262144
MemAddress08: 0
MemAddress09: 134229473
MemAddress10: 134233848
MemAddress11: 134233852
MemAddress12: 0
MemAddress13: 134218529
MemAddress14: 134233848
MemAddress15: 134233848
MemAddress16: 0
MemAddress17: 134218823
MemAddress18: 0
MemAddress19: 134218297

BR, Jan

Hi Jan,

I got your point sorry for delayed reply. All the data was getting written to address zero. I updated the program but still no success.

#include "mbed.h"
 
I2C i2c(PB_11, PB_10);
Serial RS232(USBTX, USBRX);
int address = 0x57;

#include "ds3231.h"
#include "_24LCXXX.h"

//rtc object
Ds3231 rtc(PB_11, PB_10); //SDA,SCL
_24LCXXX eeprom(&rtc, 0x57);

int ee_memory[10] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0};
int read_data[20];

int main()
{
    int i,add,data;   
  
    for(i=0; i<10; i++)
    {
        eeprom.byte_write(ee_memory[i], i);      
        RS232.printf("Write Data = %02d, %d\n",i,ee_memory[i]);
    }        
    
    for(i=0; i<10; i++)
    {
        data = eeprom.nbyte_read(ee_memory[i], &read_data[i],  1); 
        RS232.printf("Read Data = %02d, %d\n",read_data[i],ee_memory[i]);
    }
    
    while(1)
    {
    
    }
}  

The result is :
Write Data = 00, 16
Write Data = 01, 32
Write Data = 02, 48
Write Data = 03, 64
Write Data = 04, 80
Write Data = 05, 96
Write Data = 06, 112
Write Data = 07, 128
Write Data = 08, 144
Write Data = 09, 160
Read Data = 00, 16
Read Data = 00, 32
Read Data = 00, 48
Read Data = 00, 64
Read Data = 00, 80
Read Data = 00, 96
Read Data = 00, 112
Read Data = 00, 128
Read Data = 00, 144
Read Data = 00, 160

Well I found a blunder mistake in the tracks. Following code works perfectly :

#include "mbed.h"
#include "ds3231.h"
#include "_24LCXXX.h" 

I2C i2c(PB_11, PB_10);
Serial pc(USBTX, USBRX);
int address = 0x57;

//rtc object
Ds3231 rtc(PB_11, PB_10); //SDA,SCL
_24LCXXX eeprom(&rtc, 0x57);

uint8_t ee_memory[12] = {0,0,1,2,3,4,5,6,7,8,9,10};
uint8_t memory[12]    = {0,0,10,20,30,40,50,60,70,80,90,100};
uint8_t read_data[20];

int main()
{
    int i,flag;
    uint8_t ChkSign;       
    
    eeprom.nbyte_read(0, &ChkSign, 1); 
    pc.printf("Chk Sign! : = %d\n",ChkSign);  
        
    if (ChkSign != 21) //Rewrite EEPROM if not found check sign at location '0'
    {
        ChkSign = 21;
        eeprom.byte_write(0, ChkSign);  //chk sign value compare which is '21' at loc '0'       
        
        for( i=1; i<10; i++) 
        {
            eeprom.byte_write(i, memory[i]);
        }
        pc.printf("memory byte data write completed!\n");
    }
    else
    {
        for(i=1; i<10; i++)
        {
            eeprom.nbyte_read(i, &memory[i], 1); //Read Backup settings in RTC EEPROM
            pc.printf("%d\n",memory[i]);
        }
        pc.printf("memory byte data read completed!\n");
    }    


    while(1)
    {
        thread_sleep_for(1000);
    }
}

But still not sure why following code with arrays doesn’t work ?

#include "mbed.h"
#include "ds3231.h"
#include "_24LCXXX.h" 

I2C i2c(PB_11, PB_10);
Serial pc(USBTX, USBRX);
int address = 0x57;

//rtc object
Ds3231 rtc(PB_11, PB_10); //SDA,SCL
_24LCXXX eeprom(&rtc, 0x57);

uint8_t ee_memory[12] = {0,0,1,2,3,4,5,6,7,8,9,10};
uint8_t memory[12]    = {0,0,10,20,30,40,50,60,70,80,90,100};
uint8_t read_data[20];

int main()
{
    int i,flag;
    uint8_t ChkSign;       
    
    eeprom.nbyte_read(ee_memory[0], &ChkSign, 1); 
    pc.printf("Chk Sign! : = %d\n",ChkSign);  
        
    if (ChkSign != 21) //Rewrite EEPROM if not found check sign at location '0'
    {
        ChkSign = 21;
        eeprom.byte_write(ee_memory[0], ChkSign);  //chk sign value compare which is '21' at loc '0'       
        
        for( i=1; i<10; i++) 
        {
            eeprom.byte_write(ee_memory[i], memory[i]);
        }
        pc.printf("memory byte data write completed!\n");
    }
    else
    {
        for(i=1; i<10; i++)
        {
            eeprom.nbyte_read(ee_memory[i], &memory[i], 1); //Read Backup settings in RTC EEPROM
            pc.printf("%d\n",memory[i]);
        }
        pc.printf("memory byte data read completed!\n");
    }   
 


    while(1)
    {
        thread_sleep_for(1000);
    }
}

Hello,

I see only the different data types. In first case you use int in second case you uint8_t.

BR, Jan

Well its sorted now. The array first & second element were pointing to same address ‘0’ so chk sign was getting overwritten. Correcting it works !

Thanks Jan for your support!