Having got our sd card to respond to the SPI initialisation routines and “boot up” correctly, it’s time to actually start reading and writing data to the card.
The first thing to understand is that an SD card is made up from a series of sectors. Each sector is 512 bytes in size (almost always for SD cards) and the SD card likes to read and write entire sectors at a time.

not to scale 😉

Reading data is a case of sending the appropriate “command packet” – 6 bytes which are:

  • the command byte (CMD17)
  • 4 parameter bytes (the address of the sector to read)
  • two CRC bytes (an accurate crc value is not actually necessary in SPI mode, but two bytes still need to be sent)

It is possible to read multiple sectors of data, one after the other, and use the sd card like one massive serial eeprom, but for now, we’re going to just work one sector at a time (this approach is much better suited to working with FAT16-based files, which we’ll discuss later).

The command value to read a single sector of data is CMD17. After sending the command, we have to check for an “ok” response from the sd card then wait for the “not-busy” response (any non-0xFF value).

Boolean sdReadBlock(UInt32 sec){
     UInt8 v;
     UARTPrint(“Starting to read block “);
     UARTPrintLn(” “);
     v =  sdCommandAndResponse (17, ((UInt32)sec) << 9);
     if(v & FLAG_TIMEOUT) {
          UARTPrintLn(“start read block command 17 timeout”);
          return false;
               v = sdSpiByte(0xFF);
          }while(v == 0xFF);
          if(v != 0xFE) {
               UARTLog(“Read block response”,v);
               return false;
          return true;

This function accepts a single 4-byte parameter, the sector address to open, and returns true or false to indicate whether the card responded correctly.
It also resets a counter curPos, which will keep track of the number of bytes we’ve read from the current sector on the disk. When we’ve had exactly 512 bytes back from the disk, we’ll have to “close” the current sector by reading back the two bytes that make up the crc for the previous 512-byte data stream.

void sdSecReadStop(){     
          UARTPrint(“add stuff bytes to close sector – “);
          UARTPrintLn(” “);
     while(curPos < 512){
     // read back the two CRC bytes
     UARTPrint(“CRC “);
     UARTPrint(” “);
     UARTPrintLn(” “);
     UARTPrintLn(“End read sector”);
     UARTPrintLn(” “);

Now we can open a sector and close it again, the last piece of the puzzle is to stream the data back from the sd card and do something with it! When we come to make our audio player, we’ll use the data immediately – sending it to the speaker – but for testing, or for more general purpose use, we’ll put the data into a series of buffers and use it later.

Now – here’s a thing.
So far, everything discussed can be ported to another platform (AVR/Ardunio for example) but when it comes to storing the data in our internal buffers, there’s a peculiarity with the PIC microcontroller – or at least, with most compilers that support using arrays. It may be the same with Arduino and/or other compilers, but we’ve always found that large arrays are often difficult to use.
This is because of the way PICs store data in RAM – it uses “banks” to keep the data in memory, and one array can’t usually span more than one bank of data. In practice, this means a limit of about 90 elements in an array.

To keep things simple, we’re going to store an entire sector’s worth of data (512 bytes) in 8 eight arrays of 64 elements.

unsigned char wavData1[64];
unsigned char wavData2[64];
unsigned char wavData3[64];
unsigned char wavData4[64];
unsigned char wavData5[64];
unsigned char wavData6[64];
unsigned char wavData7[64];
unsigned char wavData8[64];

unsigned char readSector(){
     unsigned short cy;
     unsigned short cv;
     for(cy=0; cy < 512; cy++){
         UARTByte(r);    //write to serial for debugging
         if(cy< 64){
         }else if(cy<128){
            cv=cy- 64;
         }else if(cy<192){
         }else if(cy<256){
         }else if(cy<320){
         }else if(cy<384){
         }else if(cy<448){
         }else if(cy<512){
     UARTPrintLn(” “);

Now we can read a sector-ful of data, it’s time to write some data back.
There are any number of ways you can get data into the PIC/microcontroller in order to write them to the sd card. We’ll leave that side of things for another post – for now, here are some functions to write data to a specific sector on the card.

The command to write a single sector-ful of data to the card is CMD24
(you can write an entire stream across multiple sectors if you’re using the card as a large serial eeprom chip, but this makes things difficult if we’re going to work with FAT16 formatted cards in future).

When writing data, the 6-byte data packet consists of:

  • single command byte
  • four parameter bytes (sector to write to)
  • single crc byte
  • a single byte representing a token confirming the command request
Boolean sdWriteBlockStart(UInt32 sec){
     UInt8 v;
     v =  sdCommandAndResponse(24, ((UInt32)sec) << 9);
     if(v) {
          UARTLog(“write block start response”,v);
          return false;
          UARTPrintLn(“write block started”);     
          // keep track of how many bytes we’ve written in this sector
          // (when we hit 512 we should expect some extra bytes in the packet data)     
          // send the correct token for CMD17/18/24 (0xFE)
          // REMEMBER the token for CMD25 is 0xFC               
          return true;

Once the card has responded with an “ok” after starting to write a sector, the next bytes streamed to the card are written to the selected sector. We need to keep track of the number of bytes written – when we hit 512, it’s time to close the current sector, read back the crc bytes, then open another – you don’t have to write to the sequentially next sector every time, you can write to any old location if you like!

Boolean sdWriteByteToSector(UInt8 b){     
     UInt8 ix;

Boolean sdWriteToSectorClose(){          
     // finish closing the sector
     UARTPrintLn(“writing stuff bytes to close sector”);
     // send two CRC bytes
     // response should immediately follow
     // (for writing, we’re looking for 0bXXX00101 data accepted)
     UARTLog(“write finish response”,r);
     // now wait for the sd card to come out of busy
     UARTPrintLn(“write finished”);          

// write some data to sector numbered five:
void writeSomeData(){
    UARTPrintLn(“Writing one block”);

         for(iy=0; iy<=255; iy++){

That’s pretty much it.
We can now read and write to an individual sector on the sd card.
There are commands for reading and writing to/from multiple blocks at a time, but we’ll leave those for again; for FAT formatted cards, we need to be able to read data out-of-sequence and possibly even write files into different (non-consecutive) sectors.