Example Code

MDD File System Interface Library

MDDFS Interface Library Help
Example Code

  • Below is an example on PIC24/PIC32/dsPIC devices which shows how to create a file, write a file, read a file, close a file...etc...

   FSFILE * pointer;
   char sendBuffer[] = "This is test string 1";
   char receiveBuffer[50];

   // Wait in while loop until the physical media device like SD card, CF card or
   // USB memory device is detected in the software...
   while (!MDD_MediaDetect());

   // Initialize the file system library & the physical media device
   while (!FSInit());

   // Create a file
   pointer = FSfopen ("FILE1.TXT", "w");
   if (pointer == NULL)
      while(1);

   // Write 21 1-byte objects from sendBuffer into the file
   if (FSfwrite (sendBuffer, 1, 21, pointer) != 21)
      while(1);

   // Close the file
   if (FSfclose (pointer))
      while(1);

   // Open file 1 in read mode
   pointer = FSfopen ("FILE1.TXT", "r");
   if (pointer == NULL)
      while(1);

   // Renames the file FILE1.TXT to Microchip File 2.TXT
   if (FSrename ("Microchip File 2.TXT", pointer))
      while(1);

   // Read one four-byte object
   if (FSfread (receiveBuffer, 4, 1, pointer) != 1)
      while(1);

   // Close the file
   if (FSfclose (pointer))
      while(1);

  • Below is an example on PIC24/PIC32/dsPIC devices which shows how to create a directory, change the current working directory, delete the directory...etc...

   // Wait in while loop until the physical media device like SD card, CF card or
   // USB memory device is detected in the software...
   while (!MDD_MediaDetect());

   // Initialize the file system library & the physical media device
   while (!FSInit());

  // Create a small directory tree
   // Beginning the path string with a '.' will create the directory tree in the
   // current directory.
   if (FSmkdir (".\\Mchp Directory 1\\Dir2\\Directory 3"))
      while(1);

   // Change to current working directory to 'Directory 3'
   if (FSchdir ("Mchp Directory 1\\Dir2\\Directory 3"))
      while(1);

   // Create another tree in 'Directory 3'
   if (FSmkdir ("Directory 4\\Directory 5\\Directory 6"))
      while(1);

   // This will delete Directory 5 and all three of its sub-directories
   if (FSrmdir ("Directory 4\\Directory 5", TRUE))
      while(1);

   // Change directory to the root dir
   if (FSchdir ("\\"))
      while(1);

  • Below is an example on PIC24/PIC32/dsPIC devices which shows how to create files,search files & delete specific file

   FSFILE * pointer;
   char sendBuffer[] = "This is test string 1";
   unsigned char attributes;
   unsigned char size = 0, i;

   // Wait in while loop until the physical media device like SD card, CF card or
   // USB memory device is detected in the software...
   while (!MDD_MediaDetect());

   // Initialize the file system library & the physical media device
   while (!FSInit());

   // Create a file FILE1.TXT
   pointer = FSfopen ("FILE1.TXT", "w");
   if (pointer == NULL)
      while(1);

   // Write 21 1-byte objects from sendBuffer into the file
   if (FSfwrite (sendBuffer, 1, 21, pointer) != 21)
      while(1);

   // Close the file
   if (FSfclose (pointer))
      while(1);

   // Create a file FILE2.TXT
   pointer = FSfopen ("FILE2.TXT", "w");
   if (pointer == NULL)
      while(1);

   // Write 21 1-byte objects from sendBuffer into the file
   if (FSfwrite (sendBuffer, 1, 21, pointer) != 21)
      while(1);

   // Close the file
   if (FSfclose (pointer))
      while(1);

   // Create a file FILE3.TXT
   pointer = FSfopen ("FILE3.TXT", "w");
   if (pointer == NULL)
      while(1);

   // Write 21 1-byte objects from sendBuffer into the file
   if (FSfwrite (sendBuffer, 1, 21, pointer) != 21)
      while(1);

   // Close the file
   if (FSfclose (pointer))
      while(1);

   // Set attributes
   attributes = ATTR_ARCHIVE | ATTR_READ_ONLY | ATTR_HIDDEN;

   // Functions "FindFirst" & "FindNext" can be used to find files
   // and directories with required attributes in the current working directory.

   // Find the first .TXT file with any (or none) of those attributes that
   // has a name beginning with the letters "FILE" in your current working
   // directory
   if (FindFirst ("FILE*.TXT", attributes, &rec))
      while(1);

   // Keep finding files until we get FILE2.TXT
   while(rec.filename[4] != '2')
   {
      if (FindNext (&rec))
         while(1);
   }

   // Delete FILE2.TXT
   if (FSremove (rec.filename))
      while(1);

Note:

Microchip MDD File System Interface 1.4.2 - [Oct 15, 2012]
Copyright © 2012 Microchip Technology, Inc.  All rights reserved.