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.

MicroSD Card Interfacing with PIC18LF8720

Status
Not open for further replies.

qwertyuiop415263

Full Member level 4
Joined
Mar 26, 2010
Messages
232
Helped
50
Reputation
102
Reaction score
39
Trophy points
1,308
Location
Pakistan
Activity points
2,414
I am trying to interface MicroSd Card with PIC18LF8720 and using this SD card Breakout Board

**broken link removed**

I am just trying to send CMD0 command and check the response from MicroSd which is always 0xFF instead of 0x01.

Thankx in advance

Here is my firmware

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<p18f8720.h>
#include<string.h>
#pragma config OSC=HS, OSCS=OFF
#pragma config PWRT=OFF, BOR=ON, BORV=27
#pragma config WDT =OFF
#pragma config DEBUG=OFF,LVP=OFF,STVR=OFF,MODE=MC
#define ReadSPI()   WriteSPI(0xFF)
#define ClockSPI()  WriteSPI(0xFF)
#define SDCS          PORTGbits.RG4
void InitializeSD(void);
unsigned char WriteSPI(unsigned char);
int SendSDCommand(unsigned char , unsigned long int);
int InitializeMedia(void);
int SDCardDetect(void);
void SetTx1(unsigned char );
void delay (int);
 
void main (void)
{
    unsigned long int addr;
    int i,r,u;
    TRISCbits.TRISC6=0;
    TRISCbits.TRISC7=1;
    TXSTA1=0x20; // serial 1
    SPBRG1=3;
    RCSTA1bits.SPEN=1;
    SetTx1('T');
    delay(1);
    PORTE=0x00;
    TRISE=0x00;
    InitializeSD();
    SDCS = 1;
    for (u = 0; u < 1220; u++) 
        {
            WriteSPI(0xFF);
        }
    PORTE=SendSDCommand(0,0);
    SDCS=1;
    while(1);
}
void InitializeSD(void)
{
    SDCS=1;
    TRISGbits.TRISG4=0;
    TRISCbits.TRISC4 = 1;
    PORTCbits.RC3 = 0;
    PORTCbits.RC4 = 0;
    PORTCbits.RC5 = 0;
    TRISCbits.TRISC3 =0;
    TRISCbits.TRISC5 =0;
    SSPCON1 = 0x22;
    SSPCON1bits.SSPEN = 1;
    SSPSTATbits.SMP = 1;
    SSPSTATbits.CKE = 1;
    SSPCON1bits.CKP = 0;
}
unsigned char WriteSPI(unsigned char myData)
{
SSPBUF = myData;                    // put the data in the SSPBUF register which going to be send
while(SSPSTATbits.BF==0);               // wait until the all bits sended
return SSPBUF;
}
 
int SendSDCommand(unsigned char c , unsigned long int a)
{
int i;
unsigned char r;
SDCS=0;
delay(5);
WriteSPI(0xFF);
WriteSPI(0x40);
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0x00);
WriteSPI(0x95);
i=1000;
do
    {
        r= WriteSPI(0xff);
        if (r!=0xFF) break;
    }    
while(--i > 0);
if (r==0x01){SetTx1('Y');}
else {SetTx1('N');}
return (r);
}
 
 
void delay (int a)
{
int x=0,y=0;
for (x=0;x<a;x++)
for (y=0;y<200;y++);
}
 
void SetTx1(unsigned char c)            //====================
    {
        TXREG1=c;
        while(PIR1bits.TX1IF==0);
    }

 
Last edited by a moderator:

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
396
Reputation
786
Reaction score
380
Trophy points
1,363
Location
Pakistan
Activity points
7,263
Did you tested your hardware with the demo code present there?
 

qwertyuiop415263

Full Member level 4
Joined
Mar 26, 2010
Messages
232
Helped
50
Reputation
102
Reaction score
39
Trophy points
1,308
Location
Pakistan
Activity points
2,414
I am getting 0x00 instead of 0x01 in response of CMD0. It means that MicroSD has accept the command but didn't go into idle state.

