I am using a PIC18F4620 and an XC8 compiler. I am trying to create a file and send it to the SD card. I am using the MDD library from the MLA Legacy. My main is:

FSFILE * file;
char sendBuffer[22] = "This is test string 1";

// main function
int main()
    PORTBbits.RB0 = 0;
        // Initialise SPI and SD-card
        while ( !MDD_MediaDetect() )



        // Initialize the library
        while ( !FSInit() )

    #ifdef ALLOW_WRITES

        // Create a new file
        file = FSfopenpgm ( "FILE.TXT", "w" );
        if ( file == NULL )

        // Write 21 1-byte objects from sendBuffer into the file
            if ( FSfwrite ( (void *) sendBuffer, 1, 21, file ) != 21 )

        // Close the file
            if ( FSfclose ( file ) )



    //    Delay10KTCYx(100000000);

    PORTBbits.RB0 = 1;         //LED  
    return (0);

Does the code look OK? I am not sure about the MDD_SDSPI_SectorWrite function, and I don't know how to find the address of the sector in which I want to write the file. I know the error handling is bad. I will change it eventually.

  • \$\begingroup\$ Why are you trying to bypass the filesystem write functions by calling SectorWrite directly? \$\endgroup\$ – brhans Jul 1 '15 at 15:52
  • \$\begingroup\$ @brhans Should I call BYTE MDD_SDSPI_AsyncWriteTasks(ASYNC_IO* info); instead? \$\endgroup\$ – user2344158 Jul 1 '15 at 17:29
  • \$\begingroup\$ I would expect that you should just use FSfwrite \$\endgroup\$ – brhans Jul 1 '15 at 17:48
  • \$\begingroup\$ @brhans I think I have misunderstood the way the file system works. The file that created after calling the FSfopenpgm function is located in RAM on the PIC. How can that file be moved in the SD card instead. Correct me if I am wrong \$\endgroup\$ – user2344158 Jul 1 '15 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.