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:
- Compiler: MPLAB X 1.41, MPLAB XC16 1.10, MDDFS v2012-08-22
- Microcontroller: ds PIC30F4013
- Cytron BPS-5-3 Breadboard Power Supply Unit
- Cytron BB-uSD micro-SD card slot
- Cytron UIC00B Programmer
- Cytron UIC-A
- Cytron UC00B Serial-to-USB
- 1x 220 ohms resistor, 1x 1K resistor, 1x 10K resistor and one push-button
- 1x Green LED
- 2x 22pf capacitors
- 1x 8MHz crystal
- 1x Breadboard
- 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!)
- And finally, some jumper wires.
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 toTutorial: 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:
- Turn on the BPS-5-3. (Do this first before you plug in your PICKit 2/UIC00B!)
- Plug in your PICKit 2/UIC00B and launch PICKit 2 program. The VDD target should be “3.3V”.
- 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”.
- Wait for it to upload this program into the microcontroller.
- Launch Realterm or other equivalent software and adjust the settings first.
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.