SD-Card with CCS C Compiler using pic microcontoller

SD Card

Description

This project shows how to use a SD-Card with FAT16 file system. It is based on the example MMC/SD-Card driver (mmcsd.c) and FAT file system driver (fat.c) delivered with the CCS C compiler. Unfortunately, the example didn’t work properly for me. So I searched the web and found some required changes to make it work. All modifications are described in the following article.

SD Card

Hardware

SD-cards can be interfaced to a microcontroller via SPI-Bus, hence only 4 I/O pins (SDI, SDO, SCK, CS) are required to connect the SD-card to the PIC.

SD-cards operate at 2.7V .. 3.6V while the 18F PICs usually operate at 5V. Hence a voltage level translation is required for the SD-card inpus (SDI, SCK, CS). There are a lot of examples available in the web where a voltage divider with resistors is used to translate the 5V signals to 3.3V. This will work, but at higher SPI speeds this might cause problems. Due to that, I used a 74AC125 buffer to do the level translation which works more reliable than the voltage divider.

SD-Card Module

Here the schematic of the SD-Card module with the voltage level translation. The pins on the connector SV1 need to be connected to the microcontroller pins.

Important: SDO and SDI are the pin names from the SD-card point of view, i.e. SDO needs to be connected to SDI on the microcontroller and SDI needs to be connected to SDO in the microcontroller!

SD Card with CCS C Compiler1

Software

As mentioned in the description for this article, I used the ex_fat.c example which is delivered with the CCS C compiler.

Unforunately, when I run the demo and created some files with the demo, I was not able to see the files when I pluged the SD-card into my PC. After some investigations I found a solution for this problem in the CCS forum which I will summarize here.

 

Read

Modifications in mmcsd.c

Search for

 

uint32_t g_mmcsdBufferAddress;

and add under it:

 

uint32_t g_mmcsdPartitionOffset;

Next, just before the “mmcsd_init” function add a new function:

 

void mmcsd_check_part(uint16_t off)
{
if (g_mmcsd_buffer[off + 0] == 0x80)
{
// active partition 

uint8_t t;
t = g_mmcsd_buffer[off + 4];
if (t == 0x04 || t == 0x06 || t == 0x0B)
{
// FAT16 or FAT32 partition 

g_mmcsdPartitionOffset = make32(
g_mmcsd_buffer[off + 11], g_mmcsd_buffer[off + 10],
g_mmcsd_buffer[off + 9], g_mmcsd_buffer[off + 8]) * MMCSD_MAX_BLOCK_SIZE;
}
}
}

more: SD-Card with CCS C Compiler

Leave a Comment

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