0

The base of the problem: I have an Arduino Due and a MPU6050 accelerometer and an 23lcv512. MPU gives me 16bit signed integer. I would like to save the datas to the SRAM and after the measurement read them back and via Serial send it to PC. Sending to PC is not a problem. The problem is that this SRAM has 128k pcs 8bit address. My numbers are 16 bit. I can't write them directly. Here is my code. I tested the RAM with this code:

`

void loop() {
  int i = 0;
  Serial.print("Write Byte: ");
  for (i = 0; i < 70000; i++) {
    //Serial.print("Write Byte: ");
    //Serial.print(i);
    //Serial.println();
    SRAM.writeByte(START_ADDRESS, i);
    START_ADDRESS = START_ADDRESS + 1;
  }
  Serial.print("Write End");
  i = 0;
  START_ADDRESS = 0;
  for (i = 0; i < 300; i++) {
    Serial.print("Read Byte:  ");
    Serial.print(SRAM.readByte(START_ADDRESS));
    Serial.println();
    Serial.println();
    START_ADDRESS = START_ADDRESS + 1;
    delay(100);
  }
}`

I added the 23LC library. If it's run reads back the numbers from the RAM but after 255 it starts to read 0 again. I know why does it happen. But I don't know howto solve the problem.

I tried to use the writeBlock command but it only works for me with char variables. Char variable requires more space than integers. I have not too much.

Is there anyone who can write a sample code which can write 16 bit signed integer to sram?

fatzolaa
  • 1
  • 1

1 Answers1

2

I've commented the most obvious problems in your original code below:

void loop() {
    int i = 0;
    Serial.print("Write Byte: ");
    for (i = 0; i < 70000; i++) {          // since i is a 16bit int, 70,000 is out of range.
        SRAM.writeByte(START_ADDRESS, i);  // cool you wrote 1 byte, where is the other write?
        START_ADDRESS = START_ADDRESS + 1; // try to keep all caps names for constants.
                                           // this will make your code easier to read, trust me!
    }
    Serial.print("Write End");
    i = 0;
    START_ADDRESS = 0;
    for (i = 0; i < 300; i++) {
        Serial.print("Read Byte:  ");
        Serial.print(SRAM.readByte(START_ADDRESS)); // you read 1 byte, you can't expect a 16 bit 
                                                    // value out of that.
        Serial.println();
        Serial.println();
        START_ADDRESS = START_ADDRESS + 1;
        delay(100);
    }
}

Here's a more sound approach, it stores unsigned ints, but that can easily be changed to signed ints.

#define SRAM_SIZE (128UL << 10)  // we have 128K of SRAM available. 
                                 // The U and L make this value an unsigned long.
                                 // ALWAYS use unsigned values for addresses.

void loop()
{
    Serial.print(F("Writing sequential numbers into SRAM..."));  // _always_ store string constants in flash.
                                                                 // save your RAM for more interesting stuff.

    for (unsigned long i = 0; i < SRAM_SIZE; i += 2) // filling SRAM
    {
         // this is the (truncated from 0-65535) value we'll write.
         unsigned int value = static_cast<unsigned int>(i & 0xFFFF);

         SRAM.writeByte(i, value & 0xFF);                 // write lowest 8 bits
         SRAM.writeByte(i + 1, (value >> 8) & 0xFF);      // write next 8 bits.
    }
    Serial.println(F("done."));

    // read back
    Serial.println(F("SRAM contents (16-bits unsigned values):"));

    for (unsigned long i = 0; i < SRAM_SIZE; i += 2) // reading all SRAM in 16-bit chunks.
    {
        Serial.print(i, HEX);
        Serial.print(F(": "));

        // read two consecutive bytes and pack them into a 16-bit integer.
        unsigned int value = SRAM.readByte(i) + (static_cast<unsigned int>(SRAM.readByte(i+1)) << 8);   // the cast is necessary.
        Serial.println(value);
    }
    delay(100);
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19