1. Introduction

Micro SD stands for Micro Secure Digital. This tutorial will teach you how to use the Micro SD card. The FATFS library is used for this. This is a common used FAT library. A website with more info about FATFS can be found here: For this tutorial the SD library is required, this library can be downloaded from from our repository.

2. Includes


To begin it is needed to include the libraries. This is done by these commands:
#include "SD/ff.h"
#include "SD/init.h"
#include "SD/monitor.h"

The SD card library has to be placed in a separate folder called SD, see the CoIDE tutorial (APN0008) on how to do this.
If a different folder name is used the include names has to be changed
The libraries for the microcontroller itself and the variables have to be included to:

#include “lpc17xx.h”
#include “lpc_types.h”

3. Variables and initializing

A number of variables is needed for the SD card:
       DSTATUS errd;                           //error var for Disk
       FRESULT errf;                            //error var vor FAT
       char *ptr;                                     //path pointer
       UINT s2, cnt = 8*512;                //Counter array
       char Buff[8*512];                      //FAT buffer
       FATFS Fatfs[1];                           //Fat file system var
       FIL file1;                                       //File var
The SD card and FAT file system have to be initialized to:
 errd = disk_initialize(0);
 errf = f_mount(0, &Fatfs[0]);
If the initialize is succesfull errd will contain the value 0 and errf will contain the value 0 or FR_OK. On the bottom of this tutorials the error codes are listed.

4. Read and write

For this example reading and writing to a text file will be explained. First the file has to be opened. The command for this is FRESULT = f_open(File, Path, Mode); FRESULT is the variable errf declared earlier. File is the file, called file1, declared above. Path is the path where the file is located. Mode is the file mode used to open the file.
There are different modes of opening a file. The modes are:


FA_READ and FA_WRITE are self explaining. There are some more commands. Those are explained on the bottom of the tutorial. A correct command to open a file called "readthis.txt" for reading is:

errf = f_open(&file1, "readthis.txt", FA_READ );

After a file is opened it is possible to read and write from that file. The command to read data from the file is: FRESULT = f_read(File, Buffer, bytes to read, bytes done); File is the opened file. Buffer is a buffer where all the data will be placed in. Buff is a variable declared earlier for this. Bytes to read is the amount of bytes that will be read from the file. This can be a variable or just a number. Bytes done is a variable that will contain the number of bytes that have been read successfully. A correct command to read 1023 bytes of data is:
errf = f_read(&file1, Buff, 1023, &s2);

sd2 is a variable declared above that can be used to store the number of succeeded bytes in.
To write to a file it has to be opened with FA_WRITE or FA_OPEN_ALWAYS. The command to write to a file is: FSTATUS = f_write(File, Data to be written, Number of byte to write, Bytes done); File is the opened file. Data to be written can be the Buff variable or just a string of text. Number of bytes to write can be a variable or just a number. Bytes done is a variable that will contain the number of bytes that have been read successfully. A correct command to write a string of text to a file is:

errf = f_write(&file1, "Hello world ", 11, &s2);

When you are done with reading/writing to a file ALWAYS close the file! Not doing this can result in hard to find bugs. The command to close a file is FRESULT = f_close(file); A correct command for closing a file is:

errf = f_close(&file1);

5. Examples

There are two examples included. There examples are a bit bigger than the other examples. One example measures the temperature with an external LM335 sensor and logs this to the SD card. The other example reads a text file and displays it on a LCD screen.
The LM335 is a cheap temperature sensor. The output of the sensor has to be connected to a Analog input. The voltage on the output of the LM335 is 10mV per degree Kelvin. So 10mV for every degree Celsius above absolute zero. 20 degrees Celsius would be 273 + 20 * 10mV = 2.93V. The example reads the temperature from the LM335, converts this to degrees Celsius and prints this to the SD card. The example is just for reference, It doesn't display the temperature correctly, instead of 24.5 degrees it will display 245 degrees. A schematic how to connect the LM335:


The other example requires the readthis.txt file to be on the SD card. This file is included in the examples. It reads the file and displays the text on a LCD screen. The example has the drivers for this display: http://www.adafruit.com/products/376
For more information, see the LCD tutorial.

Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics