7 Code Description: Uses an Arduino Duemillanove or Arduino Pro to read the file contents of each file on an SD card.
9 Circuit Description: Uses the SparkFun microSD shield. (http://www.sparkfun.com/commerce/product_info.php?products_id=9520)
11 Attributions: Special thanks to Roland Riegel for providing an open source FAT library
12 for AVR microcontrollers. See more of his projects here:
13 http://www.roland-riegel.de/
15 This code is provided under the Creative Commons Attribution License. More information can be found here:
16 http://creativecommons.org/licenses/by/3.0/
18 (Use our code freely! Please just remember to give us credit where it's due. Thanks!)
21 //Add libraries to support FAT16 on the SD Card.
22 //(Note: If you already have these libraries installed in the directory, they'll have to remove in order to compile this.)
23 #include <byteordering.h>
25 #include <fat_config.h>
26 #include <partition.h>
27 #include <partition_config.h>
28 #include <sd-reader_config.h>
30 #include <sd_raw_config.h>
32 //Define the pin numbers
38 //This is the amount of data to be fetched from the SD card for each read.
39 #define BUFFERSIZE 256
41 unsigned char buffer[BUFFERSIZE];
44 struct fat_dir_struct* dd; //FAT16 directory
45 struct fat_dir_entry_struct dir_entry; //FAT16 directory entry (A.K.A. a file)
47 struct fat_fs_struct* fs; //FAT16 File System
48 struct partition_struct* partition; //FAT16 Partition
50 struct fat_file_struct * file_handle; //FAT16 File Handle
54 //Set up the pins for the Serial communication
59 //Set up the pins for the microSD shield
61 pinMode(MOSI, OUTPUT);
69 int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card.
71 init_filesystem(); //Initialize the FAT16 file system on the SD card.
73 //Get the next file in the directory
74 while(get_next_filename(dd, file_name)){
76 file_handle=open_file_in_dir(fs, dd, file_name);
77 //Read up to 512 bytes from the file
78 bytes_read = fat_read_file(file_handle, buffer, BUFFERSIZE);
79 //Print whatever we just got from the file
80 Serial.println((const char *)buffer);
81 //Keep reading from the file until we reach the end (nothing more is read from the file)
82 while(bytes_read > 0){
83 //If there's more to be read from the file, go get it.
84 bytes_read = fat_read_file(file_handle, buffer, BUFFERSIZE);
85 //Print the contents that have been read
86 Serial.println((const char *)buffer);
88 //Close the file before moving on to the next one.
89 fat_close_file(file_handle);
91 Serial.println("Next File...");
96 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry)
98 fat_reset_dir(dd); //Make sure to start from the beginning of the directory!
99 while(fat_read_dir(dd, dir_entry))
101 if(strcmp(dir_entry->long_name, name) == 0)
111 struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
113 struct fat_dir_entry_struct file_entry;
114 if(!find_file_in_dir(fs, dd, name, &file_entry))
117 return fat_open_file(fs, &file_entry);
120 char init_filesystem(void)
128 //open first partition
129 partition = partition_open(sd_raw_read,
130 sd_raw_read_interval,
131 #if SD_RAW_WRITE_SUPPORT
133 sd_raw_write_interval,
143 //If the partition did not open, assume the storage device
144 //is a "superfloppy", i.e. has no MBR.
145 partition = partition_open(sd_raw_read,
146 sd_raw_read_interval,
147 #if SD_RAW_WRITE_SUPPORT
149 sd_raw_write_interval,
163 fs = fat_open(partition);
169 //Open root directory
170 fat_get_dir_entry_of_path(fs, "/", &dir_entry);
171 dd=fat_open_dir(fs, &dir_entry);
180 char get_next_filename(struct fat_dir_struct* cur_dir, char * new_file)
182 //'dir_entry' is a global variable of type directory_entry_struct
184 //Get the next file from the root directory
185 if(fat_read_dir(cur_dir, &dir_entry))
187 sprintf(new_file, "%s", dir_entry.long_name);
188 Serial.println((const char *)new_file);
191 //If another file isn't found, return 0