interfacing sd card with pic

Status
Not open for further replies.

gopalakrishnan

New Member
**broken link removed**

Please include images in you posts, moderator

this is the circuit diagram i followed, and my code is
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;

}
Code tags added by moderator

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.**broken link removed**
 
Last edited by a moderator:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…