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

mmcsd and fat libs of CCS don't work?
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
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

mmcsd and fat libs of CCS don't work?
PostPosted: Thu Dec 15, 2011 3:32 pm     Reply with quote

Hello,

I have 1GB SD card and I'm trying to create a file, write into it etc.. using CCS libs but it doesn't work.

I also tried to make two bug fixes in this forum but I got errors in mk_file, fatopen, fatputc functions.
I tried both FAT32 and FAT16
I'm formatting the Card correctly.

My hardware is OK, I have 3.3V for the Card and pull-up resistors on DO(7) and CS(1) pins.
SD -- PIC
DO->SDI
SCLK->SCL
DI->SDO
CS->CS

Please someone help me to make it working.

Code:
#include <18F4520.h>
#fuses INTRC_IO,NOWDT,PROTECT,NOLVP,BROWNOUT, NOPUT, NODEBUG, NOSTVREN,NOCPD,BORV45
#device  *=16
#device PASS_STRINGS = IN_RAM
#ZERO_RAM

#use delay(internal=16M)

#include <stdlib.h> // for atoi32

#include <lcd.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>
#include <fat.c>

void main()
   {
   char filename[]="/utst.txt";
   char mode[] = "a";
   FILE stream;
   
   setup_oscillator(OSC_16MHZ);
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);   
   set_tris_a(0b11110110);
   set_tris_b(0b00000000);
   set_tris_c(0b10010000);
   set_tris_e(0b11111111);
   enable_interrupts(GLOBAL);

   lcd_init();
   delay_ms(500);

   while(fat_init());
   if(!fat_init())
      printf(lcd_putc,"\ffat inited");
   else
      printf(lcd_putc,"\ffat ERROR!");
   delay_ms(2000);

   if(!mk_file(filename))
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc,"file created");
   }
   else
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc,"file ERROR!");
   }
   delay_ms(2000);

   if(!fatopen(filename, mode, &stream))
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc,"file opened");
   }
   else
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc,"open ERROR!");
   }

   delay_ms(2000);
lcd_gotoxy(1,1);
   if(!fatputc('a', &stream))
      printf(lcd_putc,"file writed");
   else
      printf(lcd_putc,"write ERROR!");
   
   delay_ms(2000);
   lcd_gotoxy(1,1);
   if(!fatclose(&stream))
      printf(lcd_putc,"file closed");
   else
      printf(lcd_putc,"close ERROR");

   while(1); // END
}



+++++++++++++++++++++
No triple posting of same question.
Two other posts in Code Library removed.
This one stays.

-- Forum Moderator
+++++++++++++++++++++
temtronic



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

View user's profile Send private message

PostPosted: Thu Dec 15, 2011 3:57 pm     Reply with quote

Unless you have the proper level translator IC between the 5 volt PIC and the 3.6v SD card you're hardware is NOT correct !!
You cannot do this. There are several 'threads' about this subject and what you have to do.
Options are either use a 'low voltage' PIC ( 'L' series) or add the level translation IC.

There are a few software issues but FIRST you must get the correct hardware !
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Thu Dec 15, 2011 4:49 pm     Reply with quote

I have voltage divider resistors and 3v3 regulator. I only not sure about the pull-up resistors which I use. Some schemes I saw that its different, but some are using the same configuration and all claims that it works.

Even I don't put voltage dividers it will work good with just separated 3.3V supply voltage, the SD I/O pins are 5V compliant.

Please give me the correct drawing for CCS libs to be sure about the hardware.

Anyway I don't think that HW is wrong...

Other suggestions?
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Thu Dec 15, 2011 5:05 pm     Reply with quote

Problem is classic.
You need level translators between the SD card output lines, and the PIC inputs. The PIC using hardware SPI, _requires_ it's input signals to go to over 0.8* the supply voltage. So 4v. A 3.3v device cannot generate an output this high, so doesn't work.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Thu Dec 15, 2011 5:57 pm     Reply with quote

Why are so many people building MMC schematics with the PIC running at 5V and the MMC at 3.3V? It is much easier, cheaper and more reliable to have everything running at 3.3V.

Then to make the interface from 5V to 3.3V they use the cheap solution with resistor dividers. This limits maximum speed but should work. Plain stupid is the other way around from 3.3V to 5V where often no translation is implemented. Big surprise when it isn't working. Duhhh.....

Of course, the 3.3V LF-versions of the PIC are not always easy to buy, but that is only for the older PIC models. Many of the new PIC models run from 2V and higher. New PIC models are also cheaper than the old ones, so another reason for upgrading.

There is an alternative option for the 5V PIC problem: when it is just a hobby project and you don't care about specifications (there is no translation from 3.3V to 5V so you didn't care anyway), then take the 5V PIC and have it run at 3.3V. In most cases this will work because the LF-version is the same as the standard F-version but selected from a better part of the silicium die. Just take care to run at a lower maximum speed, say 16MHz (about 20MHz for the LF-version).
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 7:33 am     Reply with quote

My circuit is running already at 16Mhz.
Could you give me a correct drawing from 5V to 3.3V without level translator IC?
I'm not sure only about the pull-up resistors.

EDIT:
These does not have any pull-up resistors:
http://www.roland-riegel.de/sd-reader/
http://img441.imageshack.us/img441/2391/schematicuo7.jpg
http://www.mikroe.com/img/publication/dspic-books/programming-c-dspic/chapter/ch13/fig13-10.gif

This has pull-ups:
http://www.rlocman.ru/i/Image/2010/06/18/5.jpg

