Watch the Video Tutorial part 1 

A memory card (also called a flash memory card) is a solid-state electronic data storage device used for storing digital information. They are commonly used in many electronic devices, including digital cameras, mobile phones, laptop computers, MP3 players etc.
They are small, re-writable and are able to retain data without power.
This ability to retain data and the ability to re-write many times is the key for flash memory card applications, for example, in digital cameras, where the saved pictures are not lost after the memory card is removed from the camera.
There are many different types of memory cards available in the market today. Some of the most commonly known memory cards are:

  • Smart media (SM) card
  • Multimedia card (MMC)
  • Compact flash (CF) card
  • Memory stick (MS) card
  • Microdrive
  • xD card
  • Secure digital (SD) card: SD cards are the most widely used memory cards today. They are based on MMC and in terms of functionality they are the same ( except that the SD has an optional encryption feature). The SD are physically thicker than MMCs and would not fit into MMC slots. The MMC, on the other hand, can be easily inserted into SD card slots.

The circuit diagram above on figure 1 below shows an SD Card and LCD Displayed interfaced to PIC18F45K22.
To learn more on SD Card operation mode, connection and technical properties, please refer to the article: Interfacing SD Card with PIC Microcontroller.

SD Card and LCD Display connected to PIC18F45K22

Figure 1: SD Card and LCD Display connected to PIC18F45K22

Flowcode provides a component for MMC/SDC routines, the FAT Component. This component can be used with any  SD / MMC card that is formatted to the FAT16 or FAT32 file structure.

Watch the Video Tutorial part 2

FAT Component

Figure 2: FAT Component.

—>>To insert a FAT component, on the components tool bar click on FAT under the Peripheral group.
A FAT component (FAT16(0)) will be inserted on the panel.

Inserting a FAT component

Figure 3: Inserting a FAT component

—>>select the FAT component then click on the “….” next to the Ext Properties to edit the FAT properties.

Figure 4: FAT Properties

SPI:
The default connection mode is SPI using the first Hardware Channel (SPI1) of the PIC. If you need to select a different SPI channel (some PICs have more than 1 SPI channel), select one from the HW Channel drop down box. Flowcode will automatically select your pins except the Chip Select pin that you have to select manually (Figure 5).
When the software SPI mode is ticked, the FAT software interfaces with the memory card via a four wire SPI bus, you will have to specify yourself the Chip Select, Clock, data In and Data Out pins in the component connection window (Figure 6).
System:
FAT16: Sets the recognised FAT type to cards formatted with FAT16 only (for cards with capacity below 2GB).
FAT32 file systems will not work but allows for smaller memory usage.

FAT32: Sets the recognised FAT type to cards formatted with FAT32 only (for cards with capacity between 2GB and 2TB). FAT16 file systems will not work but allows for smaller memory usage as well.

Auto Detect: Allows the component to auto-detect the type of FAT file system being used on the card however uses slightly more ROM and RAM

—>>select the FAT component again and click on the “….” next to connections properties of the FAT component to open its connection properties.

     

            Figure 5: Connection with SPI mode unticked         Figure 6:Connection with SPI mode ticked

 Component Macros

Drag and drop the Component Macros from the Icons toolbar.

Figure 7: Component Macro

Double click the component macro to open its properties. Click on the FAT16(0) to see its macros.
NB: Currently these file manipulation functions are not supported in the FAT component (V5.0.3):
–>Long file names
–>Deleting data from a file
–>Deleting directories
–>Directories other then root cannot have more then 512 entries including additional long file name entries.
–>The component requires a internal buffer of 512 bytes as well as enough RAM to handle the file system variables. Therefore the microcontroller must have enough resources to support this requirement.
Not all PIC Microcontrollers can handle SD Cards but most of the PIC18 series have enough RAM. Check your datasheet for more info.

Return Codes
Whenever a macro is called, there are a number of possible values that are returned depending on whether the call was successful or not:
0xFF – No Card Error
0xFE – No Response Error
0xFD – No Ack Error
0xFC – Timeout Error
0xFB – Reserved Error
0xFA – Bad Sector Error
0xEF – File Not Found Error
0xEE – Root Full Error
0xED – Sector Bytes Error
0x03 – File Empty
0x02 – File Type
0x01 – End Of File
0x00 – Ok

Let us declare some of the variables that we are going to use.
Drag and drop a calculation icon and declare these variables:
Byte retval   //Return value

String fileName[20] = “Test.txt”                 //File name to create
String fileContent[4] = “writing to SD Card with Flowcode” //The content to write onto the card

Figure 8: Declaring variables

Init_Fat ( void ) 
Initialises the card and starts up the FAT driver, points the directory to root. Current directory is specified as Root.
This macro should always be called before other macros.
retval = 0 – Initialize OK
retval > 0 – Initialize Not OK

Figure 9: Initialize SD Card

Create_File( “String” ) 
This macro creates a file with the specified file name in the current directory of the card. It also assigns the file certain parameters such as create time and date and size etc.
The expression of the file name can either be a string variable or directly write a string name surrounded by double quotes.
retval = 0 – File created successfully
retval > 0 – Directory table of the current folder is full.

Figure 10: Create a file

Delete_File( “String” ) 
This macro scans the current directory on the card for a specific file name. If the file name is found then the file is deleted from the disk.
Expression: file name (string) to be deleted.
retval = 0 – File found and deleted successfully.
retval > 0 – File could not be found

Open_File( “filename” )
This macro searches for a file in the current directory and reads the first sector of the file into memory.
Expression: file name (string) to be open.
retval = 0 – File found and opened – 512-byte buffer contains 1st sector of file
retval = 1 – File could not found
retval > 1 – Error

Figure 11: Open a file

Append_String_To_File( “String” ) 
This macro appends a string or byte array of data to the end of the file that is currently open and updates the size parameter of the file. Note the file has to be open first before before appending it.
Expression: a string variable containing the String or byte array to be appended to the end of the file
retval = 0 – String or byte array has been appended successfuly.
retval > 0 – An error occured while appending the string.

Figure 12: Append data to file

Right click on the FAT16 component and select help for more information.

Figure 13: FAT Help

Full Flowcode project 

The circuit diagram of the project is shown on figure 1:
When the project starts:
1. “Waiting for card” is displayed on the LCD,
2. The card is initialed, if it is found and correctly formatted, “Creating file” is displayed on the LCD.
3. “Test.TXT” file is created on the card. if this file already exists, “File exists” is displayed otherwise “File created” is displayed.
4. “Opening File” is displayed. Test.TXT is open. If failed to open “File error” will be displayed and the program will terminate otherwise “File opened” will be displayed.
5. “Writing Content” is displayed. “Writing SD Card with Flowcode” is appended to Test.TXT.
If written successfully, “Content Written” will be displayed otherwise “Data Error” will be displayed.

You can download the full project files (Flowcode and Proteus Schematic design) below here.
All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

Download SD Card Flowcode Project
Download SD Card Flowcode Proteus