SD Memory Card Interface using pic microcontroller

SD memory cards, especially the ones under 1GB are cheap, relatively easy to interface and provide vast amounts of memory for imbedded control.

This device lets you ‘talk’ to sd cards through your serial port. Communication is in ASCII so you just need a terminal program to access it. (Note: the photos shown are of the rev-1 device with various changes, documentation is for the rev-2).

SD Memory Card InterfaceThe Hardware

SD sockets are available from SparkFun Electronics. The socket needs to be surface mounted so it was placed on the foil side of the single sided board (mouseover image above to see). The connections are close to 0.1 inch centers, so the layout was not difficult.

The PIC16F819 was chosen for its hardware support for SPI. (SD cards will usually work through an SPI interface) Running at 20Mhz, the SPI port clocks data at 5Mhz, and the serial communication is able to run at 115200 baud in software.

A quick look at the schematic shows one way to interface the 5 volt PIC to the 3.3 volt SD card. A red LED drops the 5 volt supply by about 1.8 volts to feed about 3.2 volts into the card. This is within the operating range.

Signals from the PIC go through a 1k/2k voltage divider to feed signals to the card. Signals back from the card feeding SDI input are a problem because the PIC uses schmit-trigger inputs in SPI mode requiring 3.5 volts for the high level. The circuit provides a 0.6 volt shift so the output of the card back to the PIC ranges from 0.6 to 3.8 volts. Cheesy, but it works.

The Software

The main issue with the software is configuring the SPI port. After a lot of diddling around, the proper setup seems to be: SMP=1, CLE=1, and CKP=1. See the software listing for the complete setups of SSPSTAT and SSPCON registers in sspinit. The routine that writes to the card also reads it, since, in SPI, read and write are simultaneous operations.

Once connected to a terminal program (115200 baud, 1 stop bit, no parity, no handshaking), the following commands are available:

Command Function
S
Select Card
D
Deselect Card
Zddd
Send ddd bytes of clocks
W xx xx xx xx xx ...
Send series of (hex) bytes
Cdd ddddd
Send command dd containing
address 512*ddddd
Xddd xx
Send ddd bytes of (hex) value xx
Rddddd
Read ddddd bytes from card
Ctrl-C Resend last command

 

For more detail: SD Memory Card Interface

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation and engineering. My writing style is clear and simple, and I am skilled in using infographics and diagrams. I am a great researcher and am able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter