Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MikroC & SD CARD & PIC18F4520

Status
Not open for further replies.

Linspire

Full Member level 5
Joined
Sep 1, 2011
Messages
303
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
M'sia
Activity points
3,279
Hi guys,
Any good steps for learning SD card interfacing programming ?
I'm afraid I got error compiling when I copy exactly code from MikroC library example : MMC.

Purpose for this design : Want collect data from serial RS232 then stored as text file then read it by sd card reader (PC).

So initially I want learn how to learn read and write a file to SD card.

Any suggestions guy ?



Regards,
Linspire
 

Can you post your code, so that one of us may look at it? How about a schematic of your design?

How are you interfacing the SD interface with the rest of the circuit? Both SD and MMC are 3.3V devices and therefore need a 3.3V supply and 3.3V I/O either by a level translator or resistor divider.

The following book has several examples of SD interfaces using MikroC:

Advanced PIC Microcontroller Projects in C: From USB to RTOS with the PIC 18F Series

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Can you post your code, so that one of us may look at it? How about a schematic of your design?

How are you interfacing the SD interface with the rest of the circuit? Both SD and MMC are 3.3V devices and therefore need a 3.3V supply and 3.3V I/O either by a level translator or resistor divider.

The following book has several examples of SD interfaces using MikroC:

Advanced PIC Microcontroller Projects in C: From USB to RTOS with the PIC 18F Series

BigDog

I will attach my schematics diagram after I try do programming.
I want ask question:

I realize some coding library are not same defined as most of programming example shown.
Such as Mmc_Fat_Init(&PORTC,0) ; which I saw most of these kind of declaration from example codes
whereby my mikroC help files shows

// MMC module connections
sfr sbit Mmc_Chip_Select at RC2_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISC2_bit;
// MMC module connections
...
SPI1_Init();

error = Mmc_Init(); // Init with CS line at RC2_bit


Do I use wrong version of mikroC ?
I want ask this to ease my understanding writing coding not just confusing with declaration stuff.
 

// MMC module connections
sfr sbit Mmc_Chip_Select at RC2_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISC2_bit;
// MMC module connections
...
SPI1_Init();

error = Mmc_Init(); // Init with CS line at RC2_bit


Do I use wrong version of mikroC ?
I want ask this to ease my understanding writing coding not just confusing with declaration stuff.

No, it looks like you are using the current version of MikroC's Libraries.

The MMC library uses the SPI module in the PIC18F, so you will need to make sure the correct pin is defined in the following declarations:

// MMC module connections
sfr sbit Mmc_Chip_Select at RC2_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISC2_bit;
// MMC module connections

I would setup my circuit as closely as possible to the MikroC example and test the functionality by running the MMC library example at the end of the MMC section in the user manual. Once you know the hardware functions correctly you can add to and modify the design to fit your needs.

Did this answer your question? Or did I not understand it correctly?

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
root@Devkit8000:# cd /mdia/
root@Devkit8000:/media# ls

card hdd mmcblkop1 ram union

cf mmc1 net realroot
root@Devkit8000:/media# ls mmcblk0p1/

flash-uboot.bin u-boot.bin x-load.bin.ift_for NAND

mlo uImage

ramdisk.gz uBI.img


this is my test on MMC/SD card on the Devkit8000 board. The OS is Linux, it is easy to read and write files to SD card.
 

this is my test on MMC/SD card on the Devkit8000 board. The OS is Linux, it is easy to read and write files to SD card.

That's nice.

However Brittany Snow, Linspire is implementing his design with a PIC18F4520 which does not support Linux.

BigDog
 
Well, my hardware circuit will be same as user manual.
I read the code and write & built got some errors:

Simple errors:
Correct one should be UART1_Init (my mikroC library defined) not USART_Init (from user manual example code).


Or another example:
Spi_Init_Advanced(.....) , From user manual 's MMC example

Correct one should be SPI1_Init_Advanced(....)


These what I mentioned earlier, it seem the library declaration abit differrent way.
 

What version of the MikroC compiler are you using?

The latest user manual MMC library example has all the correct function calls that you point out in the above posting.

You may want to grab the latest MikroC User Manual from MikroE's website.

MikroC Pro User Manual

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
What version of the MikroC compiler are you using?

The latest user manual MMC library example has all the correct function calls that you point out in the above posting.

You may want to grab the latest MikroC User Manual from MikroE's website.

MikroC Pro User Manual

BigDog

I'm using MikroC 2009, that software library should be sufficient for me right ?
Or you got better recommendations ?
I will post further question after I sort out.


root@Devkit8000:# cd /mdia/
root@Devkit8000:/media# ls

card hdd mmcblkop1 ram union

cf mmc1 net realroot
root@Devkit8000:/media# ls mmcblk0p1/

