SD Card driver and FAT Library for CCS C compiler

The CCS C compiler is one of the best Microchip PIC® compilers especially when we talk about code optimization. This compiler comes with many drivers and libraries, two of them are MMC/SD card driver (file: mmcsd.c ) and FAT library (file: fat.c ), the FAT library supports FAT16 and FAT32 file systems where the user has to choose between them (not both at the same time), hence the RAM usage is lower. But unfortunately the MMC/SD card driver has some small bugs (if I can say that) and does not support most of the MMC/SD cards available on the market today including SD and SDHC cards. The other library which is the FAT library also does not support even medium size cards (for example 2GB SD card).

Things we should know:
Abbreviations:
MMC  —> Multi Media Card
SD    —> Secure Digital
SDSC —> Secure Digital Standard Capacity
SDHC —> Secure Digital High Capacity
SDXC —> Secure Digital eXtended Capacity
Note that SD and SDSC are the same.
To distinguish between SD card types:
SD (SDSC): Size <= 2GB
SDHC:  4GB <= Size <=32GB
SDXC: 64GB <= Size <= 2TB (2048GB)

The SDSC cards can be formatted with FAT16 or FAT32 file system but the SDHC cards accept FAT32 only.

CCS C compiler MMC/SD card driver and FAT library modified:
As mentioned above the CCS C compiler has built-in libraries for MMC/SD cards and FAT file system but they are not working properly (as I think), for that I made some modifications (improvements) for the two libraries in order to work as they should be, here are some fixes:
MMC, SD (SDSC) and SDHC cards support
FAT library fixed and it’s working with any size
Many users (especially students) use Proteus simulation, these libraries now are working with it
Supports hardware SPI (SPI1 module) and software SPI

I named the new two files as : mmcsd_m.c and fat_m.c (m for modified).
Installation of these files is very simple and it is done just by adding the two files to CCS C driver folder (example:  C:\Program Files\CCS\Drivers) or by adding them to project folder. The older files (mmcsd.c and fat.c) can be kept as they are since the two new files have different file names. Name of driver files is not so important and they can be changed.
The MMC/SD card driver works with devices with RAM > 512 bytes.

*** For microcontrollers with limited RAM and ROM there is another FAT library for reading files from SD cards, it supports FAT16 and FAT32 at the same time and works with MMC, SD and SDHC cards:
SD card FAT library for CCS C compiler

MMC/SD card driver for CCS C compiler download:
The new MMC/SD card driver supports MMC, SD (SDSC) and SDHC cards and it has the name mmcsd_m.c and it can be downloaded from the link below:
mmcsd_m.c download

FAT (FAT16 and FAT32) library for CCS C compiler download:
The FAT library has the name fat_m.c and it can be downloaded from the link below:
fat_m.c download

