This tutorial is written by Ng Yong Han.
The MDDFS library which is already covered in the previous tutorial can be also used for the PIC32 microcontroller. Please refer to the previous tutorial if you are new to the MDDFS library. The best thing about the PIC32 microcontroller is it has a huge program space, more RAM, and finally, more horsepower than the other PIC microcontrollers available.
This time, look at the FSConfig.h file. Look around it – you will see more features already enabled inside compared to the last tutorial. The ones which are enabled are not commented out.
The first thing you see is this:
This means “Long File Name”. And it is self-explanatory. You can open, read or create files with names like “It’s fun doing programming.txt”. If you are making your own data-logger, why not “Sunday 07-07-13” as a file name?
Then, you may need to find a file. Where’s that “Monday.txt” you’ve placed in the SD-Card? Enabling this allows you to search for a file in the SD-Card.
If you need to organize files inside the card, create folders! Here’s that special feature for you to manipulate folders. Delete, rename, create at your own with this function.
With all the huge SD-Cards (more than 4 gigabytes) in hypermarkets like Tesco or Carrefour in lower prices, it is not surprising that you may need to enable this function to accommodate the size of these cards.
In this tutorial you learn how to manipulate files and folders in SD-Card using SKPIC32. Here are what you need in this tutorial:
Compiler: MPLAB X 1.85, MPLAB XC32 1.21, MDDFS v2013-02-15
Cytron SKPIC32, running at 32MHz
Seven male-male or more jumper wires
PICKit 3 Programmer
Your micro-SD card. (Note: Make backups on your data in your current card before you attempt to experiment with it! Or, use a spare micro-SD card instead. Cytron is not responsible for lost data and destroyed card!)
Build the circuit which is as follows:
For the SD-Card, the file system can be FAT or FAT32. If you have a bigger SD-Card, it is usually formatted in FAT32.
Then in the MPLAB X IDE, open the project called “skpic32-mddfs-tutorial.X” (make sure you extract and put the folder into your hard-disk first). You can refer to Tutorial: MPLAB X IDE + XC8 Compiler – Create project, build and load program if you are just getting started with MPLAB X IDE.
You will see the C and the header files listed exactly like in this picture. Make sure you collapse the folders first to see the contents. Press the Hammer button to compile the program. It should be successfully compiled.
Next, connect the PICKit 3 to the ICSP header you have soldered to the SKPIC32. Click this button to compile it again and copy the program into the SKPIC32.
Launch Realterm or other terminal software once the program is already downloaded into the SKPIC32. You will have to press “reset” on the SKPIC32 (the white button) for the messages on the screen to come out again.
If you can see this after you reset the microcontroller, the program is working. You may need a few more seconds for all of the messages to come out as creating a new file is a slow process in the system.
When you have seen this, take out the SD-Card and put it into your laptop or a computer. You will see a file and a folder being created.
In the picture, you see these folders are created at the 7th of July 2013, at 10:10 AM. It is because in the file main.c the “SetClockVars” function is used. Open the “main.c” in the MPLAB also to look at the new functions introduced.
Place the year, month, day, hour (in 24-hour format), minutes and seconds into the function and you will put a timestamp into the manipulated files/folders.
If you have a DS1307 (real time clock chip) connected with the SKPIC32, why not take out the current time and write it into the function? It’s pretty neat.
Next, the program attempts to search for the “hello1.txt” file you have created earlier, in the SD-Card. If the function FindFirst finds that particular file, it will return a zero, or else a -1. You need to create a search record first prior to searching the file, which is “SearchRec f_search”. You can replace “f_search” with other names in your project, as long as you can understand it.
Creating folders is an easy task too. You just type a name for your folder as a string and pass it into the “FSmkdir” function. Place a double slash “\\” prior to typing the name of the folder. In the example, the folder1 is empty. As you have the AN1045 in your hands, the rest of the directory related functions are left as an exercise.
That’s all for now. In conclusion, the MDDFS library is a very convenient tool for manipulating files in SD-Cards. The power of PIC32 allows you to have more MDDFS features inside and also to help you to improve your next invention. Have fun with it!
Introduction to Microchip’s MDDFS – Reading and Writing Files (Microchip 16-bit microcontrollers), http://tutorial.cytron.com.my/2012/11/03/introduction-to-mddfs/
Microchip AN1045: File I/O Functions Using Microchip’s Memory Disk Drive File System Library.