PIC18F4520 & MikroC & SD card

General discussion on mikroC.
Post Reply
Author
Message
linspire
Posts: 20
Joined: 03 Sep 2011 08:31

PIC18F4520 & MikroC & SD card

#1 Post by linspire » 18 Sep 2011 03:58

Hi,
I have done basic testing for UART and MMC fat16 separately which basically works.
When I combined both coding, the result doesn't show correct result such like invalid/strange characters.
Can you guys suggest opinion ?
Sorry about lengthy coding.

Code: Select all

// Define necessary definitions
char           filename[14] = "MIKRO00x.TXT";          // File names
unsigned char uart_rd;
unsigned char data;
unsigned char temp[10];
unsigned char number =0;

// MMC module connections
sbit Mmc_Chip_Select           at LATC2_bit;  // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISC2_bit;
// eof MMC module connections

// LCD 16x2 connections
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
// End of LCD 16x2 connections



// Creates new file and writes some data to it
/*void M_Create_New_File() {
  //filename[7] = 'A';
  Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
  Mmc_Fat_Assign(&filename, 0xA0);          // Find existing file or create a new one
  Mmc_Fat_Rewrite;
  Mmc_Fat_Write("Hello World",12);
} */


// Main. Uncomment the function(s) to test the desired operation(s)
void main() {

  ADCON1 |= 0x0F;                  // Configure AN pins as digital
  CMCON  |= 7; 
  TRISB = 0;                    // Turn off comparators
  PortB = 0xFF;

  UART1_Init(19200);
  Delay_ms(100);
  Lcd_Init();
  // Initialize SPI1 module
  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

  // use fat16 quick format instead of init routine if a formatting is needed
  if (Mmc_Fat_Init() == 0) {
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"MMC init OK");
    // reinitialize spi at higher speed
    SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
   do {
   if (UART1_Data_Ready()){              //<------ Check if a character has been received before reading
       uart_rd = UART1_Read();     // read the received data,
       UART1_Write(uart_rd);

       temp[number] = uart_rd;
       number = number +1;
       if (number == 9) // temp storing  string
       {
          Lcd_Cmd(_LCD_CLEAR);
          Lcd_cmd(_LCD_RETURN_HOME);
          Lcd_out(1,1,temp);
          Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
          Mmc_Fat_Assign(&filename, 0xA0);          // Find existing file or create a new one
          Mmc_Fat_Rewrite;
          Mmc_Fat_Write(temp,9);
          Lcd_Cmd(_LCD_CLEAR);
          Lcd_Out(1,1,"Test End");
          number =0;
        }

       }
    } while(1);



  }
  else {
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"MMC init FAILED"); // Note: Mmc_Fat_Init tries to initialize a card more than once.
                                   //       If card is not present, initialization may last longer (depending on clock speed)
  }

}

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

Re: PIC18F4520 & MikroC & SD card

#2 Post by linspire » 30 Sep 2011 16:36

Guys, I have done testing my code on simulation.
It's working properly, however my hardware keep displaying mmc init error which return value of -1.

I have use Windows Xp to format my SD card 2Gib & 1Gib (Kingston brand) with setting of FAT and allocation size default.
My hardware circuit follows the mikroC user manual MMC library wire example.

Any idea guys ?
I'm still suffering here.

Linspire

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

Re: PIC18F4520 & MikroC & SD card

#3 Post by linspire » 01 Oct 2011 13:35

Guys,
I have succesfully writing simple text file to my SD card already.
However, I wrote my code like this:

Code: Select all

  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
  while (Mmc_Init());
  Lcd_Out(1,1,"reintialized");
  Delay_ms(1000);
  Delay_ms(1000);
  // use fat16 quick format instead of init routine if a formatting is needed
  while(1) {

             if (!Mmc_Fat_Init()) {
             Delay_ms(1000);
             SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
             ...............
             ...............
              break;        // WHEN YOU CREATE FILE, GET OUT OF INFINITE WHILE LOOP
                               }
            }
I tried compile with MikroC user manual MMC fat 16 code, it doesn't work at all.
Anyone here can explain ?



Linspire

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: PIC18F4520 & MikroC & SD card

#4 Post by janko.kaljevic » 03 Oct 2011 09:30

Hello,

Please can you tell me which value MMC_FAT_Init returns?
If it is 1 it means that MMC Boot sector was not found and 255 (-1) that MMC SD card was not found.

Also which chip are you using?
Please explain how did you connect MMC Board to your chip.

Best regards.

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

Re: PIC18F4520 & MikroC & SD card

#5 Post by linspire » 03 Oct 2011 11:47

janko.kaljevic wrote:Hello,

Please can you tell me which value MMC_FAT_Init returns?
If it is 1 it means that MMC Boot sector was not found and 255 (-1) that MMC SD card was not found.

Also which chip are you using?
Please explain how did you connect MMC Board to your chip.

Best regards.
If I use example code to compile: it return value of -1.
I'm using PICKIT 2 to compile PIC18F4520 with external oscillator 20Mhz.

My SD card schematics diagram is shown as attached below:
Schematics Diagram.JPG
Schematics Diagram.JPG (74.27 KiB) Viewed 3732 times
Today I tried compile example code,it's still doesn't work at all.

Linspire

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: PIC18F4520 & MikroC & SD card

#6 Post by janko.kaljevic » 04 Oct 2011 08:56

Hello,

As I said return value of -1 means that SD card was not detected.
From the schematics that you have posted I see that you have not used voltage translators on data lines.
Ans since PIC18F4520 is 5V chip, you will have to do this.

Please check our MMC/SD Board which is designed for use on both 3.3V and 5V systems.
http://www.mikroe.com/eng/products/view ... -sd-board/

Best regards.

Post Reply

Return to “mikroC General”