CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problems with ex.fat32.c and PIC18F4550

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tong
Guest







Problems with ex.fat32.c and PIC18F4550
PostPosted: Mon Nov 19, 2007 2:37 am     Reply with quote

hi.
I am using 18F4550, and cccs lib ex.fat.c is not working
Now has 2 questions:
1) KINGSTON 1GB SD CARD fat_init the SD Error (SD CARD is OK)
2)be unaware of brand 256MB SD CARD fat_ini the SD ok,but not read the BPB data
ccs compiler version 4.056
I can't understand, why fat_init is Error.....
code:

//These settings are for the CCS PICEEC development kit which contains
//an MMC/SD connector.
#include <18F4550.h>
#device PASS_STRINGS = IN_RAM
//#fuses NOWDT, HS, NOPROTECT
//#use delay(clock=8M)

#DEVICE *=16
#fuses hs,NOWDT,PROTECT,NOLVP,NODEBUG,MCLR,NOVREGEN
#use delay (clock=8000000)

//#use rs232(baud=9600, UART1, errors)
#use rs232(baud=9600,parity=N, xmit=PIN_A3,rcv=PIN_A2,bits=8) //xmit=PIN_C1,rcv=PIN_C0 stream=SW_RS232

#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_B3 //o
#include <mmcsd.c>

//FAT library.
#include <fat.c>

//////////////////////
/// ///
/// Useful Defines ///
/// ///
//////////////////////

#define COMMAND_SIZE 10
#define NUM_COMMANDS 11

void main(void)
{
char buffer[255];
char opt_buffer[255];
char *cmd, *option1, *option2;

int i; // pointer to the buffer

// initialize the FAT
// keep in mind that this will automagically initialize the media
i = fat_init();
if (i)
printf("\r\n\nERROR INITIALIZING FAT\r\n\n");
else printf("\r\n\INITIALIZING FAT OK!\r\n\n");
while(1){;}
//***************************
//FAT library........
// initialize code
/*
signed int fat_init()
Summary: Initializes global variables that are essential for this library working
Returns: EOF if there was a problem with the media, GOODEC if everything went okay.
Note: This must be called before any other function calls in this library.
*/
signed int fat_init()
{
int ec = 0;

int
FATs,
Sectors_Per_Cluster;

int16
Bytes_Per_Sector,
Reserved_Sectors,
Small_Sectors;

int32
Hidden_Sectors,
Large_Sectors;

#ifdef FAT32
int32 Sectors_Per_FAT;
#else // FAT16
int16
Root_Entries,
Sectors_Per_FAT;
#endif // #ifdef FAT32

// initialize the media
ec += mmcsd_init();

// start filling up variables
ec += mmcsd_read_data(11, 2, &Bytes_Per_Sector);
ec += mmcsd_read_data(13, 1, &Sectors_Per_Cluster);
ec += mmcsd_read_data(14, 2, &Reserved_Sectors);
ec += mmcsd_read_data(16, 1, &FATs);
#ifdef FAT16
ec += mmcsd_read_data(17, 2, &Root_Entries);
#endif // #ifdef FAT16
ec += mmcsd_read_data(19, 2, &Small_Sectors);
#ifdef FAT32
ec += mmcsd_read_data(36, 4, &Sectors_Per_FAT);
#else // FAT16
ec += mmcsd_read_data(22, 2, &Sectors_Per_FAT);
#endif // #ifdef FAT32
ec += mmcsd_read_data(28, 4, &Hidden_Sectors);
ec += mmcsd_read_data(32, 4, &Large_Sectors);
#ifdef FAT16
Next_Free_Clust = 2;
#else
ec += mmcsd_read_data(0x3EC, 4, &Next_Free_Clust);
#endif
if(ec != GOODEC)
return EOF;

// figure out the size of a cluster
Bytes_Per_Cluster = Sectors_Per_Cluster * Bytes_Per_Sector;

// figure out how long one FAT is
FAT_Length = Sectors_Per_FAT * (int32)Bytes_Per_Sector;

// figure out where the FAT starts
FAT_Start = Reserved_Sectors * Bytes_Per_Sector;

// figure out where the root directory starts
Root_Dir = FAT_Start + (FATs * FAT_Length);

// figure out where data for files in the root directory starts
#ifdef FAT32
Data_Start = Bytes_Per_Cluster + Root_Dir;
#else // FAT16
Data_Start = (Root_Entries * 0x20) + (Bytes_Per_Sector - 1);
Data_Start /= Bytes_Per_Sector;
Data_Start += Reserved_Sectors + (FATs * Sectors_Per_FAT);
Data_Start *= Bytes_Per_Sector;
#endif // #ifdef FAT32

printf("\r\n Data_Start=%4lu ", Data_Start); //not read the data=0
printf("\r\n FAT_Start =%4lu ", FAT_Start); //not read the data=0
printf("\r\n Root_Dir =%4lu", Root_Dir); // not read the data=0


return GOODEC;
}

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group