9 Ekim 2017 Pazartesi

SD Card Example

We will create a text file in an SD card and write some text inside. Microchip MDD legacy library did not work for XC8 as expected. It is for XC16 and XC32. I tried to modify header files but I gave up. I followed the example at http://www.studentcompanion.net/en/interfacing-sd-card-with-pic-microcontroller-xc8/  which is using  Chan's FatFs file system. The example was for 18F45K22, my old proteus was not supporting this PIC thus I used PIC18F45K20. It needs a little modification but working well.

Proteus: PIC18F45K20 connected to an SD card using RC3, RC4, RC5 ports and RC0 is for ChipSelect CS.  To create image file for SD card I used winimage. Select custom format, FAT16, 4096 sectors for 2MByte. Save the image give a name "sdcard2M.ima" and select this image in Proteus. If you run the simulation. "test.txt" will be created inside the image. Stop the simulation and open the image file again. You will see the "test.txt file",  we extract and look inside.






MCC: 1MHz internal oscillator as usual. select MSSP peripheral for SPI Master. No interrupts. Rename RC0 as SD_CS; this is important and used in header files. 





XC8: There are some modifications at diskio.h
#define _SD_SPI 0  // we are using PIC18F45K20 and this has one SPI module

#else

#define sd_init() SPI_Initialize()
#define sd_open() SPI_Initialize()  //SPI_Open() does not exist, thus change it to SPI_Initialize()
#define sd_tx(d) SPI_Exchange8bit(d)
#define sd_rx() SPI_Exchange8bit(0xFF)

there are no changes at other files.  main.c is as below, creates "test.txt" file and writes "Hello world! ..." sentence to this file.

#include "mcc_generated_files/mcc.h"
#include "ff.h"

FATFS FatFs; /* FatFs work area needed for each volume */
FIL Fil; /* File object needed for each open file */


/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
    
        
    UINT bw;

    if (f_mount(&FatFs, "", 1) == FR_OK) { /* Mount SD */

if (f_open(&Fil, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK) { /* Open or create a file */

if ((Fil.fsize != 0) && (f_lseek(&Fil, Fil.fsize) != FR_OK)) goto endSD; /* Jump to the end of the file */

f_write(&Fil, "Hello world! This is text message written to sd card\r\n", 54, &bw); /* Write data to the file */
           
endSD: f_close(&Fil); /* Close the file */
}
}


     

    while (1)
    {
        // Add your application code
    }
}




1 yorum:

  1. Learning Pic Programing With Xc8 Compiler: Sd Card Example >>>>> Download Now

    >>>>> Download Full

    Learning Pic Programing With Xc8 Compiler: Sd Card Example >>>>> Download LINK

    >>>>> Download Now

    Learning Pic Programing With Xc8 Compiler: Sd Card Example >>>>> Download Full

    >>>>> Download LINK LL

    YanıtlaSil