The default file system of the the library is FAT32 which is recommended because it’s more tested, if you want to use FAT16 file system just define it in your project source code (ie: #define FAT16).

The user functions for both libraries are included with the files and there is no need to put them here.

If the MMC/SD card has Master Boot Record (MBR), the library will work with the first partition.

Generally, the very first sector (sector 0) of the SD card contains the MBR or the file system (FAT16, FAT32 …) boot.
Formatting the MMC/SD card with Windows may do no thing to the very first sector (sector 0) and keep it as it is depending on which it contains (MBR or file system boot).
There are many software which are able to add or remove the MBR from sector 0, for example formatting the MMC/SD card with the following two tools will add MBR:
The first one is: SD Memory Card formatter
and the second one named HPUSBDisk    —->   download link

Connecting the SD card to PIC microcontroller:
The communication between the microcontroller and the SD card uses SPI protocol. A standard SPI bus consists of four signals: Serial Clock (SCK), Master Out Slave In (MOSI), Master In Slave Out (MISO) and Slave Select (SS). The SS signal also called Chip Select (CS).

As known all SD card types works with 3.3V only which means interfacing an SD card with 3.3V microcontroller doesn’t need any additional component (except pull-up resistors if needed). But what if we want to interface a 5V microcontroller with SD card?

Basically we can’t directly connect a 5V microcontroller to SD card, otherwise our SD card is dead! but we can do that with the help of external components. We’ve to be sure that the power supply as well as the three input signals (SCK, MOSI and SS) are in the range of 3.3V, The power supply of the SD card should be stable and equal to 3.3V, we can do it using voltage regulator like the AMS1117-3V3. For the input signals (SCK, MOSI and SS) we have to step down the 5V into 3.3V and that is easy, we can use 3 voltage dividers where each voltage divider consists of 2 resistors (not recommended), or we can use high speed level converters (level shifters) such as 74HC405074LVC125A ……

Today, there are many SD card modules (or microSD modules) available for example Adafruit micro SD card breakout board which uses the level shifter 74HC4050, or there is another one which is widely used (low cost), it is shown below, this module is supplied with 5V and it consists of the AMS1117-3V3 voltage regulator, it also has the level shifter 74LVC125A:

micro SD card module

I’ve finished with the lines: SCK, MOSI and SS and now let’s discuss about the last line which is MISO.
The MISO line is the data output line of the SD card and with the MISO the SD card sends data to the microcontroller. Since the SD card works with 3.3V, the maximum voltage of the MISO signal is also 3.3V. If we’re working with 3.3V microcontroller that’s OK and we don’t need any additional component between the SD card and the MCU. But if we have a 5V PIC microcontroller, that is a problem (I don’t think that!).

As written in the datasheet a 5V PIC microcontroller needs at least 4V (0.8 * VDD) at MISO pin to detect a digital high level (when we work with hardware SPI module). But from my experiments on several types of PIC microcontrollers (PIC12F, PIC16F and PIC18F) a 3.3V on the SDI pin is sufficient for the microcontroller to detect digital high, that means we can connect the SD card MISO pin directly to the microcontroller hardware SPI MISO pin (SDI pin).

But it is still recommended to use another level shifter to translate the 3.3V (comes from the SD card) to 5V (goes to the PIC microcontroller). For that we can use an external component such as 74HCT125.

Note that there is no need for the level shifter of the MISO signal if software SPI is used, therefor the SD card MISO line is connected directly to the microcontroller.

Microchip provides a nice MMC/SD card board called PICtail, it works with 3.3V as well as 5V taking into consideration all what I’ve said (and more). It uses 74VHCT125 as voltage translator (contains 2 x 74VHCT125).

Example:
This is a list of examples done in this blog.

Read and write files from and to SD card with PIC18F4550 – CCS C   –> Hardware SPI, FAT32
DataLogger with PIC18F4550, SD card and DHT11 sensor              –> Software SPI, FAT32
CCS C FAT example – ex_fat – with PIC18F4550                             –> Software SPI, FAT32
Temperature and humidity datalogger with PIC18F4550 & DHT22   –> Software SPI, FAT32
Temperature data logging with PIC18F4550 and DS18B20 sensor   –> Software SPI, FAT16

6 thoughts on “SD Card driver and FAT Library for CCS C compiler”

  1. Hi bro, one question, have you updated the files with new code? Since I tried to use them with some memories and it doesn’t recognize all of them, it recognizes one but it doesn’t create any file inside the memory. Cheers!

    hi bro, una pregunta, no has actualizado los archivos con nuevo código?, ya que los intente usar con unas memorias y no reconoce todas, me reconoce una pero no crea ningun archivo dentro de la memoria. saludos!

  2. André Bergamo

    Hello, congratulations for the site, excelent posts…

    I’m trying to compile the program with this driver but every time i have problems to initialize fat library…

    I’m using ccs 5.76.

    if i use the original drivers from ccs i can itialize FAT and write a file in to a 2GB sd card.

    Do you test the drivers with hardware? or just in protheus?

    Sorry for my bad english…I’m brazilian….

      1. Thanks a lot for the answer…

        I applied the fix to the mmcsd_m.c file and I still get the fat initialization error. I am using the hardware exactly as shown in the example “Read and write files from and to SD card with PIC18F4550 – CCS C”.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top