flash-uboot.bin u-boot.bin x-load.bin.ift_for NAND

mlo uImage

ramdisk.gz uBI.img


this is my test on MMC/SD card on the Devkit8000 board. The OS is Linux, it is easy to read and write files to SD card.

Thank you for your simple testing code. However, I'm not using Linux and any development board; I built those circuit by referring schematics diagram available from book & internet sources.
 

I'm using MikroC 2009, that software library should be sufficient for me right ?
Or you got better recommendations ?
I will post further question after I sort out.

Yes, the version you have is probably a rev or two back, but it should serve the purpose. Any other alternative would require more coding on your part with a different compiler, there is no purpose in mixing things up at this point.

I would download the more recent version of the MikroC User Manual.

Let me know how things work out.

BigDog

---------- Post added at 05:46 ---------- Previous post was at 05:43 ----------

I have MikroC Pro v4.60 2011.
 

Bad news, my desktop doesn't has UART connection. The example given in user manual requires HyperTerminal to shows the results.
I don't have any serial port and hardware to implement it to make communication.
Any suggestion ?

Regards,
Linspire
 

Bad news, my desktop doesn't has UART connection. The example given in user manual requires HyperTerminal to shows the results.
I don't have any serial port and hardware to implement it to make communication.
Any suggestion ?

Regards,
Linspire

Use USB to Serial converter..
 

Use USB to Serial converter..

You asking me to buy a cable which can convert it, but I need to strip off Serial plug to able to communicate with my PIC18f4520.
Is that what are you trying to say ?
Can provide alternatives ways for SD card testing instead use this mikro user manual code example ?
 

Can any one help me out by providing me a sample code?
I am unable to build one using mikro c library.
I do have a few sample codes of other compilers. I need one implemented on mikro c.
 

Bad news, my desktop doesn't has UART connection. The example given in user manual requires HyperTerminal to shows the results.
I don't have any serial port and hardware to implement it to make communication.
Any suggestion ?

Yes, pickup a USB to UART bridge chip on a breakout board. I keep several of these on my bench at all times.

The example below uses a FTDI FT232R device and can directly connect to your PICs UART without a RS-232 transceiver chip like the MAX232.

FT232RL USB to UART Breakout Board

The FT232R has quite a few uses and should come in handy in future projects.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Yes, pickup a USB to UART bridge chip on a breakout board. I keep several of these on my bench at all times.

The example below uses a FTDI FT232R device and can directly connect to your PICs UART without a RS-232 transceiver chip like the MAX232.

FT232RL USB to UART Breakout Board

The FT232R has quite a few uses and should come in handy in future projects.

BigDog


Can you provide more devices and "cheap" ? Because I need to consider the cost to be budget and availability stock in my country store area such as "Cytron Malaysia" and "RS Malaysia".
 

Hi guys,
I have remodified the code from user manual MMC's example by interfacing to my 16x2 LCD instead UART.

Code:
sbit Mmc_Chip_Select           at LATC0_bit;  // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISC0_bit;
// eof MMC module connections

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;


// Variables for MMC routines
unsigned char SectorData[512];        // Buffer for MMC sector reading/writing
unsigned char data_for_registers[16]; // buffer for CID and CSD registers

// UART1 write text and new line (carriage return + line feed)
/*
void UART1_Write_Line(char *uart_text) {
  UART1_Write_Text(uart_text);
  UART1_Write(13);
  UART1_Write(10);
}
  */
// Display byte in hex
void PrintHex(unsigned char i) {
  unsigned char hi,lo;

  hi = i & 0xF0;               // High nibble
  hi = hi >> 4;
  hi = hi + '0';
  if (hi>'9') hi=hi+7;
  lo = (i & 0x0F) + '0';       // Low nibble
  if (lo>'9') lo=lo+7;

// UART1_Write(hi);
// UART1_Write(lo);
}

void main() {

  const char   FILL_CHAR = 'm';
  unsigned int i, SectorNo;
  char         mmc_error;
  bit          data_ok;

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

  Delay_ms(2500);

// UART1_Write_Line("PIC-Started"); // PIC present report


  // Initialize SPI1 module
  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
  Lcd_Init();                                   // Initialize GLCD

  Lcd_Out(1,1,"PIC-STARTED");
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);
  
  // initialise a MMC card
  mmc_error = Mmc_Init();
  if(mmc_error == 0)
     Lcd_Out(2,1,"MMC INIT OK");
  else
     Lcd_Out(2,1,"MMC INIT ERROR");


  // Fill MMC buffer with same characters
  for(i=0; i<=511; i++)
    SectorData[i] = FILL_CHAR;

  // Choose sector
  SectorNo = 590;

  // Write sector
  mmc_error = Mmc_Write_Sector(SectorNo, SectorData);
  if(mmc_error == 0)
