regarding interfacing sd card with pic 18f4520

Thread Starter

k.gopalakrishnan

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

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

Rich (BB code):
#if defined(__PCM__)
#if defined(__PCH__)
#endif
#endif

#include <stdio.h>
#include <string.h>
#include <mmcsd.c>
#include <input.c>
#fuses XT,NOWDT,NOPROTECT,NOLVP

#use delay(clock=4000000)
#use standard_io(B)
#use standard_io(C)
#use standard_io(D)


#define LCD_ENABLE_PIN PIN_B0
#define LCD_RS_PIN PIN_B2
#define LCD_RW_PIN PIN_B1
#define LCD_TYPE 2

#define SPI_SDI PIN_C4 //PIN 23
#define SPI_SDO PIN_C5 //PIN 24
#define SPI_SCK PIN_C3 //PIN 18
#define SPI_SS PIN_C2 //PIN 07

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)


void lcd_init();
void lcd_cmd(cmd);
void lcd_data(data);
void nibbel_split(unsigned long DataIn);

int mmc_init();
int mmc_response(unsigned char response);
int mmc_get_status();
int mmc_write_block(unsigned long block_number);
int mmc_read_block(unsigned long block_number);



unsigned char cmd,data;
int i,j=0;
int res,rcvdat;
char send_data[]={"TRANSINNOVA INSTRUMENTS PVT LTD"};
int read[32];
int DDLN,DDUN,LN,UN;
int errvalue;

void main()
{

set_tris_b(0x00);
set_tris_d(0x00);
set_tris_c(0x10);

lcd_init();
lcd_data((mmc_init()+0x30));
output_low(PIN_C2);
}

int mmc_init()
{

setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_16);

output_high(PIN_C2); // set high slave select signal before sending cmd0
output_high(PIN_C5);

for(i=0;i<10;i++) // initialise the MMC card into SPI mode by sending clks on
{
SPI_WRITE(0xFF);
}

output_low(PIN_C2);

spi_write(0x40);
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x95);

if( mmc_response(0x01)==1)
return 1 ;

i = 0;
while((i < 255) && (mmc_response(0x00)==1)) // if = 1 then there was a timeout waiting for 0x01 from the mmc

// must keep sending command if response
{
SPI_WRITE(0x41); // send sd command 41 to bring out of idle state
SPI_WRITE(0x00); // all the arguments are 0x00 for command one
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
i++;
}
if(i >= 254)
return 1; // if >= 254 then there was a timeout waiting for 0x00 from the mmc


OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)

SPI_WRITE(0xFF); // extra clocks to allow mmc to finish off what it is doing

OUTPUT_LOW(PIN_C2); // set SS = 0 (on)

SPI_WRITE(0X7A);
SPI_WRITE(0X00);
SPI_WRITE(0X00);
SPI_WRITE(0X00);
SPI_WRITE(0X00);
SPI_WRITE(0XFF);


OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
SPI_WRITE(0xFF); // extra clocks to allow mmc to finish off what it is doing
OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
if((mmc_response(0x00))==1)
return 1;
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)

return 0;
}
int mmc_response(unsigned char response)
{
unsigned long count = 0xFFFF;

while(SPI_READ(0xFF) != response && --count > 0);
if(count==0) return 1; // loop was exited due to timeout
else return 0; // loop was exited before timeout
}
void lcd_init()
{
cmd=0x38;
lcd_cmd(cmd);
delay_ms(250);
cmd=0x0E;
lcd_cmd(cmd);
delay_ms(250);
cmd=0x01;
lcd_cmd(cmd);
delay_ms(250);
cmd=0x06;
lcd_cmd(cmd);
delay_ms(250);
return;
}
void lcd_cmd(cmd)
{
output_d(cmd);
output_low(PIN_b2);//rs
output_low(PIN_b1);//read write
output_high(PIN_b0);
delay_ms(250);
output_low(PIN_b0);//enabel
return;
}
void lcd_data(data)
{
output_d(data);
output_high(PIN_b2);//rs
output_low(PIN_b1);//read write
output_high(PIN_b0);
delay_ms(250);
output_low(PIN_b0);//enabel
delay_ms(250);
return;

}
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:

tom66

Joined May 9, 2009
2,595
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.
 

davebee

Joined Oct 22, 2008
540
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.
 
Top