Simple FAT and SD Tutorial Part 4

In the previous parts of this tutorial, we have built both an FAT library as well as a test program to communicate with the SD card. Now it’s time to wrap it up with final parts of code to read a file from SD card and print it out.

This part will utilize the previously made FAT library with a few tweaks – it turned out that some byte and word-sized values needed explicit casting to unsigned long so that the calculations worked as they should. The new library and all code shown here can be found from the updated project zip.

Initializing the SD card automatically

Instead of manually pressing 1, 2, and 3, we’ll now write a single function to initialize the SD card to SPI mode. For standard SD (not high capacity SDHC) cards, it’s enough to:

  1. Send clock pulses for 80 cycles (“read” 10 bytes)
  2. Send command 0x40 (it should return 1)
  3. Send command 0x41 until it returns 0 (it returns 1 while the card is busy)
  4. Send command 0x50 to set read block size (as well as write block size)

Here’s the code to do just that (sd_sector and sd_pos will be used shortly):
Continue reading Simple FAT and SD Tutorial Part 4