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

SD card not working
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

SD card not working
PostPosted: Wed May 16, 2012 5:32 am     Reply with quote

Hi! I`m trying to command sd with pic18F66J60, but I have a small problem.
It`s not working.
I have made the next connections :
PIN 1 of the card to PIN_C2(controller). 20k pull-up resistor
PIN2 to PIN_C5. 20k pull-up resistor
PIN3 and 6 to GRD
PIN4 to Vcc 3.3V(3.26V exact)
PIN5 to PIN_C3. 20k pull-up resistor
PIN7 to PIN_C4. 20k pull-up
PIN8 and 9 through 20k resistor to Vcc.

Here is my program code:
Code:

#include <18F66J60.h>
#fuses HS,NOWDT

#use delay(clock=25000000)

#use rs232(baud=9600, XMIT=PIN_C6, BITS=8, STOP=1, parity=N)

#include <stdlib.h>
#define MMCSD_PIN_SELECT   PIN_C2
#define MMCSD_PIN_SCL      PIN_C3
#define MMCSD_PIN_SDI      PIN_C4
#define MMCSD_PIN_SDO      PIN_C5 

#use fast_io(c)
#include <mmcsd.c>
#include <fat.c>

void main()
{
   char filename[]="teste.txt";
   int f;
   f=fat_init();

   mk_file(filename) ;
   
}

The card is FAT32 formated. After this program I don`t have anything in the card.
Where is the problem??
Thanks![/url]
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed May 16, 2012 5:53 am     Reply with quote

You CANNOT, repeat CANNOT interface ANY SD card to a 5 volt PIC with just resistors!!
As you have found out it DOESN'T work !!

You have 3 options,well 4 really

1) Use an 'L' version PIC (Low voltage)

2) Use logic level interface IC between the 5 volt PIC and 3 volt SD chip

3) Continue banging you head against the wall, thinking it's bad software when really it's the WRONG hardware setup.

Please 'search' this forum you'll get a LOT of hits of others having the SAME problem, you're not the first and sadly won't be the last.

If you're not commited to SD/MMC cards you could use the FTDI Vinculum/Flasdrive solution and be up and running in an hour.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed May 16, 2012 6:54 am     Reply with quote

Who a sec. The 66J60, is a low voltage PIC, and should work. How is it set up, and supplied?. How is the ENVREG line connected?.
It needs to be running with Vdd to the 3.3v line, and with ENVREG also tied to Vdd. It's I/O specs are then compatible with the SD card. You also must have the smoothing cap on the Vddcore line.
On this chip the internal 'core' runs at 2.5v (2 to 2.7v max), and the I/O logic at the Vdd voltage (3.7v max). It either has to have the internal voltage regulator enabled (ENVREG tied to Vdd), or requires a separate 2.5v supply to Vddcore.
Have you actually verified that the chip can toggle an I/O pin at a specific frequency, and is running OK?.

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Wed May 16, 2012 7:35 am     Reply with quote

I have a 10uF electrolite cap on Vddcore. ENVREG is connected to Vcc. The controller is working. I have LCD connected to it and it`s working fine. But when I try with the card. I think it returns error on the card init.
Is there something specific for card formating?
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed May 16, 2012 8:10 am     Reply with quote

OK. We can now rule out the commonest problem, which was where Temtronic was 'coming from'. Smile

