I2C SRAM with EEPROM 47L16 47C16 47L04 47C04

Wiki
Here is a code snippet for the I2C SRAM 47L16
The code can be used in your own projects. No extra library is used, but the necessary functions are here.


/*
*   example for 47L16 | 47C16 SRAM with shadow EEPROM
*   work like a I2C EEPROM
*   has more write cycles and is faster.
*   
*   https://ww1.microchip.com/downloads/en/DeviceDoc/47L04_47C04_47L16_47C16_DS20005371D.pdf
*   
*   like I2C EEPROM
*   pin 2 A1
*   pin 3 A2
*   pin 4 GND
*   pin 5 SDA
*   pin 6 SCL
*   pin 8 2.7 - 3.6 V  47L## | 4.5 - 5.5 V 47C##
*   
*   diffrend 
*   pin 1   10 uF Cap
*   pin 7   high = save to EEPROM
*   
*   If the settings for bit 2 are set then the IC saves to the EEPROM when the voltage drops.
*   The same happens when a high is set at pin 7
*   When the voltage is applied again, the values ​​are written back into the SRAM
*   Both are possible via software
*   
* 
*/






#include <Wire.h>

#define EEPROM_I2C_ADDRESS      0x50     
#define EERAM_CONTROL_ADDRESS   0x18


// Function to write to EEPROOM   
void writeEEPROM(int eeaddress, long vala,  uint8_t  lange)
{  //                       byte = 1, int = 2; long  = 4 ; zahler und subZahler = 6
 // Begin transmission to I2C EEPROM
 
 Wire.beginTransmission(EEPROM_I2C_ADDRESS);

 // Send memory address as two 8-bit bytes
 Wire.write((int)(eeaddress >> 8));   // MSB
 Wire.write((int)(eeaddress & 0xFF)); // LSB
 // Send data to be stored
                   Wire.write(vala & 255); vala = vala /256 ;
 if (lange >1 )  { Wire.write(vala & 255); vala = vala /256 ; }
 if (lange >2 )  { Wire.write(vala & 255); vala = vala /256 ; 
                   Wire.write(vala & 255); }
/*  if (lange >4 )  { Wire.write(subZahler & 255);
                   Wire.write(subZahler / 255); } */
 Wire.endTransmission();    // End the transmission

 delay(50);      // Add delay for EEPROM, 
} // writeEEPROM4(int eeaddress, unsigned long vala)


