HELP ME! Arduino don't write SD-Card in the sector 0

Hello, I have to write an SD-Card, with all the values of every single Byte of every single sector a (Byte: "00000000"). The Arduino managed to write them all, except sector 0. How do I also write sector 0?

The part of code that performs the job:

byte buffer_c[512] = {0x00};
byte buffer_d[512] = {0x00};
byte buffer_e[512] = {0x00};
long i = 0;
bool uguale = false;
int car = 0;
i = 0;
long h = 0;
for (i = 0; i < sd1block; i++) {
sd1.writeBlock(i,buffer_c);
Serial.print("Write: ");
Serial.println(i);
sd1.readBlock(i, buffer_e);
uguale = true;
h=0;
for (h=0;h<512;h++) {
if(buffer_e[h] == buffer_d[h]) {

      } else {
          uguale = false;
      }
 }
 if (uguale) {
     Serial.println("Sector OK");
 } else {
     i--;
     Serial.println("Sector No OK");
 }

}
Serial.println("Scrittura Terminata");

Welcome to the Forum! You will get faster and better help if you post your code as requested by the forum guidelines. Read the forum guidelines to see how to properly post code and some good information on making a good post. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting

Please post the complete code.

Which Arduino are you using? 1536 bytes in arrays is getting to the limit of the 328P based Arduinos.

I using Arduino ATMega2560.

SD cards are not meant to be written to in sectors. They are designed to be used with files. I am sure that sector 0 is where the code stored in the SD card writes its file allocation memory map, and so this sector is protected.

The file operating system also implements sector ware control, so that no single sectors receive too many writes and burn out due to writing a sector too frequently.

Codice Completo:

#include <SD.h>

#include <SPI.h>
#include <SD.h>
#include "Timer.h"

//Versione Beta 1.02

Sd2Card sd1;

void setup() {
// put your setup code here, to run once:
bool sd1_ok = false;
long sd1block = 0;
int cid;

Serial.begin(115200);
while (!Serial) {
}
if (!sd1.init(SPI_HALF_SPEED, 53)) {
Serial.println("SRC initialisation failed!");
sd1_ok = false;
while (1);
} else {
Serial.print("Wiring is correct and a card is present. Card 1 Size Block = ");
Serial.println(sd1.cardSize());
sd1block = sd1.cardSize();
sd1_ok = true;
}
byte buffer_c[512] = {0x00};
byte buffer_d[512] = {0x00};
byte buffer_e[512] = {0x00};
long i = 0;
bool uguale = false;
int car = 0;
i = 0;
long h = 0;
for (i = 0; i < sd1block; i++) {
sd1.readBlock(i, buffer_e);
Serial.print("Read: ");
Serial.println(i);
sd1.writeBlock(i,buffer_c);
Serial.print("Write: ");
Serial.println(i);
sd1.readBlock(i, buffer_e);
uguale = true;
h=0;
for (h=0;h<512;h++) {
if(buffer_e[h] == buffer_d[h]) {
} else {
uguale = false;
}
}
if (uguale) {
Serial.println("Sector OK");
} else {
i--;
Serial.println("Sector No OK");
}
}
Serial.println("Scrittura Terminata");
}

}

void loop() {
// put your main code here, to run repeatedly:

}

Please edit your post to add code tags ("</>" editor button).

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