Write data in a SD with SPI

Good evening, I'm trying to develop a program that takes a data of my choice and writes it in a certain area of ​​the memory, for now with poor results, can someone help me? I am using a 2gb sd and an adapter of this model.
I enclose the code and thank you very much: D

#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//sck
#define CHIPSELECT 10//ss

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

byte eeprom_output_data;
byte eeprom_input_data=0;
byte clr;
int address=0;
//data buffer
int numByte = 4;
char buffer [4] = {'C','I','A','O'};

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  delay(10);
  return SPDR;                    // return the received byte
}

void setup()
{

  Serial.begin(9600);

  pinMode(DATAOUT, OUTPUT);

  pinMode(DATAIN, INPUT);

  pinMode(SPICLOCK,OUTPUT);

  pinMode(CHIPSELECT,OUTPUT);

  digitalWrite(CHIPSELECT,HIGH); //disable device

  SPCR = (1<<SPE)|(1<<MSTR);

  clr=SPSR;

  clr=SPDR;

  delay(10);

  digitalWrite(CHIPSELECT,LOW);

  spi_transfer(WREN); //write enable

  digitalWrite(CHIPSELECT,HIGH);

  delay(10);

  digitalWrite(CHIPSELECT,LOW);

  spi_transfer(WRITE); //write instruction

  address=0;

  spi_transfer((char)(address>>8));   //send MSByte address first

  spi_transfer((char)(address));      //send LSByte address

  //write numByte bytes

  for (int I=0;I<numByte;I++)

  {

    spi_transfer(buffer[I]); //write data byte

  }

  digitalWrite(CHIPSELECT,HIGH); //release chip

  //wait for eeprom to finish writing

  delay(3000);

  Serial.print('h');

  Serial.print('i');

  Serial.print('\n');//debug

  delay(1000);

  lettura_eeprom(address);

  Serial.print("fine.");
}

byte read_eeprom(int EEPROM_address)
{

  //READ EEPROM

  int data;

  digitalWrite(CHIPSELECT,LOW);

  spi_transfer(READ); //transmit read opcode

  spi_transfer((char)(EEPROM_address>>8));   //send MSByte address first

  spi_transfer((char)(EEPROM_address));      //send LSByte address

  data = spi_transfer(0xFF); //get data byte

  digitalWrite(CHIPSELECT,HIGH); //release chip, signal end transfer

  return data;
}

void lettura_eeprom(int address){
  for(int i = 0; i<numByte;i++){
    eeprom_output_data = read_eeprom(address);

      Serial.print(eeprom_output_data);

      Serial.print('\n');

      address++;

      delay(500); //pause for readability
    }
  
}

void loop()
{

}

Help me understand what you are attempting to do. Are you wanting to write to an SD card or are you wanting to write to Arduino memory?

You comment about poor results. What does that mean? What results are you getting? You are obviously NOT using the SD card as a file or any other normal access. Did you correctly format the SD card?

I am trying to write to the SD memory, but when I read it I only see FFh (or 255). the SD is formatted as fat32. Thank you

To the best of my knowledge you can't write to an SD card as if it were an EEPROM.
An SD card contains a micro controller that handles data allocation for a filling system. When you communicate over SPI you are talking to this micro controller not to the memory itself.

It seems to me that you don't want an SD card at all, how about using an external FRAM memory chip. Or is it the removable aspect of the SD card you want? In which case what is going to read this?

1 Like

unfortunately I can not decide on the type of memory, it must be a removable SD (the boss hears no excuses). The memory has to store 15 data every 10 seconds, but for now I'm trying to write just a few letters. thanks for your help: D

open file and write to it use .seek to browse it

Well this is because the boss has no idea of what he wants. And no concept of how a PC is ever going to read an SD card with no file structure.

The only thing I can suggest is if you use the SD card library and open a file to write to. When you have finished writing to it or when you want to power down the Arduino then close the file.

If you try and remove an SD card that still has an open file on it, or simply remove the power the the SD card will become corrupted. So I would suggest a button that you press before powering down or removing the SD card that closes the file.

Or you could open a file for both read and write. Store the data and then every 10 seconds open the file, write to the end of it and close it again. Have an LED lit to show this operation is taking place. Then you know when it is safe to remove the power. That is just after the LED goes off.

The best alternative is to get a better boss. :wink:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.