Forums MPLAB XC8 Tutorial Discussions How to use FatFs library in MPLAB XC8 without MPLAB Code Configurater? Reply To: How to use FatFs library in MPLAB XC8 without MPLAB Code Configurater?

#2526

Karalan
Participant

Yup I installed Peripheral Libraries and I have made following changes:

#define sd_init() OpenSPI()

#define sd_open() SPI1_Open()

#define sd_tx(d) Write_SPI(d)

#define sd_rx() Read_SPI()

and my main.c file is:

#include “newxc8_header.h”
#include “ff.h”

void main(void) {

ADCON1=0x0F;
OSCCON=0x76;
// Open the file “test.txt” and Write “Hello world!”
FATFS FatFs; // FatFs work area needed for each volume
FIL Fil; // File object needed for each open file
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
f_write(&Fil, “Hello world!\r\n”, 14, &bw); // Write data to test.txt file
}
}
}