Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

SD to PIC18f HELP please.

Status
Not open for further replies.

kristt2001

New Member
hi all, i am stuck with interfacing the pic18f with a microSD card. all i want to do is read and write to a text file.
i am using SPI mode, with

hardware connections:
DI -> RC5
DO -> RC4
SCK -> RC3


code so far:

void MMC (void){
unsigned char SPI_data;
unsigned char SPI_Read[10];
unsigned char SPI_Write[10] = "MICROCHIP";



TRISCbits.TRISC3 = 1;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC5 = 1;

PORTCbits.RC3 = 1;
PORTCbits.RC4 = 1;
PORTCbits.RC5 = 1;

Nop();

OpenSPI(SPI_FOSC_4,MODE_01,SMPMID);
SSPCON1 |= SPI_FOSC_16;
SSPBUF = 0x00;
while(1)
{// SPI_data = ReadSPI();
putsSPI(SPI_Write);
if(DataRdySPI())
{
getsSPI(SPI_Read,10);
}

}
CloseSPI();
}


however, it isnt writing to the SD.
can anyone help.
 
First, you've got no connection for the CS line? You need to take it high, send out 0xFF ten times, then take it low, and then send the init command. It's also manipulated for all the other read/write commands.

Secondly, you want to read/write a file on the card that you can then use with your PC? You can read/write sectors on the card, but there's a file structure (FAT) that you need to follow to treat it like a regular PC file. There's a directory entry and there are sector maps for describing what sectors your file uses.
 
do i need a Chip Select line if i am only using one slave device?

i need the text file to be read by a computer, not by the pic. i just need the pic to be able to write to it. i am not too sure about how i am to implement the file structures.
can you please inform?
 
yeah i did. i put the CS to RA5.

#include "p18f452.h"
#include <spi.h>

void MMC (void){
unsigned char SPI_data;
unsigned char SPI_Read[10];
unsigned char SPI_Write[10] = "MICROCHIP";
unsigned char filename[14] = "FILETEST.TXT";
unsigned int i=0;

SSPCON1bits.SSPEN = 1; //configures the SDI, SDO, SCK, and SS pins as serial port pins

TRISAbits.TRISA5 = 1; //CS
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC5 = 0;

PORTAbits.RA5 = 1; //sent high
PORTCbits.RC3 = 1;
PORTCbits.RC4 = 1;
PORTCbits.RC5 = 1;

Nop();

for(i=0; i<11; i++){ // 0xFF for 10 times then carried low
PORTA=0xFF;
}
PORTAbits.RA5 = 0;


OpenSPI(SPI_FOSC_4,MODE_01,SMPMID);
SSPCON1 |= SPI_FOSC_16;
SSPBUF = 0x00;
while(1)
putsSPI(SPI_Write);
if(DataRdySPI())
{
getsSPI(SPI_Read,10);
// CloseSPI();
}
}
CloseSPI();
}


sorry i dunno what i am doing here.
 
I haven't used the 18f452, but port A is analog by default on most PICs so you may need to disable analog functions. Most compilers have a command for it.

Also, you don't have your pins configured properly. CS, CLK, and DO are controlled by the PIC, because they're sending data to the SD card. So they should be....input or output?

DI is controlled by the SD card and read into the PIC, so it should be set to...input or output?

Remember, for the TRIS registers, "1" means input.

Then, for the init sequence, this:
Code:
for(i=0; i<11; i++){ // 0xFF for 10 times then carried low
PORTA=0xFF;
}

Doesn't do anything. Or rather, it doesn't do anything productive. It sets all of the PORTA pins to high. The 0xFF x 10 has to be shifted out via CLK and DO, which is what the puts_SPI command does.

Then, you're going to need to read up on the OpenSPI() command, I don't use C for PICs and don't know about the SPI library so I can't say what that function does. But after the 0xFF x 10 there needs to be a 6-byte sequence sent (0x40, 0, 0, 0, 0, 0x95).

Then you check for a response (should be 0x01).

Then you send another 6-byte sequence: (0x41, 0, 0, 0, 0, 0xFF).

...Sorry, guy, but you might be in over your head here. You should probably look into the compiler documentation to see if there's a library for using SD cards.

Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top