and many others have pull-ups on 1,2,7,5 pins..

Which is compatible for mmcsd lib?


Last edited by tesla80 on Fri Dec 16, 2011 7:54 am; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 7:48 am     Reply with quote

school's in...

First they are not 'pullup' resistors ! You require a 'voltage divider' network consisting of 2 resistors.

Basic, simple Electronics101 stuff..yawn..

One example, may or not work as I do not know the current requirements of the SD card...

IF output of PIC pin is 5.00 volts and the SD pin needs 3.3 volts.

PIC pin----/\/\/\/\/---*-----/\/\/\/\/\/\----gnd
r1 | r2
|___SD pin INPUT pin

if r2 is 3k3, then r1 will be 1k7.

Vsdpin=5.00*(3300/(1700+3300)).

NOTE: This ONLY works for data going FROM PIC to SD card, NOT from SD card to PIC !!! So this ONLY works ONE way, which really is useless in the real world.You can never read from the card.

The math is based upon several assumptions. PIC output will not be 5.00 volts( read the datasheet),current drive may not be 1.00 ma(again read the datasheet),Vin of the SD card is 3.3 volts(again, read the datasheets).

Bottom line...it'll never reliably work unless you use proper level translator ic or a L version of the PIC,though you might get lucky.

school's out.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 8:01 am     Reply with quote

Thanks for the re-teaching of the Ohm's Law.

Please look at newly added schematics, they all work without IC.
asmallri



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

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

PostPosted: Fri Dec 16, 2011 8:10 am     Reply with quote

tesla80 wrote:
Thanks for the re-teaching of the Ohm's Law.

Please look at newly added schematics, they all work without IC.


The first three implementation are operating outside of the manufacturers guaranteed performance envelop of the PIC. Just like overclocking a CPU. They may work today, they may work for 20 year and they may fail tomorrow. This type of implementation is fine for hobbyists. For an engineer to implement such a solution is either lazy (did not read the data sheet), illiterate (could not read the data sheet) or incompetent.
_________________
Regards, Andrew

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



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 8:52 am     Reply with quote

When I have pull-up resistor to 5V on the DO(7) pin of SD card, isn't it called a kind of level translation?

Here is my part of schematic:
http://imageshack.us/photo/my-images/62/sdsch.png/
I use LM1117-3.3V for 3.3V supply.
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 9:16 am     Reply with quote

school's back in...

No..it's a pullup resistor ! It puts +5volt on everything connected to it,in the 'idle' state. Which means it'll put +5 volts INTO the SD card, NOT good as it'll destroy the card or at the very least 'weaken' it so it will fail.

As we've already said several times you MUST use proper level translator IC. It will take the SD cards logic high of 3.2 volts and convert it to a PIC acceptable 4.8 volts. Going the 'other way' it will convert the PICs +5 outputs to +3.2 volts for the SD card.

Before you blow up anymore SD cards, just breadboard the resistors and see what voltages you get...compare to what is needed.

However there is a 3rd way to correctly interface the PIC and SD card without resistors or ICs. You could take the SD card output and connect to either an analog input or comparator input. You'ld need to create your own driver but it could work, at reduced speed.

I haven't got the time or desire to do that for you, so I'll leave it as a homework assignment.

school's out...
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 9:46 am     Reply with quote

Thanks, you said everything useless except are my pull-ups correct...

Why this person put 2 pull-up resistors on CS and DO of SD card?
http://www.picprojects.net/sdcard_demo/sdcard_module.png

Would you explain this to me?
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 10:02 am     Reply with quote

recess over, school's back in...

topic...Pullups.

question....Why?

discussion....Traditionally pullup resistors are used to put a device's pins into a KNOWN default state of 'one' or logic level of 'high'.In this case,it appears the SD card needs to have CS pulled high when 'idle' or not in use.

Please do NOT confuse this with 'logic level translation'. That is the conversion of say 3.3 volt logic level to 5 volt logic levels,RS232 +12 to ttl +5,etc.

Think of 'pullups' as being the same as 'caps lock',while logic level conversion(translators) the same as Google's online tranlator from english to french.

'stop' capslocked is 'STOP' but a Frenchman can't understand it....

'stop' translated is 'arret' and NOW the Frenchman CAN understand it.

BTW NO amount of resistors in ANY configuration will 'translate' 3.3v SD card (3.3v vcc) logic levels into 5 volt logic levels.Cannot be done and I'l bet my truck on that.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 12:51 pm     Reply with quote

Thanks for your really elementary level lectures but actually not needed.

Could you say me what to change on my schematic or how to debug the code? Anyway you are sure that code is working good (I think), aren't you?

Maybe a transistor between DO pin to MISO pin (PIC) to act like level shifter?

If I connect DO pin directly to the PIC, PIC will not accept SPI signals from the card, right?

I've tried another library from this forum and it enters into infinite loop at FindDirEntry function when I call fopen function... Maybe this happens because PIC cannot read from the Card?

Best wishes
asmallri



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

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

PostPosted: Fri Dec 16, 2011 1:04 pm     Reply with quote

If you connected the DO output to +5v then you may have killed the card. In this case you will not receive anything.

Check out the projects page on my site, you will find reference examples for implementing SC/MMC cards with 5 volt PICs.

The reason there were two pullup resistors is because the designer wanted to ensure the SD/MMC card was deselected if the output driving CS from the PIC was floating such is the case when a PIC is reset. The specific version of IC used in this example is important as not all versions of this chip are tolerant to +5 volt inputs including some of the implementations of the SD/MMC PICTail from Microchip.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
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