Interface SD Card with PIC Microcontroller

October 26, 2014 | Anonymous | 0 comments


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).

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.

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

ddd = decimal value, xx = hex value

 

Commands can be combined on a single line, for example...

 

C17 2 R520

 

Command 17 (read sector) at address 1024 (start of sector 2) followed by read back 520 bytes.

 

C0 0 R2 C1 0 R2 C1 0 R2

 

Setup board for SPI, read status, init board, check status, check init, read status.

 

Note that the C command generates an effective 'sector address' by multiplying by 512. It also generates the mandatory 95 (hex) CRC that is only required for Command 0 to put it in SPI mode. After that, the CRC is not required for any other commend.

 

If you want to issue a command the needs specific parameters other than a sector address, you need to use the W command with everything in hexadecimal.

 

Eventually, this device is to be made into an SD card programmer/reader. For now, its a way to probe the operation of various manufacturers cards.

DOWNLOADS

Source Code | Object File | Schematics | Board Layout | Parts Placement

Comments

Post Your Comment

Database based Nepali Date Converter

Convert Nepali date (B.S.) to English Date (A.D.) or Vice-Versa

Nepali Date
Gregorian Date

From 1900-01-01 A.D. to 2020-12-30 A.D.

From 1956/09/19 B.S. to 2077/09/15 B.S.

 

Download Nepali Date Converter

Like us on Facebook

Follow me on Google Plus

Follow me on Twitter