SD Cards Fat files

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Commander_Bob
Posts: 20
Joined: 02 Sep 2007 16:23

SD Cards Fat files

#1 Post by Commander_Bob » 17 Oct 2007 05:09

I am tiring to use the FAT 16 Library but I have been having trouble. I am using the example with some changes for a dsPIC33FJ128GP710 running at 80MHz (10MHz with PLL) I receive s in the UART terminal but nothing else, the LED on the lines for the SD card don't change. What happened? This happens with two of my SD cards, both are 256MB, one is SanDisk and one is Matchspeed. Here is the modified code (also the project file was changed to use PLL and the dsPIC33)

Thanks,
Justin

Code: Select all

/*
 * Project name:
     Mmc_Fat16_Test (Demonstration on usage of Mmc_Fat16 library)
 * Platforms:
     dsPIC, PIC;
 * Copyright:
     (c) MikroElektronika, 2005 - 2006
  * Revision History:
      20060420:
        - Initial release for the dsPIC platform;
      20060201:
        - added test for swap file creation, to reflect changes in the
          mmc_fat16 library;
      20051204:
        - added test for file delete, to reflect changes in the mmc_fat16 library;
      20050512:
        - initial release;
 * Description:
     This project consists of several blocks that demonstrat various aspects of
     usage of the Mmc_Fat16 library. These are:
     - Creation of new file and writing down to it;
     - Opening existing file and re-writing it (writing from start-of-file);
     - Opening existing file and appending data to it (writing from end-of-file);
     - Opening a file and reading data from it (sending it to USART terminal);
     - Creating and modifying several files at once;
     - Reading file contents;
     - Deleting file(s);
     - Creating the swap file (see Help for details);
 * Test configuration:
     MCU:             PIC24FJ96GA010
     Dev.Board:       LV 24-33
     Oscillator:      XT-PLL, 10.000MHz
     Ext. Modules:    None.
     SW:              mikroC for dsPIC30/33 and PIC24 v3.0.0.0
 * NOTES:
     - Please make sure that MMC card is properly formatted (to FAT16 or just FAT)
       before testing it on this example!
     - Enable MMC on switch SW1
     - This example expects MMC card to be inserted before reset, otherwise,
       the FAT_ERROR message is displayed!!!
 */

#include <spi_const.h>

const char SWAP_FILE_MSG[] = "Swap file at: ";

char
 fat_txt[20] = "FAT16 not found",
 file_contents[50] = "XX MMC/SD FAT16 library by Anton Rieckert\n";

char
 filename[14] = "MIKRO00xTXT";          // File names
unsigned
 loop, loop2;
unsigned short
 caracter;
unsigned long
 i, size;
char Buffer[512];

//I-I-I--------- Writes string to USART
void I_Write_Str(char *ostr) {
  unsigned i;

  i = 0;
  while (ostr[i]) {
    Uart1_Write_Char(ostr[i++]);
  }
}//~

//M-M-M--------- Creates new file and writes some data to it
void M_Create_New_File() {
  filename[7] = 'A';
  Mmc_Fat_Assign(&filename, 0xA0);         // Will not find file and then create file
  Mmc_Fat_Rewrite();                    // To clear file and start with new data
  for(loop = 1; loop <= 99; loop++) {   //  We want 5 files on the MMC card
    Uart1_Write_Char('.');
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, 42);   // write data to the assigned file
  }
}//~

//M-M-M--------- Creates many new files and writes data to them
void M_Create_Multiple_Files() {
  for(loop2 = 'B'; loop2 <= 'Z'; loop2++) {
    Uart1_Write_Char(loop2);             // signal the progress
    filename[7] = loop2;                 // set filename
    Mmc_Fat_Assign(&filename, 0xA0);        // find existing file or create a new one
    Mmc_Fat_Rewrite();                   // To clear file and start with new data
    for(loop = 1; loop <= 44; loop++) {
      file_contents[0] = loop / 10 + 48;
      file_contents[1] = loop % 10 + 48;
      Mmc_Fat_Write(file_contents, 42);  // write data to the assigned file
    }
  }
}//~

