SD card read/write Using PIC18F4550

///SD card read/write Using PIC18F4550
  • SD Card interfacing in PIC18F4550-compressed

SD card read/write Using PIC18F4550

In this tutorial, we will be learning how to program the PIC18F4550 to perform the raw SD read/write functions on an SD card. To implement this hobby project, we will be using the popular SPI protocol and the software UART to display the output.

Microchip’s PIC18F4550 is a small microcontroller in terms of its functionalities as well as the memory space. To design a code to implement the raw SD read/write using this microcontroller requires a few considerations. In our previous tutorial, we’ve discussed the process of SD card interfacing with microcontrollers.

To begin with, it only has a 32KB program memory and 2048 bytes of data memory. One of the features of SD card is it has a limited memory space is available when coding the program.
We’re going to use UART for debugging the SD card. The Receive pin(Rx) of the UART and Serial Data Out (SDO) pin of SPI are sharing the same port pin of PIC18F4550 and here the software UART is used instead of a hardware interrupt.


The first step is to interface the hardware with the software using the microchip’s library function, OpenSPI( ). The OpenLab uses RB4 as chip select(CS) or the slave select(SS) pin instead of the usual RA5 pin. The direction of RA5 must be set as output and keep it as low in order to apply this change. The RB4 pin must be set as an output pin and should be used as the chip select pin.

You can choose the CS pin as you desire. The SPI pins RB4 (CS), RB1(SCK) and RB0(SDI) are analog pins by default. So the pins configured to digital pins. Set the SPI at mode(0,0) and in the 8-bit asynchronous mode with the input sampled at the middle of the data out. The directions of the SPI pins apart from the adopted RB4 pin are set in the OpenSPI( ) function. The soft_hard_init( ) function shows how the SPI is set up successfully.


Next, we must create the basic functions to carry out the transmission and reception of data and command with the SD card. The function WriteSPI( ) is used to transmit command or data to SD card. The function ReadSPI( ) is used to read every datum or response that is received from the SD card. While receiving a response from the SD card, it is important to make sure that the chip select pin must be low prior to reading.


There is an exception while using Microchip’s ReadSPI( ) here.

The above code shows the ReadSPI( ) microchip library function. In line 6, 0x00 byte write to the SSPBUF register to initiate a bus cycle. The microcontroller generates the bus cycle only after the buffer is filled. Writing 0x00 does not fill the buffer. Rather, it clears the buffer. A 0xFF write to SSPBUF register in order to generate a bus cycle before the arriving of response. So we need to use the modified version of the ReadSPI() function in our code.

The command has a fixed length of 6 bytes. A dummy byte (0xFF) is sent in order to generate the bus cycle.

First, transmitting the argument ‘ cmd’, which contains a single byte hexadecimal command number. The variable ‘arg’ contains the 4-byte argument, It transmitted as 8 bits starting from the most significant bit (MSB) at a time and finally sents the CRC bit. Before starting the initialization process, we must create a function called dummy_clocks( ) and another function called proceed( ). The purpose of dummy_clocks( ) is to send bus clocks to the SD card.

Set the chip select to high to send each dummy byte. Now, the purpose of proceed( ) is to send bus clocks at instances where the buffer of the microcontroller is empty during the execution time.

As you may notice, there is a difference between the two functions all though they may appear similar. To generate the clock pulses using the function proceed(). Sent the 0xFF byte to the buffer and it does not send to the SD card. To write the 0xFF byte turn off the slave selection.

While in the case of dummy_clocks( ), our purpose is to send clock pulses to the SD card soon after power-up or prior to sending a command, to prepare the SD card for the appropriate communication frequency. At this point, writing a 0xFF does not actually mean anything to the SD card.

For a detailed explanation of the timing and flow diagram of SD card initialization, read and write process, please refer the tutorial: Interfacing Microcontrollers with SD Card

SD Card Initialization

After power up, wait for one second or two seconds. In the initialization process, the first step is to make the card to expose to the frequency at which the data is being sent. And then reset the SPI mode from its default operating mode. For this, send around 75 dummy bits approximately (dummy byte * 10 times = 80 bits) and the card will be ready to communicate in the required frequency.
The first command to send is CMD0. Which resets the SD card and set the card at an idle state.

The loop do-while continues to runs until the card is inserted into SD card slot. And it receives an idle response (0x01). At this point, the SD card is in an idle state and will only accept the commands, CMD0, CMD1, ACMD41, CMD58 and CMD59.

Sent the command CMD8 is after the checking the version of SD card. If the SD card is Version 2, then continuously sent the CMD1 command after receiving the response of the CMD0 command. Keep sending the CMD1 command till the SD card gets out of its idle mode and returns a 0x00 byte as a response.

To read or write the raw data first accept the command CMD1. There is also the pat.bility that the SDC requires a CMD41 command to initialize the card instead of the CMD1 command.  Send the command CMD41 to write a general code. Sent the command CMD1 if the command CMD41 is to reject. Sent the command CMD1 if any rejection of the command CMD41 has occurred.

The CMD<n> command is CMD55 command followed by CMD<n>. So follow the same procedure to send both the commands i.e. CMD55 and wait for a 0x01 response and then CMD41 and wait for a 0x00 response. Send command CMD1 otherwise after a definite amount of time the command will not accept.

Block Length

After the initialization is complete, set the block length. The command to set the block length is CMD16 and it takes the argument as the size of the block. Wait for the command to accept.

Write to SD Card

The initial command is the same to send for raw data read/write.  first, we will look at how to write a single block.
First, send the command CMD18 with the argument. The argument contains the address of the sector. The command is to accept with a usual response of 0x00 byte.

Then the  SD card is waiting for the microcontroller to give a start token of 0xFE. This indicates the beginning of the block write and the card would be ready to accept a block of data. After block write completes then send two 0xFF bytes as CRC. It is mandatory to send the CMD13 command after the completion of every data block write.

Read from SD card

Next, we will see how to read a block of data from an SD Card. First, send the command CMD24 and wait for the command to accept. Then wait for its response. The microcontroller immediately receives the command 0xFE from the SD card. It is the indication of the start of data transmission. Set the chip select pin as low and remains the same throughout the process. The command CMD12 indicate the end of the reading process and it sent a block of data at the end.

Multi-Block write

To write multi-block use the command CMD25. The 0xFC byte indicating start token indicating in the case of a multi-block write.

Finally, once the data write is over, issue a stop token 0x4D to indicate the end of the transmission. In the case of a multi-read, wait till the SD Card issues a 0xFE byte(start read) after accepting the CMD18 command.

Multi-Block read

Now, continuously keep reading the data bytes until the end of the specified number of blocks. The readings are as one block at a time because of space limitation. And collects every block of data as different array in a static declaration. And using a 0x4C as stop token at the end of the reading process.

Finally, create a switch condition for the user to choose between the different functions of the system.

Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this

Leave A Comment

Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
25% OFF - OpenLabPro Course
Use Coupon: CRS25UL

PIC Microcontroller Primer
Want to learn indepth programming, peripheral interfacing and driver development?
Launched: IoT online Course - 25% Off when you complete your order today.
Checkout Now

Subscribe To our Email Newsletter

Be the first to get latest updates and exclusive content straight to your email inbox.
Send me Tutorials and Guides