Hello Everyone,
I made a post here before about SD Card and PIC integration and received a lot of good advice, so I am posting again. I am using the CCS C compiler, and I am using the fat.h and mmcsd.c libraries to write the following code:
I've already formatted the 2GB SD Card, and I am thinking that this should create a file named test.txt on the card for me to view in PC. However, it's not working, and I just can't see why. Anybody have any experience with this? Thanks!
I made a post here before about SD Card and PIC integration and received a lot of good advice, so I am posting again. I am using the CCS C compiler, and I am using the fat.h and mmcsd.c libraries to write the following code:
Rich (BB code):
#include <18F4550.h>
#device PASS_STRINGS = IN_RAM
#fuses NOWDT, HS, NOPROTECT
#use delay(clock=25000000)
#use rs232(baud=2400, parity=N, invert, brgh1ok, xmit=PIN_D0, stream=LCD, FORCE_SW)
#use rs232(baud=2400, parity=N, brgh1ok, rcv=PIN_C7, stream=RFID)
#include <stdlib.h> // for atoi32
//media library, a compatable media library is required for FAT.
#use fast_io(c)
#define MMCSD_PIN_SCL PIN_B1 //o
#define MMCSD_PIN_SDI PIN_B0 //i
#define MMCSD_PIN_SDO PIN_C7 //o
#define MMCSD_PIN_SELECT PIN_A0 //o
#include <mmcsd.c>
//FAT library.
#include <fat.h>
/*
Summary: Creates a file.
Param: The full path of the file to create.
Returns: None.
Example Usage: \> make "Log.txt"
*/
void MakeFile(char *fileName)
{
fprintf(LCD, "\r\nMaking file '%s': ", fileName);
if(mk_file(fileName) != GOODEC)
{
printf("Error creating file");
return;
}
fprintf(LCD, "OK");
}
/*
Summary: Formats the media to a specified size.
Param: The size of the media, in kB, in string form.
Returns: None.
Example Usage: /> format 524288 (format a 512MB card)
*/
void FormatMedia(char *mediaSize)
{
int32 size;
size = atoi32(mediaSize);
printf("\r\nFormatting media (size=%LU): ", size);
if(format(size) != GOODEC)
{
printf("Error formatting media");
return;
}
printf("OK");
}
/*
Summary: Prints either all of or the last 80 characters in a file.
Param: The full path of the file to print off.
Param: If true, this function will print off the last 80 characters in the file.
If false, this funciton will print off the entire file.
Returns: None.
Example Usage: /> cat "Logs.txt" (this will display the entire file)
Example Usage: /> tail "Logs.txt" (this will display the last 80 characters in the file)
*/
void PrintFile(char *fileName, int1 startFromEnd)
{
FILE stream;
if(fatopen(fileName, "r", &stream) != GOODEC)
{
printf("\r\nError opening file");
return;
}
printf("\r\n");
if(startFromEnd)
fatseek(&stream, 80, SEEK_END);
fatprintf(&stream);
fatclose(&stream);
}
/*
Summary: Append a string to a file.
Param: The full path of the file to append to.
Param: A pointer to a string to append to the file.
Returns: None.
Example Usage: \> append "Log.txt" "This will be appended to the end of Log.txt"
Note: A "\r\n" will be appended after the appendString.
*/
void AppendFile(char *fileName, char *appendString)
{
FILE stream;
printf("\r\nAppending '%s' to '%s': ", appendString, fileName);
if(fatopen(fileName, "a", &stream) != GOODEC)
{
printf("Error opening file");
return;
}
fatputs(appendString, &stream);
fatputs("\r\n", &stream);
if(fatclose(&stream) != GOODEC)
{
printf("Error closing file");
return;
}
printf("OK");
}
/*
Summary: Deletes a file.
Param: The full path of the file to delete.
Returns: None.
*/
void DeleteFile(char *fileName)
{
printf("\r\nDeleting '%s': ", fileName);
if(rm_file(fileName) != GOODEC)
{
printf("Error deleting file");
return;
}
printf("OK");
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void main()
{
char opt_buffer[255];
strcpy(opt_buffer, "test.txt");
fat_init();
MakeFile(opt_buffer);
AppendFile(opt_buffer)
}
I've already formatted the 2GB SD Card, and I am thinking that this should create a file named test.txt on the card for me to view in PC. However, it's not working, and I just can't see why. Anybody have any experience with this? Thanks!
Last edited by a moderator: