Thursday, November 19, 2015

Adding the 24LC256 EEPROM to the Arduino Due

Adding the 24LC256 EEPROM to the Arduino Due

The Arduino due lacks an EEPROM. This instructable adds one and allows you to store values in non volatile memory that will survive an Arduino firmware update.
Step 1: Bread-boarding


There are some really good instructions here.

https://www.robomart.com

I just followed them.

The photo shows the bread-boarded circuit. Pins 1 to 4 and pin 7 are grounded. Pin 8 is connected to the 3.3V supply on the due board.

The yellow (pin 6) and white (pin 5) wires connected to the i2c SDA (data) and SCL (clock) pins on the due board (numbered 20 and 21).

Step 2: Code time.


Here are some snippets of code that I apply in my sketches.

Firstly, include the wire library headers somewhere near the top of your sketch.

/* Use 24LC256 EEPROM to save settings */

#include <Wire.h>

Then add some functions to read and write bytes from the EEPROM (I only care about individual bytes but there is a page write feature in the chip too). Note there is a macro definition of 0x50.This is the address of the chip on the i2c bus (you can connect more than one i2c thingies on an i2c bus and select which one you want to talk to by changing the address).

/* These two functions help us write to the 24LC256 EEPROM chip */

#define EEPROM_ADDR 0x50

void EEPROM_write(unsigned int addr,byte data) {

  int rdata = data;

  Wire.beginTransmission (EEPROM_ADDR);

  Wire.write((int)(addr >> 8));       // MSB

  Wire.write((int)(addr & 0xFF));     // LSB

  Wire.write(rdata);

  Wire.endTransmission();

  //Serial.print("EEPROM write: addr: ");

  //Serial.print(addr);

  //Serial.print(" ");

  //Serial.println(data);

  delay(5);
}

byte EEPROM_read(unsigned int addr) {

  byte data = 0xFF;

  Wire.beginTransmission (EEPROM_ADDR);

  Wire.write((int)(addr >> 8));       // MSB

  Wire.write((int)(addr & 0xFF));     // LSB

  Wire.endTransmission();

  Wire.requestFrom(EEPROM_ADDR,1);

  if (Wire.available()) data = Wire.read();

  //Serial.print("EEPROM read: addr: ");

  //Serial.print(addr);

  //Serial.print(" ");

  //Serial.println(data);

  delay(5);

  return data;
}



You can uncomment the Serial.print(...) lines if you want to see some debug output.

In arduinos setup() function you start the wire library and can read in initial values.
Here I read in two bytes (flags and max_cc), two words (lean_min and lean_max) and an array of words sd_max[3]:

    // read values saved in the EEPROM

    Wire.begin();

    flags=EEPROM_read(0);

    max_cc=EEPROM_read(1);

    lean_min=word(EEPROM_read(3),EEPROM_read(2));

    lean_max=word(EEPROM_read(5),EEPROM_read(4));

    for(int j=0;j<3;j ) {

      sd_max[j]=word(EEPROM_read(7 j*2),EEPROM_read(6 j*2));

    }


Here's the bit of code that writes them into the EEPROM:

         EEPROM_write(0,flags);

         EEPROM_write(1,max_cc);

         EEPROM_write(2,lowByte(lean_min));

         EEPROM_write(3,highByte(lean_min));

         EEPROM_write(4,lowByte(lean_max));

         EEPROM_write(5,highByte(lean_max));

         for(int j=0;j<3;j ) {

           EEPROM_write(6 j*2,lowByte(sd_max[j]));

           EEPROM_write(7 j*2,highByte(sd_max[j]));

         }


That’s about it really.

Step 3: Wire It Up


 

Wire it onto some veroboard to screw into an enclosure and the jobs done.


Thanks and references:

Thank you for your support,For more information visit to: www.robomart.com/blog

No comments:

Post a Comment