PIC18F4520 + SD Card + MikroC

General discussion on mikroC.
Post Reply
Author
Message
Velimir
Posts: 15
Joined: 02 Jun 2010 07:24

PIC18F4520 + SD Card + MikroC

#1 Post by Velimir » 04 Jun 2010 14:43

Hello everyone.
I need help. I'm trying to communicate with the PIC18F4520 with SD card.
I use MC for PIC V.8. My problem is in their initial cards, functions Mmc_Init () and Mmc_Fat_Init () does not work.
I use the MMC / SD card of microelectronics.
I really do not know what is the problem , if anyone has any ideas I would be grateful.

Code: Select all

Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW,      LOW_2_HIGH);

     if (!Mmc_Fat_Init(&PORTC, 0)) {  

       Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);

Thank you in advance

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: PIC18F4520 + SD Card + MikroC

#2 Post by anikolic » 07 Jun 2010 11:24

Hello,

What you should try to do is to use simple MMC_Init() and see of it initializes the card. If this line of code returns the right result, then you might have and issue with the card format. You should make sure to format the card using FAT16 only. All necessary information on MMC_Init() is contained in the Help file and MMC Common example.

Otherwise, I would need more information than one you gave me. Primarily, I would need to know how your configuration bits are set and what oscillator are you using. I also wish to encourage you to switch over to mikroC PRO for PIC v3.80 compiler, because lots of issues are resolved and you can enjoy dozens of cumulative corrections and improvements with the new version. It is free of charge for already licensed owners of previous versions.

Best regards,
Aleksandar
Web Department Manager

Velimir
Posts: 15
Joined: 02 Jun 2010 07:24

Re: PIC18F4520 + SD Card + MikroC

#3 Post by Velimir » 07 Jun 2010 18:57

Hello Alexandar,
Thank you for your reply,
I tried to do with Mmc_Init () but all the same, no change.
I use XT oscillator 8MHz. I'll send you the entire code to watch. Yesterday I managed to initialize the card, and everything is working ok, but when I tried to re-initialize the card is not wanted, and still will not.

Code: Select all

#include <built_in.h>
  unsigned short mmc_error;
  unsigned short loop;
 char file_contents[8]="struja";
 char filename[14] = "STRUJAxTXT";

 void M_Create_New_File() {
  filename[6] = '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

    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, 42);   // write data to the assigned file
  }
}
void main(){
TRISA=0X00;
TRISB= 0X00;
TRISC=0X13;
PORTB=0XF0;

  ADCON1 |= 0x0F;                  // Configure AN pins as digital
  CMCON  |= 7;
Delay_100ms();

     Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_HIGH, LOW_2_HIGH);
     Delay_100ms();
     PORTB=0XA0;
      Delay_100ms();

      while(1) {

     if (!Mmc_Fat_Init(&PORTC,0)) {
       PORTB=0xF0;
      Delay_100ms();
       Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_HIGH, LOW_2_HIGH);

       PORTB=0x50;
      Delay_100ms();

       M_Create_New_File();
    }
  }
}
I need more memory.

Any advice we would welcome.
Thanks in advance!
Velimir

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: PIC18F4520 + SD Card + MikroC

#4 Post by anikolic » 09 Jun 2010 10:58

Hello,

You should initialize the card using MMC_INIT() at lower speed, and then increase the SPI speed and use MMC_Fat_Init() for initializing FAT. Also, when you finish writing to your file, you should get out of while loop. Here's the tested and working code, which is attached also as a complete project:

Code: Select all

#include <built_in.h>
#define _FILE_CONTENTS_SIZE 10
unsigned short mmc_error;
unsigned short loop;
char file_contents[_FILE_CONTENTS_SIZE+1]="strujaxx";
char filename[14] = "STRUJAx.TXT";

void M_Create_New_File() {
  filename[6] = '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

    file_contents[6] = loop / 10 + 48;
    file_contents[7] = loop % 10 + 48;
    file_contents[8] = 13; // CARRIAGE RETURN
    file_contents[9] = 10; // LINE FEED
    Mmc_Fat_Write(file_contents, _FILE_CONTENTS_SIZE);    // write data to the assigned file
  }
}
void main(){
  TRISA = 0x00;
  TRISB = 0x00;
  TRISC = 0x13;
  PORTB = 0xF0;

  ADCON1 |= 0x0F;                  // Configure AN pins as digital
  CMCON  |= 7;
  Delay_100ms();

  Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_HIGH, LOW_2_HIGH);
  // Loop until MMC is initialized
  while (Mmc_Init(&PORTC,0));
  
  Delay_100ms();
  PORTB = 0xA0;
  Delay_100ms();

  while(1) {

    if (!Mmc_Fat_Init(&PORTC,0)) {
      PORTB = 0xF0;
      Delay_100ms();
      Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_HIGH, LOW_2_HIGH);

      PORTB = 0x50;
      Delay_100ms();

      M_Create_New_File();
      PORTB = 0xFF; // WRITE IS OVER
      break;        // WHEN YOU CREATE FILE, GET OUT OF INFINITE WHILE LOOP
    }
  }
}
Best regards,
Aleksandar
Attachments
MMC.rar
(100.34 KiB) Downloaded 1125 times
Web Department Manager

Velimir
Posts: 15
Joined: 02 Jun 2010 07:24

Re: PIC18F4520 + SD Card + MikroC

#5 Post by Velimir » 10 Jun 2010 05:37

Hello, Aleksandar

I tried your program and it works great. Thanks to you I found where I was wrong. My mistake was that I included the XT oscillator (8MHz), I do not know why it is a mistake, but when I include XT, program can not initialize SD ...
I want to thank you, really helped me ...

Thank you very much .. :)

Velimir

linspire
Posts: 20
Joined: 03 Sep 2011 08:31

Re: PIC18F4520 + SD Card + MikroC

#6 Post by linspire » 28 Sep 2011 15:13

Velimir wrote:Hello, Aleksandar

I tried your program and it works great. Thanks to you I found where I was wrong. My mistake was that I included the XT oscillator (8MHz), I do not know why it is a mistake, but when I include XT, program can not initialize SD ...
I want to thank you, really helped me ...

Thank you very much .. :)

Velimir
Hi,
I know this is old thread, but I'm doing SD card so I have some questions.
How'd you make your schematic for your SD card + sd card connector ?
And second question, what'd you mean included XT oscillator ?
Did you mention if you exclude XT oscillator mode, you can init the SD card successfully ?

Regards
Linspire

tariq7868
Posts: 5
Joined: 26 Sep 2011 21:05

Re: PIC18F4520 + SD Card + MikroC

#7 Post by tariq7868 » 04 Oct 2011 21:40

Please, could you explain, why is it necessary to select HS oscillator in configuration bits instead of XT oscillator?
Thanks

Post Reply

Return to “mikroC General”