//M-M-M--------- Opens an existing file and rewrites it
void M_Open_File_Rewrite() {
  filename[7] = 'C';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Rewrite();
  for(loop = 1; loop <= 55; loop++) {
    file_contents[0] = loop / 10 + 64;
    file_contents[1] = loop % 10 + 64;
    Mmc_Fat_Write(file_contents, 42);    // write data to the assigned file
  }
}//~

//M-M-M--------- Opens an existing file and appends data to it
//               (and alters the date/time stamp)
void M_Open_File_Append() {
     filename[7] = 'B';
     Mmc_Fat_Assign(&filename, 0);
     Mmc_Fat_Set_File_Date(2005,6,21,10,35,0);
     Mmc_Fat_Append();                                    // Prepare file for append
     Mmc_Fat_Write(" for mikroElektronika 2005\n", 27);   // Write data to assigned file
}//~

//M-M-M--------- Opens an existing file, reads data from it and puts it to USART
void M_Open_File_Read() {
  filename[7] = 'B';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);                 // To read file, procedure returns size of file
  for (i = 1; i <= size; i++) {
    Mmc_Fat_Read(&caracter);
    Uart1_Write_Char(caracter);         // Write data to USART
  }
}//~

//M-M-M--------- Deletes a file. If file doesn't exist, it will first be created
//               and then deleted.
void M_Delete_File() {
  filename[7] = 'F';
  Mmc_Fat_Assign(filename, 0);
  Mmc_Fat_Delete();
}//~

//M-M-M--------- Tests whether file exists, and if so sends its creation date
//               and file size via USART
void M_Test_File_Exist(char fLetter) {
  unsigned long fsize;
  unsigned int year;
  unsigned short month, day, hour, minute;
  unsigned char outstr[12];

  filename[7] = fLetter;
  if (Mmc_Fat_Assign(filename, 0)) {
    //--- file has been found - get its date
    Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
    WordToStr(year, outstr);
    I_Write_Str(outstr);
    ByteToStr(month, outstr);
    I_Write_Str(outstr);
    WordToStr(day, outstr);
    I_Write_Str(outstr);
    WordToStr(hour, outstr);
    I_Write_Str(outstr);
    WordToStr(minute, outstr);
    I_Write_Str(outstr);
    //--- get file size
    fsize = Mmc_Fat_Get_File_Size();
    LongToStr((signed long)fsize, outstr);
    I_Write_Str(outstr);
  }
  else {
    //--- file was not found - signal it
    Uart1_Write_Char(0x55);
    Delay_ms(1000);
    Uart1_Write_Char(0x55);
  }
}//~

//-------------- Tries to create a swap file, whose size will be at least 100
//               sectors (see Help for details)
void M_Create_Swap_File() {
  unsigned int i;

  for(i=0; i<512; i++)
    Buffer[i] = i;

  size = Mmc_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20);   // see help on this function for details

  if (size) {
    LongToStr((signed long)size, fat_txt);
    I_Write_Str(fat_txt);

    for(i=0; i<5000; i++) {
      Mmc_Write_Sector(size++, Buffer);
      Uart1_Write_Char('.');
    }
  }
}//~

