Go Down

Topic: How to write 16bit signet int to SPI SRAM. (Read 234 times) previous topic - next topic

fatzolaa

Hello eveyone,

I have a MPU 6050 sensor. And I would like to write the accelerometer datas to SPI SRam one after another. And read them back.  The acc. datas are 16bit signed integer. How can I write it to spi sram(23lc512)? I know that probably I have to convert it into BIN and split into 2 parts. But I have no idea how to do it. And write it to the next address. Any example code would help me.

Thanks,
Fatzolaa

DrDiettrich

Have you searched for the SRAM library for Arduino? I already found it with a search for 23LC512.

 Then use e.g. the writeInt() method for writing a 16 bit information to the SRAM, readInt() for reading. You have to provide a new address with each function call. For writing multiple integers you can use writeInts().

For reading readInts() or writing writeInts() multiple integers at once you can use an int array or a struct containing the values, and provide the number of items as the len parameter. See the library and data sheet for more information.

fatzolaa

I still can't solve the problem. I download and installed the SRAM_23LC.h(by Justin Mattair) library and added to my program.  I wrote the program below. From 0 to 255 has no problem, but when I wrote 256 to the RAM I only can read back 1. So probably I only can write maximum 8bit integer, anyone can help me how to split it into 2 or any more idea, example code etc.
Code: [Select]
void loop() {
  int i=0;
  for (i = 0; i < 1000; i++){
  Serial.print("Write Byte: ");
  Serial.print(i);
  Serial.println();
  SRAM.writeByte(START_ADDRESS, i);
  Serial.print("Read Byte:  ");
  Serial.print(SRAM.readByte(START_ADDRESS));
  Serial.println();
  Serial.println();
  START_ADDRESS = START_ADDRESS + 1;
  delay(100);
  }
}


Ui.: The board is Arduino Due(32bit).

DrDiettrich

Use read/writeBlock() instead of read/writeByte().

fatzolaa

#4
Sep 21, 2019, 10:35 am Last Edit: Sep 21, 2019, 10:37 am by fatzolaa
What I am doing wrong? I can only write char type variables. If I do that It uses a more lot space from the ram, and gets hard to identify the begining and the end of the numbers.

Code: [Select]
#include <SPI.h>
#include <SRAM_23LC.h>

//SRAM settings
#define SPI_PERIPHERAL SPI
#define CHIP_SELECT_PIN 27
#define START_ADDRESS  0
SRAM_23LC SRAM(&SPI_PERIPHERAL, CHIP_SELECT_PIN, SRAM_23LCV512);
char buffer[]="2048";
#define BUFFER_SIZE  (sizeof(buffer) / sizeof(uint8_t))
void setup() {
  SRAM.begin(20000000UL);
  Serial.begin(9600);
}

void loop() {
  SRAM.writeBlock(START_ADDRESS, BUFFER_SIZE, buffer);
  SRAM.readBlock(START_ADDRESS, BUFFER_SIZE, buffer);
  for (size_t i=0; i < BUFFER_SIZE; i++) {
    Serial.write(buffer[i]);
  }
  Serial.println();
  Serial.print("Buffer_size");
  Serial.println(BUFFER_SIZE);
  delay(1000);
 
}



DrDiettrich

You better use binary data (long, float...) instead of text.

fatzolaa

I found the solution:

Code: [Select]
int START_ADDRESS = 0;
  digitalWrite(led1Pin, HIGH);
  Serial.println("Write to RAM.");
  for (int i = 0; i < 100; i++) {
  Raw2 = olvas2() ;
  Serial.println(Raw2);
  // rl1 = lowByte(Raw2);         //Szetosztas 2 byte-ra
  // rh1 = highByte(Raw2);
  rl2 = Raw2 & 0xff;
  rh2 = (Raw2 >> 8);
  SRAM.writeByte(START_ADDRESS, rl2);
  START_ADDRESS++;
  SRAM.writeByte(START_ADDRESS, rh2);
  START_ADDRESS++;
  }

Go Up