PIC18F46K22 Interface with SD Card – Read & Write Files | mikroC

This post shows how to interface PIC18F46K22 microcontroller with SD card formatted with FAT32 file system.
It shows how to create folders and files, how to write to a text files and how to read and print a text file.
The compiler used in this project is mikroC PRO for PIC.

FAT32 Library for mikroC compiler:
MikroElektronika provides a nice library for FAT32 file system (& SD card). This library can be downloaded from the following link:
FAT32 Library for mikroC compiler

The extension of the downloaded file is .mpkg, and in order to open and install the library we need a small software named: Package Manager, it’s also a free software provided by MikroElektronika, download link is below:
Package Manager download

Hardware Required:
This is a list of the basic components required for this project (all components are shown in the circuit diagram)

  • PIC18F46K22 microcontroller   —->  datasheet
  • FAT32 formatted SD card (MMC, SD, SDHC, microSD ….)
  • micro SD card module
  • USB-to-serial UART converter (like: FT232RL module)
  • Breadboard
  • 5V source
  • Jumper wires

To connect the microcontroller with the laptop (PC) we need (one of the following):
—> USB to serial UART converter such as FTDI232RL module, or
—> an Arduino UNO board (connect reset pin to GND, Arduino GND to PIC GND, TX to TX and RX to RX), here the PIC18F46K22 can be supplied from the Arduino board, or
—> USB-to-RS232 converter (if the PC doesn’t have a RS232 com port), MAX232 chip, RS232 female connector and four 10 uF capacitors.

The easiest way is to use the FTDI232RL module.

Interfacing PIC18F46K22 with SD card circuit:
Project circuit diagram is shown below.

As a small hint, it is recommended to add a voltage level translator to MISO line in order to step up the 3.3V that comes from the SD card into 5V which goes to SDI1 pin (#23) of the PIC18F46K22 microcontroller, here we can use the integrated circuit 74HCT125.

In this project I used micro SD card module, this module is supplied from circuit 5V source, it contains the AMS1117-3V3 voltage regulator which is used to supply the SD card with 3.3V. Also this module contains an IC which is 74LVC125A and it is used as level translator (from 5V to 3.3V).

PIC18F46K22 SD card micro SD card module

All the grounded terminals are connected together.

The PIC18F46K22 microcontroller does have 2 hardware SPI modules (MSSP1 and MSSP2), in this example the SD card module is connected to hardware SPI1 pins (SCK, MOSI and MISO) and SS (slave select or chip select) pin is connected to pin RD4.

The FT232RL board (USB to serial converter) RX pin is connected to PIC18F46K22 TX1 (RC6) pin which is UART1 module transmission pin (the PIC18F46K22 has 2 hardware USART modules).

In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz and MCLR pin is configured as an input pin.

Interfacing PIC18F46K22 with SD card C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.

The SD card chip select pin (SS) is connected to pin RD4 of the PIC18F46K22, it’s defined in the C code as:

Before initializing the FAT32 library (as well as the SD card), the SPI clock frequency is set to lowest (Fosc/64):
Fosc/64 = 16MHz/64 = 250 kHz is below the recommended clock frequency for initializing the SD card which is 400 kHz.

After the initialization of the FAT32 library we can go to higher speed by setting the clock frequency to Fosc/4:
Fosc/4 = 16MHz/4 = 4 MHz.
This means we’ve a transfer rate of: 4 Mbit/second.

Used functions:
FAT32_Init(): this function initializes the FAT32 library as well as the SD card, it returns 0 if OK and non-zero if error.

FAT32_MakeDir(“Test Dir”): creates a new folder (directory) with name “Test Dir”, returns 0 if OK and non-zero if error.

FAT32_Open(“Log.txt”, FILE_APPEND): this function creates (& opens) a new file named “Log.txt” if it doesn’t already exists, if the file exists, it opens and moves the cursor to the end of the file, returns 0 if OK and non-zero if error (or file exists).

FAT32_Write(fileHandle, “Hello, …”, 113): writes a text “Hello, …” of length 113 bytes to the file associated with fileHandler.

FAT32_Close(fileHandle): closes the file associated with fileHandler, returns 0 if OK and non-zero if error.

FAT32_Open(“Log.txt”, FILE_READ): opens the file named “Log.txt” with read permission, returns 0 if OK and non-zero if error.

FAT32_Read(fileHandle, buffer, 113): this function read 113 bytes from the file associated with fileHandler and stores them to the array buffer.

Full mikroC code:
Configuration words (for PIC18F46K22 MCU):
CONFIG1H = 0x0028
CONFIG2L = 0x0018
CONFIG2H = 0x003C
CONFIG3H = 0x0037
CONFIG4L = 0x0081
CONFIG5L = 0x000F
CONFIG5H = 0x00C0
CONFIG6L = 0x000F
CONFIG6H = 0x00E0
CONFIG7L = 0x000F
CONFIG7H = 0x0040

This project was tested in hardware circuit using 2 original Samsung microSD cards (SDHC), one with capacity of 4GB and the the other with 32GB.
MikroElektronika USART terminal result is shown below (data sent from the microcontroller to the laptop):

mikroC USART terminal output for SD card

And the created folder (Test Dir) and file (Log.txt) are shown below:

Samsung micro SD card files created by PIC18F46K22

This project could be simulated with Proteus and it should give the same result, the simulation of this project is near to the one shown in the following video where PIC18F4550 microcontroller and CCS C compiler were used (not that simulation circuit is not the same as real hardware circuit, project hardware circuit is shown above):

Proteus simulation file download link:
PIC18F46K22 + SD card

SD Card file is FAT32 image file (FAT32_MBR.ima), it can be downloaded from the link below:
SD Card FAT32 image

2 thoughts on “PIC18F46K22 Interface with SD Card – Read & Write Files | mikroC”

  1. could you help to solve this error “Unresolved extern ‘Mmc_Chip_Select’ __Lib_Mmc.c” while compiling above mentioned code

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