unsigned long readEEPROM(int eeaddress,  uint8_t  lange) {
 
uint8_t  tea = 0;  uint8_t  teb = 0;  uint8_t  tec = 0;  uint8_t  ted = 0;
uint8_t  sea = 0;  uint8_t  seb = 0; 

 // Begin transmission to I2C EEPROM
 Wire.beginTransmission(EEPROM_I2C_ADDRESS);

 // Send memory address as two 8-bit bytes
 Wire.write((int)(eeaddress >> 8));   // MSB
 Wire.write((int)(eeaddress & 0xFF)); // LSB 
 Wire.endTransmission();             // End the transmission 
 Wire.requestFrom(EEPROM_I2C_ADDRESS, lange);  // Request 4 byte of data at current memory address
 while(Wire.available()) {// slave may send less than requested
                   tea =  Wire.read();     // Read the data and assign to variable
 if (lange >1 )  { teb =  Wire.read(); }
 if (lange >2 )  { tec =  Wire.read(); }
 if (lange >3 )  { ted =  Wire.read(); }  
/*  if (lange >4 )  { sea =  Wire.read();     // zweiten Wert mit lesen
                   seb =  Wire.read(); 
            if (eeaddress == 2 ) {      
            subZahler = (seb*256 + sea); 
            } } } */
                   
 // Return the data as function output
unsigned long out = teb*256 + tec;
             out = out*256 + teb;
             out = out*256 + tea;          

return out  ;   
} // readEEPROM(   unsigned long, 4 Byte

void saveSRAM2EEPROM() {
   Wire.beginTransmission(EERAM_CONTROL_ADDRESS);  
   Wire.write(85);
   Wire.write(51);
   Wire.endTransmission();
   delay(10);
} //  

void saveEEPROM2SRAM() {
   Wire.beginTransmission(EERAM_CONTROL_ADDRESS);  
   Wire.write(85);
   Wire.write(221);
   Wire.endTransmission();
   delay(10);
} // 

void writeSRAMstatus( uint8_t  rdata) {
   Wire.beginTransmission(EERAM_CONTROL_ADDRESS);
   Wire.write(0);
   Wire.write(rdata);
   Wire.endTransmission();
   delay(20);
} // writeSRAMstatus(  nur byte

void readSRAMstatus() {    // byte 
   uint8_t  rdata = 0xFF;
   Wire.beginTransmission(EERAM_CONTROL_ADDRESS);
   Wire.write(0); 
   Wire.endTransmission();
   Wire.requestFrom(EERAM_CONTROL_ADDRESS,1);
   if (Wire.available()) rdata = Wire.read();
   if (rdata == 255 ) {  Serial.println("Lesefehler"); return ;}
   
   Serial.println();
   Serial.println(F(" Status Register "));
   Serial.println(F("1M--BBBSE     M= mmodified  S= Auto-Store E= HS Pin"));
   Serial.println(rdata + 256,BIN); // 9. Bit in front cheat then the length is suitable
   //Serial.println(rdata); 
     if (rdata & 128) { Serial.println(F("modified"));
         saveSRAM2EEPROM(); 
         }
     if (rdata & 2) { Serial.println(F("Auto Store aktiv"));}
     if (rdata & 1) { Serial.println(F("HS pin gespeichert"));}
         rdata=(rdata/4) & 7 ;
      if (rdata > 0) {
          Serial.println(F("Speicherschutz eingeschaltet !")); }
      
    Serial.println(); Serial.println();
   
   //return rdata;
} // readEEPROM(  nur byte

/* 
*  Status Register
*   76543210
*   A00BBBAE
*   M  PPPSV
*      210EE
*          N
*          T
*          
*   AM       1 = SRAM array has been modified
*            0 = SRAM array has not been modified     
*   0
*   0
*   BP2 - BP0  = 000 = Entire array is unprotected
*                001 = Upper 1/64 of array is write-protected
*                010 = Upper 1/32 of array is write-protected
*                011 = Upper 1/16 of array is write-protected
*                100 = Upper 1/8 of array is write-protected
*                101 = Upper 1/4 of array is write-protected
*                110 = Upper 1/2 of array is write-protected
*                111 = Entire array is write-protected
*   ASE           1 = Auto-Store feature is enabled
*                 0 = Auto-Store feature is disabled
*   EVENT         1 = An event was detected on the HS pin
*   HardwareStore 0 = No event was detected on the HS pin
*          
*  
*/












void setup() {
Wire.begin(SDA,SCL)  ;       // 
Wire.setClock(400000);      // Set the I2C SCL to 400kHz
Serial.begin(115200);      // Serielle Schnittstelle initialisieren

 

 // write to SRAM
 uint8_t i = 123 ;
 writeEEPROM(0, i,  1) ;
 //         /\ where to put
 //                 /\ how long in byte

 uint16_t j = 2022 ;
 writeEEPROM(1, j,  2) ;

 uint32_t k = 4444 ;
 writeEEPROM(3, k,  4) ;

 // save SARM to EEPROM
 saveSRAM2EEPROM();

 // restore EEPROM to SRMA
 saveEEPROM2SRAM();

 // set parameter
 writeSRAMstatus(2);

 // read parameter
 readSRAMstatus();

 // read SRAM
 k = readEEPROM(3,  4)
  //            /\ where to read
 //                 /\ how long in byte
 
 

}

void loop() {
 // put your main code here, to run repeatedly:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.