//    UART1_Write_Line("Write-OK");
     Lcd_Out(1,1,"WRITE OK");

  else  // if there are errors.....
    //UART1_Write_Line("Write-Error");
     Lcd_Out(1,1,"Write ERROR");
     Delay_ms(5000);

  // Reading of CID register
  mmc_error = Mmc_Read_Cid(data_for_registers);
  if(mmc_error == 0) {
   // UART1_Write_Text("CID : ");
    Lcd_Out(2,1,"CID");

    for(i=0; i<=15; i++)
      PrintHex(data_for_registers[i]);
   // UART1_Write_Line("");
  }
  else
  //  UART1_Write_Line("CID-error");
      Lcd_Out(2,1,"CID-ERROR");

  // Reading of CSD register
  mmc_error = Mmc_Read_Csd(data_for_registers);
  if(mmc_error == 0) {
  //  UART1_Write_Text("CSD : ");
      Lcd_Out(1,1,"CSD: ");
    for(i=0; i<=15; i++)
      PrintHex(data_for_registers[i]);

//   UART1_Write_Line("");
  }
  else
//   UART1_Write_Line("CSD-error");
     Lcd_Out(1,1,"CSD-error");

  // Read sector
  mmc_error = Mmc_Read_Sector(SectorNo, SectorData);
  if(mmc_error == 0) {
    //UART1_Write_Line("Read-OK");
     Lcd_Out(1,1,"Read-ok");
    // Chech data match
    data_ok = 1;
    for(i=0; i<=511; i++) {
      //UART1_Write(SectorData[i]);
      if (SectorData[i] != FILL_CHAR) {
        data_ok = 0;
        break;
      }
    }

    //UART1_Write_Line("");
    if (data_ok)
//      UART1_Write_Line("Content-OK");
       Lcd_Out(1,1,"Content Ok");

    else
      //UART1_Write_Line("Content-Error");
        Lcd_Out(1,1," Content ERROR");
  }
  else  // if there are errors.....
    //UART1_Write_Line("Read-Error");
      Lcd_Out(1,1,"Read Error");
 //   Lcd_Cmd(_LCD_CLEAR);
  // Signal test end
  //UART1_Write_Line("Test End.");
      Delay_ms(5000);
    Lcd_Out(1,1,"Test End");
}

Schematics Diagram.JPG

These'r my brief schematics diagram.
I have use multi-meter to measure the input of my SD card circuit which around 3.5-3.6V.
I have compiled & tested . The LCD just display MMC init Error & Write Error and didnt show until "Test End".

I have formatted my SD card 1GB with my windows 7(ulti) 64bits with following settings:
Fat (default), Allocation Size : Default

My mikroC PIC18f4520 oscillator value have tried 8 & 10 Mhz.

Any idea how to fix this ?
Sorry for lengthy above description.
 

Hi guys,
I try another simple code initialization
Code:
// MMC module connections
sfr sbit Mmc_Chip_Select at RC2_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISC2_bit;
// MMC module connections




char           filename[] = "TempLogF.TXT";



void main() {


  // Initialize SPI1 module and set pointer(s) to SPI1 functions
  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
  Mmc_Fat_Assign(&filename,0xA0);
  Mmc_Fat_Write("Hello world",2);


}

But I got following error compilation which I cant program into PIC :

0 1 mikroCPIC1618.exe -MSF -DBG -pP18F4520 -DL -O11111114 -fo10 -N"C:\Users\User\Desktop\MikroC testing\MMC.mcppi" -SP"C:\Program Files (x86)\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Program Files (x86)\Mikroelektronika\mikroC PRO for PIC\uses\P18\" -SP"C:\Users\User\Desktop\MikroC testing\" "MMC.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_Button.mcl" "__Lib_SPI_c345.mcl" "__Lib_UART_c67.mcl" "__Lib_Mmc.mcl" "__Lib_MmcFat16.mcl"
0 125 All files Preprocessed in 32 ms
0 121 Compilation Started MMC.c
22 122 Compiled Successfully MMC.c
0 126 All files Compiled in 78 ms
0 359 Unresolved extern 'islower' __Lib_MmcFat16.c
0 359 Unresolved extern 'islower' __Lib_MmcFat16.c
0 359 Unresolved extern 'toupper' __Lib_MmcFat16.c
0 359 Unresolved extern 'toupper' __Lib_MmcFat16.c
0 359 Unresolved extern 'islower' __Lib_MmcFat16.c

0 0
0 102 Finished (with errors): 03 Sep 2011, 15:46:44 MMC.mcppi

Any one of you face this problem before ?
 

Try this, it may help

extern sfr sbit Mmc_Chip_Select at RC0_bit;
extern sfr sbit Mmc_Chip_Select_Direction at TRISC0_bit;
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top