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?
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 ?
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 ?
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) {}
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
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 ?