- Published: Monday, 05 October 2009
- Written by Graham Mitchell
- Hits: 15242
Note: Please ensure you are using the [emphasisbold]latest version of SDFileSystem.bas[/emphasisbold] (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.
- Use a 3.3V compatible PIC (all of the LF PIC's are designed for low voltage) & a LD1117V33 (3.3V regulator).
- OR use voltage level switching resistors to interface with a 5v PIC (see the SF help file - great example).
- Design a footprint with your circuit design program of choice. (Eagle is free)
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.
|1||CS||I||Chip selection in low status|
Programming with SD Cards
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;
Note that I have made three changes to the program;
- Changed the first line from Device = 18F452 to Device = 18F2620.
- Added #option SD_SPI = SW as the 18F2620 has hardware SPI errata.
- Changed SD.Init(spiOscDiv64) to SD.Init() as we are now using the software SPI module.
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!);
(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;