0

I'm currently trying to write to and read from an 25LC256-E EEPROM with my Arduino Uno via SPI. I've made a program which is supposed to do this, but I only get weird answers.

I'm sending in a Serial-string. This string should start with 'w', 'r' or 'e', depending on what action I want to perform. The second character should be an int between 0 and 3 to determine witch PIN I should use as slave (currently I'm using only 1 slave, so the int should always be 0). The rest of the string gets written to the EEPROM, one byte/character at the time.

What am I missing?

CODE:

    #include <SPI.h>

    #define WREN  6
    #define WRDI  4
    #define RDSR  5
    #define WRSR  1
    #define READ  3
    #define WRITE 2

    // Multiple slaves for future project
    const int CHANNELS[4] = {10, 8, 7, 4};
    String readResult;

    void setup() {

      Serial.begin(9600);
      channelSetup(); // Set all slave pins in CHANNELS to OUTPUT and HIGH

      SPI.begin();
      SPI.setDataMode(SPI_MODE3);
      SPI.setClockDivider(SPI_CLOCK_DIV2);
      SPI.setBitOrder(MSBFIRST);

    }

    void loop() {

      Serial.println("START");

      while(!Serial.available());
      delay(20);

      // Get all available bytes/characters
      String serialInput = getSerialStream(Serial.available());

      // Second character in serialInput is an int between 0 and 3
      int channel = makeValidChannel(serialInput[1]);

      // "channel" must be between 0 and 3
      // to be able to point to CHANNELS
      if (channel >= 0 && channel <= 3) {

        // First character decides what to do
        switch (serialInput[0]) {

          case 'w':
            writeToChannel(channel, serialInput.substring(2));
            break;

          case 'r':
            readResult = "EMPTY";
            readResult = readFromChannel(channel);
            Serial.print("Result: ");
            Serial.println(readResult);
            break;

          case 'e':
            overWriteEEPROM(channel);
            break;

          default:
            Serial.println("INVALID FIRST OPERATION");

        }

      }

      else
        Serial.print("INVALID CHANNEL: ");
        Serial.println(channel);

    }

    void channelSetup() {

      for (int i = 0; i < sizeof(CHANNELS); i++) {

        pinMode(CHANNELS[i], OUTPUT);
        digitalWrite(CHANNELS[i], HIGH);

      }

    }

    String getSerialStream(int numOfChars) {

      char serialStream[numOfChars + 1];
      int i;

      for (i = 0; i < numOfChars; i++) {

        serialStream[i] = char(Serial.read());
        delay(5);

      }

      serialStream[i] = '\0';
      return String(serialStream);

    }

    int makeValidChannel(char chIn) {

      int channel = chIn - '0';
      //channel -= 48;

      if (channel >= 0 && channel <= 3)
        return channel;
      else
        return -1;

    }

    void writeToChannel(int channel, String message) {

      int slaveselect = CHANNELS[channel];
      int i;

      // Write enabled
      digitalWrite(slaveselect, LOW);
      SPI.transfer(WREN);
      digitalWrite(slaveselect, HIGH);
      delay(5);

      // Disable Write Protection
      digitalWrite(slaveselect, LOW);
      SPI.transfer(WRSR);
      SPI.transfer(0);
      digitalWrite(slaveselect, HIGH);
      delay(5);

      for (i = 0; i < message.length(); i++) {

        digitalWrite(slaveselect, LOW);
        SPI.transfer(WRITE);
        SPI.transfer(0);
        SPI.transfer(i);
        SPI.transfer(message[i]);
        digitalWrite(slaveselect, HIGH);
        delay(5);

      }

      // When writing of "message" is done,
      // add a 0-byte as terminating character
      digitalWrite(slaveselect, LOW);
      SPI.transfer(WRITE);
      SPI.transfer(0);
      SPI.transfer(i);
      SPI.transfer(0x00);
      digitalWrite(slaveselect, HIGH);
      delay(5);


      // Enable Write Protection
      digitalWrite(slaveselect, LOW);
      SPI.transfer(WRSR);
      SPI.transfer(3);
      digitalWrite(slaveselect, HIGH);
      delay(5);

      // Write Disabled
      digitalWrite(slaveselect, LOW);
      SPI.transfer(WRDI);
      digitalWrite(slaveselect, HIGH);
      delay(5);

    }

    String readFromChannel(int channel) {

      Serial.println("Reading started");

      int slaveselect = CHANNELS[channel];
      char outputArr[64];
      int i = 0;

      while (true) {

        digitalWrite(slaveselect, LOW);
        SPI.transfer(READ);
        SPI.transfer(0);
        SPI.transfer(i);
        outputArr[i] = SPI.transfer(0x00);
        digitalWrite(slaveselect, HIGH);
        delay(5);

        if (outputArr[i] == 0x00 || i >= 64) {

          outputArr[i] = '\0';
          return String(outputArr);  

                }

        else
          i++;

      }

    }

    void overWriteEEPROM(int channel) {

      char zeroArr[64];
      for (int i = 0; i < 256; i++) {

        zeroArr[i] = 0;

      }

      writeToChannel(channel, String(zeroArr))

    }
  • Don't know if this is the issue, but the else part of your if statement in the loop is missing opening and closing curly brackets.... – Anonymous Penguin Jul 24 '14 at 12:31
  • @AnnonomusPenguin : No, but thanks for noticing. It's just a minor bug that causes Serial.println(channel); to run regardless of if/else. – DoTheGenes Jul 24 '14 at 12:41
  • "but I only get weird answers". That's kind of vague, could you give us some sample outputs? – Gerben Jul 24 '14 at 18:00
  • 1
    @Gerben : I tested some more, and I think I found out what the "weird answers" were: I filled the first 8 adressfields of my EEPROM (using working demo code) with int values ranging from 59 and upwards, and when I tried reading the values later they printed out in ASCII. I.E., "59" became ";". – DoTheGenes Jul 25 '14 at 12:30
  • @DoTheGenes great work. So it's solved then! – Gerben Jul 25 '14 at 13:49
-1

Type must be BYTE, not INT. The write value is 1 BYTE 0 - 255. Use typecasting if necessary.

int x;
byte y;
y = byte(x);
EEPROM.write(100, y);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.