Writing unsigned long long type into SPI RAM

Hi all,

I’m using Arduino Mega with a 23LCV512 SPI RAM.
I need to read and write unsigned long long values from/to the RAM.
The writing and reading functions require the data to be represented as char array.
I have found a way of converting the unsigned long long to char array, and as a result I get a hexadecimal char array.

However, the spi RAM seems does not accept hexadecimal char array.
Below is the code I’m trying to use:

#include <SPI.h>
#include <SpiRAM.h>

#define SS_PIN 53

byte clock = 0;
SpiRAM SpiRam(0, SS_PIN);

void setup()   {                
  Serial.begin(9600);
}

void loop()                    
{
  //char data_to_chip[8] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  char data_to_chip[8] ={'1','2','3','4','5','6','7'};
  char data_from_chip[8];
  int i = 0;

  // Write some data to RAM
  SpiRam.write_stream(0, data_to_chip, 7);
  delay(100);

  // Read it back to a different buffer
  SpiRam.read_stream(0, data_from_chip, 7);

  // Write it to the serial port
  for (i = 0; i < 7; i++) {
    Serial.print(data_from_chip[i]);
  }
  Serial.print("\n");
  delay(1000);                  // wait for a second
}

When I represent the data as char “data_to_chip[8] ={‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’}” then everything works fine. However when I represent it as hexadecimal “char data_to_chip[8] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}” the it fails to write in the RAM.

Can anyone suggest how can I convert an unsigned long long to regular (not hex) char array? Or is there any way I can overcome this issue.

I’d appreciate any help.

You realize that ASCII codes are different than numeric values. That is, 0 as a numeric is...well, zero. However, your string has '0', which is ASCII for zero and is 48. So, add 48 (0x30) to each number and send that and see what happens. Both are sending the same values, only you see the difference.

However, your string has '0', which is ASCII for zero and is 48. So, add 48 (0x30) to each number and send that and see what happens.

Hi econjack. As I'm not an expert in data types, could you please explain what you meant in the quotes more in detail? My string does not have '0', does it?

Thanks in advance.

The writing and reading functions require the data to be represented as char array.

From what I see of the library, you can send a byte array, and you should be able to change

char data_to_chip[8] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

to

byte data_to_chip[8] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};