//-------------- Main. Uncomment the function(s) to test the desired operation(s)
void main() {
     //--- prepare PORTD for signalling
     PORTD = 0;
     TRISD = 0;
  AD1PCFGH = 0xFFFF;                //ADC off
AD1PCFGL = 0xFFFF ;
AD2PCFGL = 0xFFFF ;
CLKDIV=0b0111000000000000;   //set PLL so a 10MHz clock is 80MHz
PLLFBD=0b0000000000011110;
OSCCON=0b0011001110000000;
     //--- set up USART for the file read
     Spi2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_64,
                       _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);

     Uart_Init(19200);
     Uart_Write_Char('s');                      // wait for UART module to stabilize

     //--- init the FAT library
     if (!Mmc_Fat_Init(&PORTG,9)) {
         // reinitialize spi at higher speed
         Spi2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_4,
                       _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
         //--- Test start
         PORTD = 0x0005;
         
         //--- Test routines. Uncomment them one-by-one to test certain features
         M_Create_New_File();
         M_Create_Multiple_Files();

         M_Open_File_Rewrite();
         M_Open_File_Append();
         M_Delete_File();
         M_Create_Swap_File();

         M_Open_File_Read();
         M_Test_File_Exist('F');         // this file will not exist here
         M_Test_File_Exist('B');         // this file will exist here

    }
     else {
       I_Write_Str(fat_txt);
     }
     //--- Test termination
     PORTD = 0xFFFF;
}//~!


pefi1
Posts: 2
Joined: 08 Jun 2009 16:11

dsPIC33FJ128GP710 and Mmc_examples

#2 Post by pefi1 » 08 Jun 2009 16:21

Hello Justin!

I have the same trouble here now: I have got the LV24-33A board which comes along with the PIC24FJ96G010. I'm using die MikroC-Compiler.
Both MMC-Example runing with this CPU. When I put a dsPIC33FJ128GP710 on the board and change the ADxPCFGx register according to this CPU, I can compile the source and will also get a "s" on the terminal.

Allthough your posting is rather old I would like to ask if you have got a result on this topic found?

Until now I have no idea except that perhaps project's configuration register are wrong or the timing is wrong (clock input to SPI module).

Thanks for help in advance!
PeFi

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

#3 Post by Sobrietytest » 12 Jun 2009 04:34

Hi PeFi, it could be a speed issue; the MMC initialisation has to be done at slow speed (~400kHz) so maybe your first SPI_Init will need to be slowed down with the prescalers.

pefi1
Posts: 2
Joined: 08 Jun 2009 16:11

SD-card problems with dsPIC33FJ128GP710 on LV24-33A

#4 Post by pefi1 » 12 Jun 2009 05:54

Sobrietytest wrote:Hi PeFi, it could be a speed issue; the MMC initialisation has to be done at slow speed (~400kHz) so maybe your first SPI_Init will need to be slowed down with the prescalers.
Hello!
I have made the same initialisations like within my project than in the Mikroelektronikas original example project Mmc_Fat16_test. When using the scope I had seen that SD card's clock speed is about 770 kHz - and this speed didn't change even the Mmc_Fat_init() were successfully finished. So I added SPI2STAT = 0x0000 before the second SPI init. And then I measured a SD card clock frequency about 1.25 MHz. This was one part.

The other more important thing had been to switch off all RBx connections on DIP switch SW5 (SW5.2, SW5.4, SW5.6 and SW5.8 -> OFF)! Without this I have had no success at all with the dsPIC33.

Regards,
PeFi

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

#5 Post by slavisa.zlatanovic » 12 Jun 2009 11:15

Dear Pefi1,
So I added SPI2STAT = 0x0000 before the second SPI init
This is due to the fact that in our old SPI libraries we assumed that the SPI module is Off. Module settings can't be changed if it is running.

In our new mikroC PRO for dsPIC30/33 and PIC24 we'll fix this issue.

Best regards
Slavisa

saif
Posts: 1
Joined: 26 Jan 2011 14:31

Re: SD Cards Fat files

#6 Post by saif » 27 Jan 2011 15:51

I am trying to log data into a microsd card using the dspic33f128gp706A.I can successfully log data when i run the microcontroller at 37.5MHz using PLL and external crystal of 10MHz.When i try to log data beyond 37.5MHz,the Mmc_Fat_Assign function does not work.
I want to log 500 samples per second(ADC data) into the microsd card.I am currently logging 30 samples per second.
Please help... :?

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”