Several things on from here. Have you made the setting in fat.c, to use fat32?. It should be the default, but check (#define FAT32, rather than FAT16).

Yes, the library as standard, only works with a card that has the 'drive' starting directly after the MBR. Standard way to ensure this is to format it in something like a camera, or format it with the PIC itself. Several 'editor' type programs also allow you to format a card this way, and they should be like this 'out of the box', but unfortunately once this is changed, Windows will keep the changes if you reformat the card. This is a very common problem.
There have been patches posted here to fix this, a search should find them.

Is the card you have an SD card, or an SDHC card?. The drivers are designed for the former.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed May 16, 2012 3:45 pm     Reply with quote

stoyanoff,
I recommend you to do some reading in the fat.c and mmcsd.c driver files.

mk_file() is using mmcsd_write_data() for actual writing the data to the card. This function is documented:
Quote:
//// mmcsd_write_data(a, n, p) ////
//// Writes n bytes of data from pointer p to the MMC/SD card ////
//// starting at address a. This function intelligently manages ////
//// a write buffer, therefore if you may need to call ////
//// mmcsd_flush_buffer() to flush any buffered characters. ////
//// returns 0 if OK, non-zero if error. ////


and flushbuffer():
Quote:
//// mmcsd_write_data(a, n, p) ////
//// Writes n bytes of data from pointer p to the MMC/SD card ////
//// starting at address a. This function intelligently manages ////
//// a write buffer, therefore if you may need to call ////
//// mmcsd_flush_buffer() to flush any buffered characters. ////
//// returns 0 if OK, non-zero if error. ////


I haven't tested it, but with the above information your main should be modified to something like this:
Code:
void main()
{
   char filename[]="teste.txt";
   int f;
   f=fat_init();

   mk_file(filename);
   mmcsd_flush_buffer();
   
   while (1) ;    // Loop forever to prevent the PIC from entering sleep
}
Note the added forever loop at the program end to prevent the processor from entering sleep mode.
If you still have problems I suggest you to add code for testing the result codes from the driver functions.

And as mentioned before, the CCS drivers don't support SDHC cards, limiting the cards to types less than 4Gb.
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu May 17, 2012 2:31 am     Reply with quote

My micro SD card is 2G. I'm using adapter for SD card. The card is formatted in FAT32 512 bytes. In fat.c #define FAT32 is active. I tried with mmcsd_flush_buffer(); no success.

I'm almost certain the fat_init() doesn't initialize the card. What must be the returned result if the card is initialized 0 or 1???
Any other ideas?

One more thing! Should I setup spi interface???
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Thu May 17, 2012 4:27 am     Reply with quote

No, If you look at MMCSD.c, it initialises the SPI for you, using the #DEFINE pin numbers.

You certainly do need to prevent the code dropping off the end (as shown by Ckielstra). Because I/O is buffered, it'll still be proceeding when your code reaches it's end.

What does 'f' return from fat_init?. It should be 'GOODEC' (0) if the card has been seen, and is ready to be used.

Obvious comment, you have got the write protect switch 'off'...

As already mentioned, the CCS drivers will only work with a card that has the actual filesystem immediately after the MBR. This might well be your problem. Get the SD Formatter (https://www.sdcard.org/downloads/formatter_3/), and format the card with this - it creates the right format, since a lot of things like cameras also require the cards to be formatted this way - Windows doesn't.....

Best Wishes
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu May 17, 2012 5:54 am     Reply with quote

I formatted my card with this program. When I use a single instruction fat_init() it returns 1(sd not initialized). So I put it in a cycle
Code:

while(fat_init()!=GOODEC);

So this way it pass through this cycle. But again when it reaches the code for creating a file(or deleting) nothing happenes. mk_file(filename) returns 1. So I tried to put it in a cycle too:
Code:

while(mk_file()!=GOODEC)

but this time it doesn`t pass. I tried with mk_dir,rm_file.... the same effect...
The write protection of the card is OFF. I double checked!
HELP!
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri May 18, 2012 4:04 am     Reply with quote

CAUTION SHAMELESS PLUG FOLLOWS:

I do not know what is wrong with your implementation however, if you are sick of bashing your head against the wall. Check out my file system driver for the CCS compiler. It dynamically supports FAT16 and FAT32, dynamically supports standard and high capacity media, has no restriction on where the "drive" starts after the master boot record and comes with two operational sample applications demonstrating how to use the driver and how to add a DOS command line interface.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Thu May 24, 2012 11:00 am     Reply with quote

Ok! I tried with the examples and I have the same problem again. fat_init() or mmc_init() returns 1. I don`t think the problem is in the program but in sd card. It`s 2G micro SD card ADATA...
barton82



Joined: 18 Dec 2006
Posts: 1

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 8:57 am     Reply with quote

i do make a program like this
Code:
//These settings are for the CCS PICEEC development kit which contains
//an MMC/SD connector.
#include <18F4520.h>
#device PASS_STRINGS = IN_RAM
#fuses NOWDT, HS, NOPROTECT
#use delay(clock=20M)
#define use_portb_lcd TRUE //reserve portb for lcd
#include <LCD.C>
#use rs232(baud=9600, UART1, errors)

#include <stdlib.h> // for atoi32

//media library, a compatable media library is required for FAT.
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_C3 //o
#define MMCSD_PIN_SDI     PIN_C4 //i
#define MMCSD_PIN_SDO     PIN_C5 //o
#define MMCSD_PIN_SELECT  PIN_C2 //o
#include <mmcsd.c>

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

//////////////////////
///                ///
/// Useful Defines ///
///                ///
//////////////////////
#byte PORTE=0xf84
#define COMMAND_SIZE 10
#define NUM_COMMANDS 11

void MakeFile(char *fileName);
void AppendFile(char *fileName, char *appendString);
void DeleteFile(char *fileName);
void DisplayDirectory(char *dir);

void main(void)
{  char buffer[99];
   char opt_buffer[99];
   int32 j,i;
int r = 0; //0:ok !0:error

   //char trial[5]="hello";

   char *cmd, *option1, *option2;
   set_tris_e(0);

   delay_ms(200);
   i = fat_init();
   delay_ms(10000);
   lcd_init();
   delay_ms(200);
   if (i)
      printf(lcd_putc,"ERROR INITIALIZING FAT\r\n\n");
   delay_ms(500);

   lcd_putc("\n");
   printf(lcd_putc,"Ready!");
   delay_ms(1000);
   option2="2";

   opt_buffer="/200.txt";
   lcd_putc("\f");
   //MakeFile(opt_buffer);
   delay_ms(2000);
   opt_buffer="/100.txt";
   lcd_putc("\f");
   DeleteFile(opt_buffer);
   delay_ms(2000);
   lcd_putc("\f");
   //lcd_gotoxy(1,1);
   //AppendFile(trial,option2);
   do
   {PORTE=255;
   delay_ms(100);
   PORTE=0;
   delay_ms(100);
}while(1);

}

/*
Summary: Creates a file.
Param: The full path of the file to create.
Returns: None.
Example Usage: \> make "Log.txt"
*/
void MakeFile(char *fileName)
{
    printf(lcd_putc,"file '%s': ", fileName);
   if(mk_file(fileName) != GOODEC)
   {  lcd_putc("\n");
      printf(lcd_putc,"Error creating file");
  //    return;
   }
   lcd_putc("\n");
   printf(lcd_putc,"OK");
}


/*
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(lcd_putc,"Appending '%s' to '%s': ", appendString, fileName);
   if(fatopen(fileName,"a", &stream) != GOODEC)
   {
      printf(lcd_putc,"Error opening file");
      return;
   }
   
   fatputs(appendString, &stream);
   fatputs("\r\n", &stream);

   if(fatclose(&stream) != GOODEC)
   {
      printf(lcd_putc,"Error closing file");
      return;
   }
   lcd_putc("\n");
   printf(lcd_putc,"OK");
}


/*
Summary: Deletes a file.
Param: The full path of the file to delete.
Returns: None.
*/
void DeleteFile(char *fileName)
{
   printf(lcd_putc,"Deleting '%s': ", fileName);
   if(rm_file(fileName) != GOODEC)
   {
      lcd_putc("\n");
      printf(lcd_putc,"Error deleting file");
      return;
   }
   lcd_putc("\n");
   printf(lcd_putc,"OK");
}

/*
Summary: Display the contents of the working directory.
Param: The full path of the directory contents to display.
Returns: None.
Example Usage: /> dir
*/
void DisplayDirectory(char *dir)
{
   disp_folder_contents(dir);
}


to connect to SD card, i use 2 resistor to divide the voltage, the measure V is 3.3V, then use 1NPN and 1PNP to level 3.3V to 5V for DO from sd card to PIC, but the program will stop response during make new file, even in proteus happen same. anyone have idea?
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 9:54 am     Reply with quote

stoyanoff wrote:
Ok! I tried with the examples and I have the same problem again. fat_init() or mmc_init() returns 1. I don`t think the problem is in the program but in sd card. It`s 2G micro SD card ADATA...


Is it SDHC? (that's an important and helpful thing to know)

2GB is on the line of where makers made SDHC and non-SDHC cards available.

I have 2GB SD's that are NOT...and some that ARE.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

PostPosted: Sat Jul 21, 2012 10:26 am     Reply with quote

Hi, again! I fixed the problem with the initialisation. It works fine now, but still I can`t use the card. I suppose the problem is in the formating. I`m with win 7 x64. Can you offer me a software for formatting micro SD cards. I`m not sure my win formats the card in the correct format(FAT32).
Thanks!
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sat Jul 21, 2012 12:24 pm     Reply with quote

stoyanoff wrote:
Hi, again! I fixed the problem with the initialisation. It works fine now, but still I can`t use the card. I suppose the problem is in the formating. I`m with win 7 x64. Can you offer me a software for formatting micro SD cards. I`m not sure my win formats the card in the correct format(FAT32).
Thanks!


If you can't use the card, then it's not working fine.

You should be able to format for FAT32 or FAT (which is FAT16) under windows just fine.
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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