
Note: Please ensure you are using the latest version of SDFileSystem.bas (at time of writing: 4.1.4). If you are updating (or do not have the library), then ensure to remove the old "C:\ProgramData\Mecanique\Swordfish\Library\SDMMC" files (if you haven't already).
The capability of being able to store GB's of data will make some microcontroller programmers get a little excited. SD Cards are very easy to use and only have two downsides - the PCB footprint and they operate on 3.3V.
Solutions;
The first option is easy enough, you may need some more info for the footprint design. With the SD Card connectors visible, here's the pinout;
Note that pin 9 is before pin 1.
| Pin | SPI Mode | ||
| Name | Type | Description | |
| 1 | CS | I | Chip selection in low status |
| 2 | DI | I | Data input |
| 3 | VSS | S | Supply voltage |
| 4 | VDD | S | Power supply |
| 5 | SCLK | I | Clock |
| 6 | VSS2 | S | Supply voltage |
| 7 | DO | O/PP | Data output |
| 8 | RSV | ||
| 9 | RSV | ||
Swordfish ships with a library for SD Cards, and there are some advanced user modules available. For this article, I'll focus on the standard SD Card library "SDFileSystem.bas" as it is both covered in SF Help and there are three sample programs in the SF "Samples\SDMMC" folder. Here's the SDReadWrite.bas program;
// device and clock... Device = 18F2620 Clock = 20 // import SD file system, usart and conversion modules... #option SD_SPI = MSSP #option SD_SPI_SPEED = spiOscDiv4 Include "SDFileSystem.bas" Include "usart.bas" Include "Convert.bas" // variables... Dim Index As Byte // program start... SetBaudrate(br19200) USART.Write("Initialising card...", 13, 10) If SD.Init() Then // format SD card... USART.Write("Formatting, please wait...", 13, 10) QuickFormat // write data to SD card... USART.Write("Writing data, please wait...", 13, 10) If SD.NewFile("test.txt") = errOK Then For Index = 0 To 255 SD.Write("Line ",DecToStr(Index,3), 13, 10) Next SD.CloseFile EndIf // read data back... USART.Write("Reading data...", 13, 10) If SD.OpenFile("test.txt") = errOK Then Repeat USART.Write(SD.ReadByte()) Until SD.EOF SD.CloseFile USART.Write("Finished.", 13, 10) EndIf EndIf
Note that I have made three changes to the program;
There are some points to consider when working with the SD Card library, one is that the card must be formatted to FAT16 or FAT32 (can be done QuickFormat procedure). Another point is that one file can be open at any time. This does not stop you from opening a file, appending/writing, closing, then doing the same for others. When working with files, always ensure that you are working in the right directory, as all file specific commands will function in the current directory.
The above program covers the basics to interfacing with SD Cards, now its time to build the hardware to support it.
The SF help file has a circuit diagram for how to interface with SD Cards, however, keep in mind that it uses level shifting circuitry for 5V applications - many of the resistors are not required if a low voltage compatible PIC and 3.3V regulator is used.
This single layer development board is purpose built to interface with SD Cards, and provides headers to work with external circuits/projects. There's also a 3.3V output to power external circuits or connect common grounds. Here's the layout of the board, and schematic below (download
the bottom copper layout and make your own board!);
The SD Card socket used for this project was brought from sparkfun electronics for $3.95. Download the bottom copper layout
here and make your own development board.
(Note: The PIC shown is a 18F2520, although in real life I used the low voltage variant 18LF2520).
To program the PIC; simply connect your PICKit to the PICKIT2 PRG 6 pin header and set Vdd to target in the PK2 software (a safe option for in-circuit programming).
When complete, plug the PK2 into the other 6 pin header to receive the UART output.
Here's an animated GIF image of the PICKit 2 UART Tool receiving from the PIC.
The final product (top view);
And the bottom view;
