Could the code be used with this SDcard connection

Hello everyone,

according the link of SDcard read and write tutorial
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206874649

if i just wanted to write things into the SDcard, i could delete the
section of " int readDisk; " ?

if it does, what things can be also deleted ?

The content is changed, sorry for the previous reader

i need much more help with the SDcard write
i don't know how to write this
i will keep trying, please help
Thanks a lot :slight_smile:

for this code below, could it automatically create the file that i would like to save the data to ?

#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>





int print_disk_info();
int sample();
int readDisk();

byte incomingByte;
void printWelcome();
long int address;
byte tempBytes[2];


void setup()
{

  Serial.begin(9600);
  delay(1000);

  printWelcome();
  if(!sd_raw_init())
  {
     Serial.println("MMC/SD initialization failed");
  }
  print_disk_info();
}



void loop()
{
    int i;

    if(Serial.available()>0)
    {
         incomingByte=Serial.read();

       switch(incomingByte)
       {
           case 114:
                     readDisk();
                     break;
           case 115:
                     sample();
                     break;
           default:
                     break;
       }
    }
}

int sample()
{
    int i,j;
    int temp;
    byte low;
    byte high;
    byte inByte;

    Serial.println();
    Serial.println();
    Serial.println("Sampling..");
    for(i=0;i<500;i=i+2)
    {
        if(Serial.available()>0)
        {
            inByte=Serial.read();
            if(inByte==113) return 0;
        }
        temp=analogRead(0);
        Serial.print(temp,DEC);
        Serial.print(" ");

        //Convert int to 2 bytes
        low=temp&0xFF;
        high=temp>>8;
       // Serial.print(temp,DEC);
        //Serial.print(low,DEC);
        //Serial.print(high,DEC);

        tempBytes[0]=low;
        tempBytes[1]=high;

        if(!sd_raw_write(i,tempBytes,2))
        {
          Serial.print("Write error");
        }
        //sd_raw_sync();
        delay(5000);

  Serial.println();

    }

    return 1;
}


int readDisk()
{
    byte low;
    byte high;
    byte info[2];
    int i;
    int  result;
    Serial.println();
    for(i=0;i<50;i=i+2)
    {
        sd_raw_read(i,info,2);

        //Serial.print(info[0],DEC);
        //Serial.print(" ");
        //Serial.print(info[1],DEC);
        low=info[0];
        high=info[1];
        result=high<<8;
        //result<<8;
        Serial.print(" ");
        Serial.print(result+low,DEC);
        Serial.print(" ");
    }

}

void printWelcome()
{
    Serial.println("------------------------");
    Serial.println("Data sampling system");
    Serial.println("send r to read disk");
    Serial.println("send s to start sampling");
    Serial.println("send q to stop sampling");
    Serial.println("Ready.....");
    Serial.println("-------------------------");
}


int print_disk_info()
{


    struct sd_raw_info disk_info;
    if(!sd_raw_get_info(&disk_info))
    {
        return 0;
    }


    Serial.println();
    Serial.print("rev:    ");
    Serial.print(disk_info.revision,HEX);
    Serial.println();
    Serial.print("serial: 0x");
    Serial.print(disk_info.serial,HEX);
    Serial.println();
    Serial.print("date:   ");
    Serial.print(disk_info.manufacturing_month,DEC);
    Serial.println();
    Serial.print(disk_info.manufacturing_year,DEC);
    Serial.println();
    Serial.print("size:   ");
    Serial.print(disk_info.capacity,DEC);
    Serial.println();
    Serial.print("copy:   ");
    Serial.print(disk_info.flag_copy,DEC);
    Serial.println();
    Serial.print("wr.pr.: ");
    Serial.print(disk_info.flag_write_protect_temp,DEC);
    Serial.print('/');
    Serial.print(disk_info.flag_write_protect,DEC);
    Serial.println();
    Serial.print("format: ");
    Serial.print(disk_info.format,DEC);
    Serial.println();
    Serial.print("free:   ");

    return 1;
}

How to tell the arduino to create a txt file in the SDcard?

I think the intention is that you create the txt file on a PC before you connect it to the Arduino.

I think the intention is that you create the txt file on a PC before you connect it to the Arduino.

You have to admit, though, this is a pretty severe limitation.

I haven't looked in any detail, but I have often wondered a) why this is the case (that you have to pre-create the files), and b) why hasn't somebody fixed it?

:slight_smile:

Its probably a messy and resources costly bit to implement on an arduino....

oh
then the file is need to be created in the first place.

there is a data, A for instance, i would like to save "A" to the SDcard
i saw that there is a code

int readDisk();

i can delete this section, right? if i just write things into the SDcard.

You have to admit, though, this is a pretty severe limitation.

I haven't looked in any detail, but I have often wondered a) why this is the case (that you have to pre-create the files), and b) why hasn't somebody fixed it?

oh, the limitation you meant is that could not create the txt file automatically?

if i pre-created the file, let's say the file name is B for instance,
how to tell the arduino to save the data into B ?

Are there any others saving method that using the SDcard ?
please suggest it if you have any recommendations , i very appreciate it

It would help a lot to my project,
and seriously, can help me to graduate
Thanks a lot

IIRC it is only the demo program that requires the file to be created.

I believe you want to add the FAT library. This will you give a FAT file system
and you can create, read and write files.

(* jcl *)


www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org

I believe you want to add the FAT library. This will you give a FAT file system
and you can create, read and write files

is it this library ?
http://code.google.com/p/sdfatlib/downloads/list

