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 support@ccsinfo.com

What am I doing wrong? - Connect SD card to PIC
Goto page 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

What am I doing wrong? - Connect SD card to PIC
PostPosted: Sat Mar 03, 2012 7:03 pm     Reply with quote

Hello

I've tried all the libraries in this forum to communicate with the SD card, but all without success Confused Sad .

What should I be doing wrong?

I now use a very simple code ... but the LED on pin A0 never lights up and I do not get anything for UART.

I have the impression that the code does not flow ... why?

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

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

#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>

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

   mk_file(filename);
   {
  printf("sucesso\n");
   }

output_high(PIN_A0);

   while(TRUE) ;
}




Thank you very much Wink



PS:
I forgot, the circuit is equal to this:

temtronic



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

View user's profile Send private message

PostPosted: Sat Mar 03, 2012 8:58 pm     Reply with quote

hardware error...

like dozens before you.....

5volt PIC....3volt SD card NOT compatible...
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: Sat Mar 03, 2012 11:17 pm     Reply with quote

You can find reference designs on my projects page for interfacing a 5 volt PIC with a 3.3 volt SD card.

With your current HW configuration there is a good chance the card has been damaged.
_________________
Regards, Andrew

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



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

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 4:45 am     Reply with quote

I have asked it before on this forum but I still don't get it, why do people want to mix 3.3V devices with a 5V PIC????

We have seen the bad mixture of 3.3V SD card and 5V PIC many times before, but here the design includes a 3.3V Bluetooth module as well... So, why making things difficult to yourself by including a processor with a different power supply? You need glue logic and a second power supply to get it working.

It is not because the PIC18F2550 is so special. This chip is quiet old, my datasheet was downloaded 7 years ago. Now there is a good 3.3V compatible processor that is also available in the same 28 pdip housing, uses less power, has more features (more RAM and a Real Time Clock) and is even cheaper to buy. The name of this chip differs in just 1 letter from the chip used here: PIC18F25j50.

My mantra: KISS = Keep It Simple, Stupid.
Often you have to put in a little more effort to find the simple solution, but in the end you save yourself a lot of troubles and money by keeping your design as simple as possible.
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 6:34 am     Reply with quote

Why the 'mismatch' is simple...the internet ! It instantly gives access to wrong information as fast as correct. Also, seem nobody has time to READ the data sheets,understanding what is said either in text or pictures.
There's an entire generation of hobbiest,students and, sorry to say, 'technicians' who have never actually burnt their fingers soldering, read a real data book,wirewrapped 50 ICs on a perfboard. You can design,'simulate'(hahaha),layout and have your project made offshore.In less than 10 days you can have the smt parted PCBs in your hands NEVER having actually seen any of the devices.
PICs are great little slabs of silicon,the CCS compiler is wonderful at allowing easy access to all of their neat features, it's just that some people have to read the books and get their own handson experience!

Thanks for pointing out the PIC age..yeesh..I bought PCM back in 1999...now I really feel old !!
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 8:13 am     Reply with quote

Why do you think I have the PIC powered +5V?

I fed the whole circuit at 3.3V. .. thus should not work well?

Please let us return again to the subject!

As for SD, it is working, I tested right now on my computer.

... So, what is wrong? ... Confused


Thanks for all the answers!
ckielstra



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

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 9:25 am     Reply with quote

Quote:
Why do you think I have the PIC powered +5V?
Maybe because it is in Figure 28-1 of the data sheet?
You are right, we should have asked instead of assuming. But, now you are running a PIC outside the specifications. What is worse?

The PIC18LF2550 (note the 'L') is specified for the lower voltages but only for a frequency up to about 20MHz @ 3.3V.
ckielstra



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

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 9:42 am     Reply with quote

Back to your original question:

There are several things in your program that can be improved.
1) The fuses for the clock frequency are missing. The call to setup_oscillator() should get you processor running at 4MHz, but for example the USB will most likely run at a wrong frequency.

2) You are setting the TRIS registers. Maybe you have good reasons to do so but it is against CCS compiler policy. In CCS it is normal practice to let the compiler handle the TRIS registers for you. If you want to override this setting you have to add #use fast_io or #use fixed_io in your code. See the manual for more details.

3) Does the LED light up when you remove the SD related code?

P.S. 1: when setting the TRIS registers yourself make sure to do it correct. Now you have the SDI pin configured as an output...
P.S. 2: always post your compiler version number.
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 9:58 am     Reply with quote

Sorry, but I do not understand.

I'm using PIC18LF2550, then I see Figure 28.2 and note that I am within specifications.

I'm not using the library 18LF2550.h, because I do not have the compiler (V-4068).

------------------------------------------------------------

I really liked your answer and I will make the following changes:

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

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

#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";
   
   //set_tris_a (0b0000000);
   //set_tris_b (0b00000000);
   //set_tris_c (0b10000000);
   //set_tris_e (0b0);
   
   setup_oscillator(OSC_4MHZ);
   
   fat_init();

   mk_file(filename);
   {
  printf("sucesso\n");
   }

output_high(PIN_A0);

   while(TRUE) ;
}




If I remove the code related to the SD card, I can do everything. I can do the LED lights without difficulty.


The problem is the library?

Actually it is not being used in the correct PIC!



Thanks
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 11:03 am     Reply with quote

grrreat...

now you're using fast_io() but not set_tris().....!!!!

you have 2 options
1) getrid of the fast_io()....statements..let the compiler handle the 'details'

OR

2) add the correct set_tris()..... statements for your project

1 or 2 MUST be done ...

sigh, poor compiler and PIC can't survive .
This IS covered in the onscreen HELP files that CCS supplies.Press F11 to run and read....


Your #include says 18F2550 so we( and the compiler) assume you're using the F version.The two PICs are not 100% the same...

You should have a comment in the program saying you're using the LF version of the PIC.

Very,very unlikely a 'library' problem as CCS is very diligent in cutting working code.

note: You should always use the 'errors' option when using the hardware UART ( #use rs232(.....) ).
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Mar 04, 2012 11:38 am     Reply with quote

OK, thanks.

I will then keep the set_tris () correctly.

However,'ve tried again and the LED lit, but the card still empty. Why?
miro



Joined: 15 Jan 2011
Posts: 62

View user's profile Send private message

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

The fat_init returns "-1" when the sdcard init finishes with error

/*
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.
*/

You may add following to see what happens:

signed int f;
...
f = fat_init();
if (f) printf("\r\n\nERROR INITIALIZING FAT f= %i \r\n\n",f);
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

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

Hum... OK.

Quote:
ERROR INITIALIZING FAT f= -1

Crying or Very sad Crying or Very sad


It may be that the card is not suitable for this?
miro



Joined: 15 Jan 2011
Posts: 62

View user's profile Send private message

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

Welcome in the club Razz
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

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

Now I changed a few pins on the bread board and I got:

Quote:
ERROR INITIALIZING FAT f = 0



PS: I used: printf("\r\n\nERROR INITIALIZING FAT f= %i \r\n\n",f);
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, 3  Next
Page 1 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