■ FIGURE 9. Write demo LCD.
■ FIGURE 8. Write demo code.
there is an un-lock and lock sequence that surrounds this PPS
programming to insure that this critical function is not invoked
haphazardly. Also take note that only #include FSIO.h is
needed in Main () to reference the entire Microchip library.
DEMOWRITE - Open a Text
File and Write Data to It
Our first example creates a file and writes the
■ FIGURE 10. Read demo code.
following line to it: “This files I/O test
using Microchip library.“ The LCD
library is used to display the status.
Download and unzip WRITEDEMO
from the website. Open the folder and
double-click on MDDFS_SD_PIC24.mcp.
In Step 1, we call MDD_MediaDetect ().
This function determines if the SD card
is present. After Step 1, we then execute
Step 2: FSInit (). This function initializes
the library and the SPI port. Step 3
opens a file “FILE1. TXT” with write or
the “w” attribute. In this step, the FSfopen () will
create the file if it doesn’t exist; if it does exist, it will
clear it of previous data. Step 4 does the actual write
of data to the file using FSfwrite (). This function takes
data preloaded in the send buffer array and writes it
to the file one byte at a time. In Step 5, we finally close
the file with FSfclose (). After executing WRITEDEMO,
we can remove the SD card from the Experimenter and
place it in the PC SD card reader to verify the file
contents. With the SD card in the PC, it will register the
card as a hard drive.
Open up the hard drive and note that
FILE1. TXT exists; double-click on the file
(Windows Notepad should automatically open it)
and the proper text should be displayed. It
should read “This a file I/O test using
We can create files with any extension
that Windows can recognize. However,
. TXT is the simplest. This format works well
for data written in an ASCII format. If you write
lots of data records to your file, keep in mind
that you need to use some kind of delimiter
between your data records like a space or
comma.
■ FIGURE 11. Read demo LCD.
50
October 2010