regarding interfacing sd card with pic 18f4520

Discussion in 'Embedded Systems and Microcontrollers' started by k.gopalakrishnan, Nov 8, 2010.

  1. k.gopalakrishnan

    Thread Starter New Member

    Jun 18, 2010
    4
    0
    http://img442.imageshack.us/img442/8744/mmcconnection.jpg

    this is the circuit diagram i followed, and my code is

    Code ( (Unknown Language)):
    1.  
    2. #if defined(__PCM__)
    3. #if defined(__PCH__)
    4. #endif
    5. #endif
    6.  
    7. #include <stdio.h>
    8. #include <string.h>
    9. #include <mmcsd.c>
    10. #include <input.c>
    11. #fuses XT,NOWDT,NOPROTECT,NOLVP
    12.  
    13. #use delay(clock=4000000)
    14. #use standard_io(B)
    15. #use standard_io(C)
    16. #use standard_io(D)
    17.  
    18.  
    19. #define LCD_ENABLE_PIN PIN_B0
    20. #define LCD_RS_PIN PIN_B2
    21. #define LCD_RW_PIN PIN_B1
    22. #define LCD_TYPE 2
    23.  
    24. #define SPI_SDI PIN_C4 //PIN 23
    25. #define SPI_SDO PIN_C5 //PIN 24
    26. #define SPI_SCK PIN_C3 //PIN 18
    27. #define SPI_SS PIN_C2 //PIN 07
    28.  
    29. #define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
    30. #define SPI_MODE_1 (SPI_L_TO_H)
    31. #define SPI_MODE_2 (SPI_H_TO_L)
    32. #define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
    33.  
    34.  
    35. void lcd_init();
    36. void lcd_cmd(cmd);
    37. void lcd_data(data);
    38. void nibbel_split(unsigned long DataIn);
    39.  
    40. int mmc_init();
    41. int mmc_response(unsigned char response);
    42. int mmc_get_status();
    43. int mmc_write_block(unsigned long block_number);
    44. int mmc_read_block(unsigned long block_number);
    45.  
    46.  
    47.  
    48. unsigned char cmd,data;
    49. int i,j=0;
    50. int res,rcvdat;
    51. char send_data[]={"TRANSINNOVA INSTRUMENTS PVT LTD"};
    52. int read[32];
    53. int DDLN,DDUN,LN,UN;
    54. int errvalue;
    55.  
    56. void main()
    57. {
    58.  
    59. set_tris_b(0x00);
    60. set_tris_d(0x00);
    61. set_tris_c(0x10);
    62.  
    63. lcd_init();
    64. lcd_data((mmc_init()+0x30));
    65. output_low(PIN_C2);
    66. }
    67.  
    68. int mmc_init()
    69. {
    70.  
    71. setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_16);
    72.  
    73. output_high(PIN_C2); // set high slave select signal before sending cmd0
    74. output_high(PIN_C5);
    75.  
    76. for(i=0;i<10;i++) // initialise the MMC card into SPI mode by sending clks on
    77. {
    78. SPI_WRITE(0xFF);
    79. }
    80.  
    81. output_low(PIN_C2);
    82.  
    83. spi_write(0x40);
    84. spi_write(0x00);
    85. spi_write(0x00);
    86. spi_write(0x00);
    87. spi_write(0x00);
    88. spi_write(0x95);
    89.  
    90. if( mmc_response(0x01)==1)
    91. return 1 ;
    92.  
    93. i = 0;
    94. while((i < 255) && (mmc_response(0x00)==1)) // if = 1 then there was a timeout waiting for 0x01 from the mmc
    95.  
    96. // must keep sending command if response
    97. {
    98. SPI_WRITE(0x41); // send sd command 41 to bring out of idle state
    99. SPI_WRITE(0x00); // all the arguments are 0x00 for command one
    100. SPI_WRITE(0x00);
    101. SPI_WRITE(0x00);
    102. SPI_WRITE(0x00);
    103. SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
    104. i++;
    105. }
    106. if(i >= 254)
    107. return 1; // if >= 254 then there was a timeout waiting for 0x00 from the mmc
    108.  
    109.  
    110. OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
    111.  
    112. SPI_WRITE(0xFF); // extra clocks to allow mmc to finish off what it is doing
    113.  
    114. OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
    115.  
    116. SPI_WRITE(0X7A);
    117. SPI_WRITE(0X00);
    118. SPI_WRITE(0X00);
    119. SPI_WRITE(0X00);
    120. SPI_WRITE(0X00);
    121. SPI_WRITE(0XFF);
    122.  
    123.  
    124. OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
    125. SPI_WRITE(0xFF); // extra clocks to allow mmc to finish off what it is doing
    126. OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
    127. if((mmc_response(0x00))==1)
    128. return 1;
    129. OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
    130.  
    131. return 0;
    132. }
    133. int mmc_response(unsigned char response)
    134. {
    135. unsigned long count = 0xFFFF;
    136.  
    137. while(SPI_READ(0xFF) != response && --count > 0);
    138. if(count==0) return 1; // loop was exited due to timeout
    139. else return 0; // loop was exited before timeout
    140. }
    141. void lcd_init()
    142. {
    143. cmd=0x38;
    144. lcd_cmd(cmd);
    145. delay_ms(250);
    146. cmd=0x0E;
    147. lcd_cmd(cmd);
    148. delay_ms(250);
    149. cmd=0x01;
    150. lcd_cmd(cmd);
    151. delay_ms(250);
    152. cmd=0x06;
    153. lcd_cmd(cmd);
    154. delay_ms(250);
    155. return;
    156. }
    157. void lcd_cmd(cmd)
    158. {
    159. output_d(cmd);
    160. output_low(PIN_b2);//rs
    161. output_low(PIN_b1);//read write
    162. output_high(PIN_b0);
    163. delay_ms(250);
    164. output_low(PIN_b0);//enabel
    165. return;
    166. }
    167. void lcd_data(data)
    168. {
    169. output_d(data);
    170. output_high(PIN_b2);//rs
    171. output_low(PIN_b1);//read write
    172. output_high(PIN_b0);
    173. delay_ms(250);
    174. output_low(PIN_b0);//enabel
    175. delay_ms(250);
    176. return;
    177.  
    178. }
    179.  
    180.  
    i am expecinh 0x00 in my lcd display it shows blank or i am suspecting that my sd card is working or not. if any one having the test program kindly send to me.kindly help me out of this problem.
     
    Last edited by a moderator: Nov 10, 2010
  2. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    Microchip have a Memory Disk Drive library which implements support for SD cards, including FAT32, over SPI, for PIC18F/24F and dsPIC30F/33F. Try that before writing your own library.
     
  3. k.gopalakrishnan

    Thread Starter New Member

    Jun 18, 2010
    4
    0
    where i can find memory disk library for sd card provided by microchip
     
  4. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
  5. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    SD and MMC cards work about the same after initialization, but they have different initialization sequences. The SD initialization is something like sending a series of 0x41/0x55 characters until the card responds with a ready signal. The code you have is probably almost ready to work, so if you modify it with the correct SD initialization sequence it would probably work.
     
Loading...