Anyone will help me in that
 

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
396
Reputation
786
Reaction score
380
Trophy points
1,363
Location
Pakistan
Activity points
7,263
I would think power supply issues like ripples, noises or oscillations are the first thing one would check if the circuit is behaving intermittently.
When you send CMD0 command to place the SD card into SPI mode, you have to make sure it is placed in that mode by checking for the 0x01 response.
At end of each command, dis-select SD Card and send one more 0xFF (sends 8 more clock pulses). These can actually be done in either order.
Then you check for completion of initialization of SD card using the CMD1 command, for a 0x01(still busy) response or a 0x00(done) response. Wait until you get a 0x00 response. This might take up to a second for very large size SD card.
Now send other commands.
 

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
396
Reputation
786
Reaction score
380
Trophy points
1,363
Location
Pakistan
Activity points
7,263
Yes if you did'nt enabeled internal, i guess. It does not harm to check if it works.
A reference design.
https://www.brushelectronics.com/index.php?page=projects
After supply voltage reached 2.2 volts, wait for a millisecond at least. Set SPI clock rate between 100kHz and 400kHz. Set DI and CS high and apply 74 or more clock pulses to SCLK. The card will enter its native operating mode and go ready to accept native commands.
Send a CMD0 with CS low to reset the card. The card samples CS signal on a CMD0 is received successfully. If the CS signal is low, the card enters SPI mode and responds R1 with In Idle State bit (0x01). Since the CMD0 must be sent as a native command, the CRC field must have a valid value. When once the card enters SPI mode, the CRC feature is disabled and the CRC is not checked by the card so that command transmission routine can be written with the hardcorded CRC value that valid for only CMD0 and CMD8 with the argument of zero. The CRC feature can also be switched with CMD59.

https://ww1.microchip.com/downloads/en/AppNotes/01045b.pdf
https://www.microchip.com/forums/tm.aspx?m=623061&high=spi+pullup+resistor

If you bought an SD card, chances are its already pre-formatted with a FAT filesystem. However you may have problems with how the factory formats the card, or if its an old card it needs to be reformatted.
The memory cards are initially patitioned and formatted to align the allocation unit to the erase block. When re-patition or re-format the memory card with a device that not compliant to MMC/SDC (this is just a PC) with no care, the optimization will be broken and the write performance might be lost. I tried to re-format 512MB SDC in FAT32 with a PC, the write performance measured in file copy was lowerd to one several. Therefore the re-formatting the card should be done with MMC/SDC compliant equipments rather than PC.
use the official SD card formatter utility - written by the SD association it solves many problems that come with bad formatting!
**broken link removed**
Even though you can could use your SD card 'raw' - its most convenient to format the card to a filesystem.
the card must be formatted FAT16 or FAT32. Some only allow one or the other. It depends on your compiler and libraries available. FAT or FAT16 is popular.

[
 
Last edited:

qwertyuiop415263

Full Member level 4
Joined
Mar 26, 2010
Messages
232
Helped
50
Reputation
102
Reaction score
39
Trophy points
1,308
Location
Pakistan
Activity points
2,414
Can you kindly show me the SPI Pin configuration and also SSPCON and SSPSTAT registers?
 

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
396
Reputation
786
Reaction score
380
Trophy points
1,363
Location
Pakistan
Activity points
7,263
Code:
; SPI Master_Init_1 ; function begin
      	BCF trisc,5
     	BSF portc,5
      	BSF trisc,4
      	BCF trisc,3
     	BSF portc,3
      	MOVLW 0x03
     	ANDWF prescale_1, W
      	MOVWF sspcon1
      	BSF sspcon1,4
      	BCF sspstat,6
      	BCF sspstat,7
      	BSF sspcon1,5
     	RETURN
; } SPI_Master_Init_1 function end
 

qwertyuiop415263

Full Member level 4
Joined
Mar 26, 2010
Messages
232
Helped
50
Reputation
102
Reaction score
39
Trophy points
1,308
Location
Pakistan
Activity points
2,414
I am using MicroSD card 2GB which comes with nokia mobile and i formatted that card using default FAT file system. I have looked on internet for Specification Sheet of microsd card but could find one. I want to know the timing diagram of SCK, MISO and MOSI signals and also whether microsd respond during each bit transfer i.e whether i have to make SMP bit 1 or 0.
 

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
396
Reputation
786
Reaction score
380
Trophy points
1,363
Location
Pakistan
Activity points
7,263
https://www.adafruit.com/datasheets/TS16GUSDHC6.pdf


4525903700_1351767262.png


Tutorial.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top