SD card FAT library for CCS C compiler

This is a simple FAT library (FAT16 and FAT32) for CCS PIC C compiler for reading files from MMC/SD card module. This library works with PIC microcontrollers with limited RAM and ROM like PIC12F1840 MCU. This library works with FAT16 and FAT32 formatted MMC/SD card, it’s automatically detects the FAT type (FAT16 or FAT32).

About the FAT library:

  • MMC/SD Card driver integrated in the library
  • This library is for reading files only
  • This library works with MMC/SD cards which do not have MBR (Master Boot Record) on its first sector. The MMC/SD card may run on PC or camera but the library may not be able to detect the FAT file system and hence FAT initialization error because the MBR and to make it run just format the card using Windows
  • Only short file name is supported (file name + extension < 13 characters)
  • Only one file can be opened at a time, if a 2nd file is opened the first file will be automatically closed
  • The user can set buffer size starting from 1 byte. Bigger buffer size –> higher speed.
  • Letter case insensitive
  • Library functions read files from the root directory only.

Library Functions:
MMC/SD card and MCU connection:
The MMC or the SD card is interfaced with the microcontroller using SPI protocol, with this library I can use hardware SPI module of the microcontroller or software SPI which is implemented by the compiler. The hardware SPI is much faster than the software SPI.
Hardware SPI module is used and the CS pin is connected to pin RB3:

Software SPI: here the SD card communicates with the microcontroller via soft SPI:

sdcard_init(); : Initializes the media. Returns 0 if OK, non-zero if error. This function must be called before any other function.
sdcard_read_byte(int32 addr, int8* data); : Reads a byte from the MMC/SD card at address addr, saves it to pointer data. Returns 0 if OK, non-zero if error. 
fat_init(); : Initializes the FAT library. Returns 0 if OK, 1 if error.
fat_open_file(int8* fname); : Opens file stream. fname is file name. Returns 0 if OK and 1 if the opening process faced problem (file not found, long file name ………..).
fat_read_data(int32 size, int8* data); : Reads an array data of size size from the opened file. Returns 0 if OK, 1 if error or end of file is reached.

Library Download:
FAT Library for CCS PIC C compiler

Wave player using PIC18F4550 microcontroller

Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “SD card FAT library for CCS C compiler”

  1. Hi, i’m using your (excellent) fat32 lib for CCS (PIC18f micro). Is there a way to copy or to rename a file? Thank you

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