qwertyuiop415263
Full Member level 4
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
**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: