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

What am I doing wrong? - Connect SD card to PIC
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 12:48 pm     Reply with quote

Code:
#include <18F2550.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOLVP, NOMCLR
#use delay (clock=4M)

#use rs232(baud=4800, XMIT=PIN_C6, rcv=PIN_C7, BITS=8, STOP=1, parity=N, ERRORS)

#define MMCSD_PIN_SCL      PIN_B1
#define MMCSD_PIN_SDI      PIN_B0
#define MMCSD_PIN_SDO      PIN_C7
#define MMCSD_PIN_SELECT   PIN_A5

#include <mmcsd.c>
#include <fat.c>

   #use fast_io(a)
   #use fast_io(b)
   #use fast_io(c)
   #use fast_io(e)

void main(void)
{
   char filename[]="teste.txt";
   
   signed int f;
   
   set_tris_a (0b0000000);
   set_tris_b (0b00000001);
   set_tris_c (0b00000000);
   set_tris_e (0b0);
   
   setup_oscillator(OSC_4MHZ);

   fat_init();

f = fat_init();

 if(f)
   printf("\r\n\n Error\r\n\n");
 else
      printf("SD card successfully started!!!\r\n\r\n");
 
   
   mk_file(filename);
   {
   printf("sucesso cartao\n");
   }
   
output_high(PIN_A0);

   while(TRUE);
   {
     printf("sucesso fim\n");
     delay_ms(1000);
   }
}




In the UART terminal appears: SD card successfully started!

The SD card is still empty.
miro



Joined: 15 Jan 2011
Posts: 62

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 1:02 pm     Reply with quote

.. remove the first fat_init();
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 1:13 pm     Reply with quote

Have removed the first "fat_init ()".

The program can access the card, but still does not record anything.
miro



Joined: 15 Jan 2011
Posts: 62

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 1:15 pm     Reply with quote

..you may try the "ex_fat.c" demo. It includes a shell so u can provide various operations with the card..
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 4:19 pm     Reply with quote

I followed your advice and I'm using the following program:

Code:
#include <18F2550.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOLVP, NOMCLR
#use delay (clock=4M)

#use rs232(baud=4800, XMIT=PIN_C6, rcv=PIN_C7, BITS=8, STOP=1, parity=N, ERRORS)

#define MMCSD_PIN_SCL      PIN_B1
#define MMCSD_PIN_SDI      PIN_B0
#define MMCSD_PIN_SDO      PIN_C7
#define MMCSD_PIN_SELECT   PIN_A5

#include <mmcsd.c>
#include <fat.c>

 #use fast_io(a)
 #use fast_io(b)
 #use fast_io(c)
 #use fast_io(e)

void main(void)
{
   char filename[]="teste.txt";
   signed int f;
   
   set_tris_a (0b0000000);
   set_tris_b (0b00000001);
   set_tris_c (0b00000000);
   set_tris_e (0b0);
   
   setup_oscillator(OSC_4MHZ);


delay_ms(5000);

f = fat_init();

 if(f)
   printf("\r\n\n Error\r\n\n");
 else
      printf("SD card successfully started!!!\r\n\r\n");
 
   
   printf("\r\nMaking file '%s': ", filename);
   
   
   if(mk_file(filename) != GOODEC)
   {
      printf("Error creating file");
   }
   else printf("OK");
   
   
   
   output_high(PIN_A0);
   
}



For UART appears written:

Quote:
SD card successfully started!!!


Making file 'teste.txt': OK


But the SD card still empty
Crying or Very sad Crying or Very sad
miro



Joined: 15 Jan 2011
Posts: 62

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 5:19 pm     Reply with quote

Try this:

printf("\r\n\n**** CSD *****\r\n\n");
mmcsd_print_csd();
printf("\r\n\n**** CID *****\r\n\n");
mmcsd_print_cid();

It shall print some SDcard parameters.

And here are some patches, basically from this forum:
http://www.picprojects.net/sdcard_demo/index.html

I cannot get the stuff working as well. The only card which at least initiates is a 512MB SD card, the bigger cards show init error.
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 5:48 pm     Reply with quote

I just do it. With the 64MB card that was testing not get anything, but with 4GB card, a list with all zero!

I do not understand why this is happening ...
asmallri



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

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

PostPosted: Sun Mar 04, 2012 6:48 pm     Reply with quote

Dave_25152 wrote:
I just do it. With the 64MB card that was testing not get anything, but with 4GB card, a list with all zero!

I do not understand why this is happening ...


64M card will be formatted standard SD format formatted to FAT12 or FAT16 and, seeing as 64M card are VERY OLD, they will be based on version 1 of the SD physical layer specification.

4G card will be SDHC SD format formatted to FAT32 and will be most likely be based on version 2 of the specification. If this is an very old 4G card then it may predate the version 2 specification and you may not be able to initialize the card (extremely unlikely)

If it your intention to be able to support both types of media you are testing with then your driver needs to support both standard SD and SDHC media as well as FAT16 and FAT32.
_________________
Regards, Andrew

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



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Mon Mar 05, 2012 9:02 am     Reply with quote

I'll have to spend too much time to solve it.

I've tried 3 SD cards and could not write files.
ckielstra



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

View user's profile Send private message

PostPosted: Mon Mar 05, 2012 9:54 am     Reply with quote

Dave_25152 wrote:
I'll have to spend too much time to solve it.
No one said it was going to be easy. One problem is that there are no good free SD card drivers for the CCS compiler. I'm surprised nobody picked up this issue but I guess there is no-one on this forum willing to put in their spare time. Just like me.

Quote:
Sorry, but I do not understand.

I'm using PIC18LF2550, then I see Figure 28.2 and note that I am within specifications.
If you are using the 'LF' version then your 3.3voltage should be ok, but this is the first time you mention the 'LF'. In your schematic and source code you have the 'F' version. As they say: 'The devil is in the details'.
With computers, and electronics, you have to make 200% sure you have every detail correct.
Even in your tiny program there are several issues that make me worry. Perhaps correct, but not how I like to write my code. Most worrying is that you have the #use fast_io directives after the including of the two driver files. In the CCS compiler many of the #something directives are location dependent, meaning that they only will have effect from that point in the source onwards. Please move these commands a few lines up.


Quote:
I'm not using the library 18LF2550.h, because I do not have the compiler (V-4068).
CCS has no header files for the 'LF' versions because they are exactly the same as the 'F' versions. Most likely both chip types are exactly the same but in the production process some tests are done to give the best quality parts an 'LF' mark.
Note that v4.068 is very old and not the best release. The early v4.0xx compiler versions were terrible quality and only became more or less usable around v4.075.

Quote:
I've tried 3 SD cards and could not write files.
What are the details of these cards?
The CCS driver, and many others, don't support the SDHC cards, this means you can only use cards up to 2Gb.

Code:
#use rs232(baud=4800, XMIT=PIN_C6, rcv=PIN_C7, BITS=8, STOP=1, parity=N, ERRORS)

#define MMCSD_PIN_SCL      PIN_B1
#define MMCSD_PIN_SDI      PIN_B0
#define MMCSD_PIN_SDO      PIN_C7
#define MMCSD_PIN_SELECT   PIN_A5
You are using pin C7 for both RS-232 and SPI... One pin an input, the other an output. Something tells me this is not going to work.
A stupid design decision from Microchip.
Other people have dealt with this problem, but I don't remember the solution right now. Searching the internet should give the answer.

After writing the file with the PIC, how did you test the file to exist?

O yes, don't use the format function from the CCS library. It is missing a feature and that's one of the reasons people can't read files on the PC. Repairing such a damaged SD card requires special software for overwriting the first sector, a standard format on the PC won't do it.
temtronic



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

View user's profile Send private message

PostPosted: Mon Mar 05, 2012 10:00 am     Reply with quote

Tired of beating your head against the wall! Time to work smarter, not harder.BTDT.

If you want to spend $25 for a readymade ,easy, yes it works solution...buy a Vinculum based USB flashdrive 'datalogger'.
Works with all 5 volt PICS ! Simple, serial communications( looks like a UART).100% compatible with any PC software that can read USB drives.

EVERY PC comes with USB, so data transfer is easy. Heck, save data in CSV format and Excel will grab it all in a flash( no pun intended).

Yes, it's $25. Wow, woopee,yes I'm being sarcastic but that $25 is peanuts compared to R&D costs of $100/hr.How many dayze did YOU waste? Life is shorter than you think.

No, I have nothing to gain buy you buying Vinculum products( wish I did).
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 2:56 pm     Reply with quote

Hello

I'll wait until Friday to go home and there I have other SD cards. The card I have here is 4GB SDHC.

I'll search for the solutions presented by temtronic. I was very interested.

Thank you;)
asmallri



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

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

PostPosted: Wed Mar 07, 2012 6:10 pm     Reply with quote

ckielstra wrote:

O yes, don't use the format function from the CCS library. It is missing a feature and that's one of the reasons people can't read files on the PC. Repairing such a damaged SD card requires special software for overwriting the first sector, a standard format on the PC won't do it.


Formatting with a digital camera will address this problem. A lot of people developing their own drivers accidentally corrupt the boot sector or MBR and then waste days or weeks trying to get their code to work without realizing it cannot even though they have regularly reformatted the card on a window's PC.
_________________
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: Tue May 01, 2012 3:27 am     Reply with quote

I have similar problem. I`m using 2G micro SD card with adapter to SD. So I tried the program from above:
Code:

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

#use delay(clock=25000000)

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

#define MMCSD_PIN_SELECT   PIN_C2
#define MMCSD_PIN_SCL      PIN_C3
#define MMCSD_PIN_SDI      PIN_C4
#define MMCSD_PIN_SDO      PIN_C5 

#include <mmcsd.c>
#include <fat.c>

#use fast_io(c)

void main()
{
   char filename[]="teste.txt";
   signed int f;
   delay_ms(500);
   initialLCD();
   writerow(0x80,5);
   delay_ms(500);
   f=fat_init();
   if(f)
   {
      printf("ERROR");
   }
   else
   {
      printf("OK");
   }
   mk_file(filename);
   {
      printf("file created");
   }
}

So I have "OK" and "FILE created" on the screen, but in the card there is nothing. I formated the card in FAT32. Do you have any suggestions???
temtronic



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

View user's profile Send private message

PostPosted: Tue May 01, 2012 4:50 am     Reply with quote

split the program up....
1) using a hex editor on a PC, create a file on the SD cad with KNOWN data. 'testfile.txt' which contains '1 2 3 4 5 a b c d e' or something YOU know.

2) create a PIC program that will read and display the data either to a local LCD or send to the PC

3) once that is working then cut code on the PIC to create a 2nd file with known data. 'PICfile.txt', containing 'Hello World it works' or something but NOT the same data as the 1st file.

however be sure to have the proper hardware! An 18F PIC will NEVER work 100% connected directly to an SD card.5!=3 !!!
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 Previous  1, 2, 3  Next
Page 2 of 3

 
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