How to save Long value in DS1307 RAM

Hi!

I have an Arduino Nano and a DS1307-Module. This module has built in RAM, where I need to store a number (a counter value).
Previously I used the Arduinos EEPROM for saving the number, but at some point this is going to break, so I want to use the modules RAM.

Until now I used this function to save the value to EEPROM:

void EEPROMWritelong(int address, long value) {
    //Decomposition from a long to 4 bytes by using bitshift.
    //One = Most significant -> Four = Least significant byte
    byte four = (value & 0xFF);
    byte three = ((value >> 8) & 0xFF);
    byte two = ((value >> 16) & 0xFF);
    byte one = ((value >> 24) & 0xFF);
    
    //Write the 4 bytes into the eeprom memory.
    EEPROM.write(address, four);
    EEPROM.write(address + 1, three);
    EEPROM.write(address + 2, two);
    EEPROM.write(address + 3, one);
}

I am very new to Arduino and I struggle to write a function for the DS1307. So can somebody help me, to create a similar function to save the long-value in the DS1307 RAM instead if the EEPROM?

Best regards
Michael

I think I figured it out with the help of another Thread here. It seems to work this way:

void NVRAMWritelong(int address, long value) {
    byte four = (value & 0xFF);
    byte three = ((value >> 8) & 0xFF);
    byte two = ((value >> 16) & 0xFF);
    byte one = ((value >> 24) & 0xFF);
    
    Wire.beginTransmission(DS1307_ADDR);
    Wire.write(address + RAM_OFFSET);
   
    Wire.write(four);
    Wire.write(three);
    Wire.write(two);
    Wire.write(one);    
    
    Wire.endTransmission();   
}

Best regards
Michael

Looks good,

  1. you can gain some performance as the Arduino takes one clock to shift one bit.
  2. you can add a return code to check if all went well.
  3. address will probably only be an 8 bit value
  4. no need for local variables

that would give something like

int NVRAMWritelong(byte address, long value) 
{
    Wire.beginTransmission(DS1307_ADDR);
    Wire.write(address + RAM_OFFSET);
   
    Wire.write(value & 0xFF);
    value >>= 8;
    Wire.write(value & 0xFF);
    value >>= 8;
    Wire.write(value & 0xFF);
    value >>= 8;
    Wire.write(value & 0xFF);    
    
    return Wire.endTransmission();   
}

did you also manage to create the NVRAMReadlongfunction?

Thank you for your hints! Yes I also managed to implement the read-function:

long NVRAMReadlong(int address) {
  byte neededBytes = 4;
  Wire.beginTransmission(DS1307_ADDR);
  Wire.write(address + RAM_OFFSET);
  Wire.endTransmission(); 
  Wire.requestFrom( (uint8_t)DS1307_ADDR, neededBytes );
  
  //Read the 4 bytes from the nvram memory.
  long four = Wire.read();
  long three = Wire.read();
  long two = Wire.read();
  long one = Wire.read();
      
  //Return the recomposed long by using bitshift.
  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}

The only problem was understanding the I2C-Bus. Once I learned how it worked, the rest was not hard anymore.
However I will revise those functions according to your hints. Thank you!