Introduction to Microchip’s MDDFS – Reading and Writing Files (Microchip 16-bit microcontrollers)

This tutorial is written by Ng Yong Han.

Electronic storage is already much cheaper nowadays in the 21st century. Even in the supermarkets, you would find a couple of SD-Cards and USB thumb-drives being sold there. And plus, these storages are more than enough for you to store a lot of your college assignments and documents. What’s best is, you can store your favourite tunes and movies inside, or your greatest vacation photos.

Micro-SD card (source: Kingston Website)

In this tutorial, you will learn how to read and write files in a Micro-SD card using a microcontroller. Do not worry as you will be using Microchip’s Memory Disk Drive File System (MDDFS) libraries to help you to do all of these immediately. One thing about the library is, it requires a lot of microcontroller’s program space. The dsPIC30F4013 will be used in this project since it has 48KB of program memory.

Here’s what you need:

Build the circuit which is as follows:

Click to enlarge

The numbers in brackets at the PICKit 2/UIC00B are connected to the respective pins of the microcontroller. Example, (1) is connected to pin 1 of the microcontroller, and etc.

For your micro-SD card, make sure the file system is in FAT form. Copy the file “hello.txt” into the card, and then insert it into the BB-uSD slot.

Then in the MPLAB X IDE, open the project called “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.

Then follow the instructions as stated below:

  1. Turn on the BPS-5-3. (Do this first before you plug in your PICKit 2/UIC00B!)
  2. Plug in your PICKit 2/UIC00B and launch PICKit 2 program. The VDD target should be “3.3V”.
  3. Import Hex, and locate the directory “mddfs-tutorial.X\dist\default\production”. Once you are inside, click on the file “mddfs-tutorial.X.production.hex”.
  4. Wait for it to upload this program into the microcontroller.
  5. Launch Realterm or other equivalent software and adjust the settings first.
Make sure the serial settings are as follows:
Baud: 9600
Port: (Your COM port for the Serial to USB adapter)
Data bits: 8
Stop bits: 1
Hardware Flow Control: None6. Reset the microcontroller by pushing the button in that project.

7. You will see the messages:

And afterwards, the LED is blinking. (Ignore the LF – it’s the new line thing) This shows that the program is working. Now what should you do now? Let’s go check out what it has written into the card…

In the card, open the “HELLO1.TXT”. You will see that the software inside has created a file, and written something inside.

How does this software work, anyway? It’s pretty simple. The MDDFS has already did a lot of work for you, and only thing you should concern about how to use the functions.

Go back to the MPLAB X IDE, and double click the “main.c” at the “Projects” tab on your left.

On the first few more lines on the screen, the file pointers must be initialized. All operations on the file is done on the file pointers.

The buffer is just to store a sentence which will be explained later in the tutorial.

Scroll to around lines 57-59. Each function is explained individually in red numbers.

(1) This function, MDD_MediaDetect()returns 0 when there is no media detected. For the sake of convenience, the system keep checking for a card until 1 is returned. The returned value of 1 means that the card is present.
(2) After the card is detected, the system needs to check for the file system in the card. The FSInit() function does all these. If the file system is not present or faulty, it returns 0. Similarly to (1), the system keeps checking for the condition of the file system – if it’s not present or not good, it will keep doing that until the 1 is returned.
(3) The file is opened, which is called “hello.txt” and it’s for reading, in the second parameter. (“r”) If the file does not exist, (the fPointer is null or zero) the system will print out “File does not exist!” in the Hyperterminal and stops there. Else, the program continues.
(4) The function reads 31 bytes of stuff from the fPointer, and then placed into the tempBuffer array. The null-termination ‘’ is placed at the 31st element of the array so that the system prints the correct amount of characters in the array.
(5) Printing the buffer to the Hyperterminal, where you will see: “Hello World! This is a string.”
(6) Safely close the file pointer. You must do this after you worked with the file and the file pointer.
(7) Now, the file is opened again, but instead of opening an existing file, it’s creating a new file. Note the “w” inside the function, and a new name called “hello1.txt”. That’s why you find a new file (HELLO1.TXT) after launching this program.

(8) The FSfprintf prints text into the file. As in (7) you need to open the file for writing first. This is almost similar to the fprintf in C programming. Please refer to the AN1045 Technical Manual for more information on the FSfprintf function.
(9) Safely closes the file again after modifying/reading a file.
(10) When all is done, the LED is blinking indefinitely. That’s about it.

For more details on the functions please read the manual supplied (MDDFS Library Help – PDF).

You can experiment with these functions in the tutorial for now. If you are wondering why the newly created file has no date, it is because of the system does not put a timestamp on the file. Plus, you can only use 8 letters on each file and the system cannot open/create directories. To use directories manipulation, long file names and FAT32 support, you must enable such functions in the “FSConfig.h”. This will be discussed in the next tutorial.

That’s all for now. There will be more on that, so stay tuned for the next one.

Attachment:

1. Introduction to microchip’s MDDFS – dsPIC30F4013

Related post:

1. Microchip’s MDDFS for SKPIC32 – Reading and Writing Files (Microchip PIC32 microcontrollers)

References:

1.) Microchip AN1045: File I/O Functions Using Microchip’s Memory Disk Drive File System Library

2.) MDDFS Library Help

Buy

, , ,

Related Post

FatFS for PIC32MX/MZ

Serial Loopback test

Microchip’s MDDFS for SKPIC32 – Reading and Writing Files (Microchip PIC32 microcontrollers)

Remote Control of Tilt and Pan Camera using Microcontroller

13 thoughts on “Introduction to Microchip’s MDDFS – Reading and Writing Files (Microchip 16-bit microcontrollers)

  1. Hi,

    I am playing with MDDFS-SD project. I got one question how to add more than one new line when writing to sd card.
    I tried

    FSfprintf(pointer,”Creating a file!%c%c%c”,0x0D,0x0A,0x0A);
    but does not give me one more new line.

    Let me know your comments.

    Lasa

  2. I am totally new to PIC as well as MPLAB X so i am not getting this right whenever i open project provided by you i get error that FSIO.h files do not exist in the disk……i have installed the .exe given by microchip for the libraries….but i am not getting that files…can u pls help me how to get that????

  3. You cannot use XC8 to compile. You have to use the hi-tech c18 compiler. Not sure why the author of this website is telling you to use xc8. It will not work for this project.

  4. Hi, What modification did you made to ( hardwareProfile.h ) to make the library work with the dsPIC30F4013?
    Cheers

  5. Hallo and thanks for the project.

    Are there any examples how to show a bitmap File with the library from the SD Card to a LCD Display?

    Thanks in Advance!

  6. Good afternoon, I’m testing your project with a 30F4013, it worked correctly, but when I do the code migration to use a 30F4011 I can not connect to the micro SD, the pins, and modules are the same in both dspic, but I do not know why It does not work when I use it with a 30F4011, can you please help me? I’ve been trying this for several days and I’m not able to communicate with the 30F4011. Any help would greatly appreciate it as I urgently require it.

  7. I am the writer of the tutorial. As the dsPIC’s MDDFS libraries not being compatible with the newer Harmony libraries, the tutorial is no longer applicable.

    Feel free to port the modified versions (thanks to Aiden Morrison) of the FatFs from my github: https://github.com/uncle-yong/sk1632-fatfs

    The FatFs is much simpler and robust than the Harmony versions of the SD-card drivers, and they are much more portable. Therefore, when there is a huge platform change, the FatFs is mainly unaffected, allowing you to move the drivers to a new platform without a lot of modifications.

Leave a Reply

Your email address will not be published. Required fields are marked *