for the code i posted on third reply, i couldn't find the code of writing data into the SD card , which section in the code that i should look further to change the code that suitable for me ?

is it this library ?
Google Code Archive - Long-term storage for Google Code Project Hosting.

I believe so. Also ladyada has a library too (which I believe was derived from
the one you linked to).

(* jcl *)


www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org

i have some question with the datalogging without the library

there is a tutorial from here : Arduino Playground - SDCARD

the SPI section must be needed ?

and if i need to write thing into the SDcard
these two code, SPI section and writing of SDcard section are both needed ?

/********************** SPI SECTION BELOW **********************/

// SPI Variables
byte clr;     // dummy variable used to clear some of the SPI registers
byte spi_err; // SPI timeout flag, must be cleared manually

// send an SPI command, includes time out management
// returns spi_err: "0" is "no error"
byte spi_cmd(volatile char data) {
  spi_err = 0; // reset spi error
  SPDR = data; // start the transmission by loading the output byte into the spi data register
  int i = 0;
  while (!(SPSR & (1<<SPIF))) {
    i++;
    if (i >= 0xFF) {
      spi_err = 1;
      return(0x00);
    }
  }
  // returned value
  return(SPDR); 
}

// initialize SPI port 
void spi_initialize(void) {
  SPCR = (1<<SPE) | (1<<MSTR); // spi enabled, master mode
  clr = SPSR; // dummy read registers to clear previous results
  clr = SPDR;
}


// write block on SD card 
// addr is the address in bytes (multiples of block size)
void sdc_writeBlock(long blockIndex) {
  byte retries=0;
  while(sdc_cmd(WRITE_BLOCK, blockIndex * blockSize) != 0x00) { 
    delay(1);
    retries++;
    if (retries >= 0xFF) return; // timed out!
  }
  spi_cmd(0xFF); // dummy byte (at least one)
  // send data packet (includes data token, data block and CRC)
  // data token
  spi_cmd(0xFE);
  // copy block data
  for (int i=0; i<blockSize; i++) {
    spi_cmd(vBlock[i]); 
  }
  // write CRC (lost results in blue sky)
  spi_cmd(0xFF); // LSB
  spi_cmd(0xFF); // MSB
  // wait until write is finished
  while (spi_cmd(0xFF) != 0xFF) delay(1); // kind of NOP
}

hello to everyone,
forget the previous code that i posted,
i have found a code that from arduino playground
there is a example from the sdfat library,
but i don't know how to connect the SDcard to the ATmega168
here is the library that i download, have a look please
http://code.google.com/p/fat16lib/downloads/detail?name=fat16lib20100131.zip&can=2&q=

i really really need this datalogging, please help me out, i am going crazy with the code

Here is the SDfat write code,

#include <Fat16.h>
#include <Fat16util.h> // use functions to print strings from flash memory

SdCard card;
Fat16 file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
{
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode) {
    PgmPrint("SD error: ");
    Serial.println(card.errorCode, HEX);
  }
  while(1);
}

/*
 * Write an unsigned number to file
 */
void writeNumber(uint32_t n)
{
  uint8_t buf[10];
  uint8_t i = 0;
  do {
    i++;
    buf[sizeof(buf) - i] = n%10 + '0';
    n /= 10;
  } while (n);
  file.write(&buf[sizeof(buf) - i], i); // write the part of buf with the number
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println();
  PgmPrintln("Type any character to start");
  while (!Serial.available());
  
  // initialize the SD card
  if (!card.init()) error("card.init");
  
  // initialize a FAT16 volume
  if (!Fat16::init(card)) error("Fat16::init");

  // create a new file
  char name[] = "WRITE00.TXT";
  for (uint8_t i = 0; i < 100; i++) {
    name[5] = i/10 + '0';
    name[6] = i%10 + '0';
    // O_CREAT - create the file if it does not exist
    // O_EXCL - fail if the file exists
    // O_WRITE - open for write
    if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) break;
  }
  if (!file.isOpen()) error ("file.open");
  PgmPrint("Writing to: ");
  Serial.println(name);
  
  // write 100 line to file
  for (uint8_t i = 0; i < 100; i++) {
    file.write("line "); // write string from RAM
    writeNumber(i);
    file.write_P(PSTR(" millis = ")); // write string from flash
    writeNumber(millis());
    file.write("\r\n"); // file.println() would work also
  }
  // close file and force write of all data to the SD card
  file.close();
  PgmPrintln("Done");
}

void loop(void) {}

but i don't know how to connect the SDcard to the ATmega168

i am going crazy with the code

Why are you worried about the code if you don't have the hardware connected correctly?

Why are you worried about the code if you don't have the hardware connected correctly?

i am pushing myself too hard, i don't know what to say :frowning:
i am graduating, so there is a lot of stress around me

thanks to you that i have the frequency counter code succeeded, thanks a lot

now i am trying to save the data, i am worrying that if SDcard circuit could help me, here is a question about the SDcard, i saw a schematic inside the fat16library, it is showing the SDcard connection, here it is.

if i need to use the fat16 example from the library, the connection should be like the picture on the above, or the tutorial can also work ?

what is 4050N ?

but i don't know how to connect the SDcard to the ATmega168

There is a very long uSD thread on this forum. IIRC there are links to uSD and SD
card schematics in one of the early posts.

(* jcl *)

what is 4050N

It's a buffer. In this circuit it is translating 5V TTL levels to the 3.3V TTL levels
required by the SD Card.

(* jcl *)