View previous topic :: View next topic |
Author |
Message |
Dave_25152
Joined: 11 Dec 2010 Posts: 60
|
What am I doing wrong? - Connect SD card to PIC |
Posted: Sat Mar 03, 2012 7:03 pm |
|
|
Hello
I've tried all the libraries in this forum to communicate with the SD card, but all without success .
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
PS:
I forgot, the circuit is equal to this:
![](http://lh3.ggpht.com/_Vz3EGLO6i58/S_nwMU-4vgI/AAAAAAAACFs/JCVe4YHpR34/image_thumb%5B11%5D.png) |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9172 Location: Greensville,Ontario
|
|
Posted: Sat Mar 03, 2012 8:58 pm |
|
|
hardware error...
like dozens before you.....
5volt PIC....3volt SD card NOT compatible... |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sat Mar 03, 2012 11:17 pm |
|
|
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!! |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Mar 04, 2012 4:45 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9172 Location: Greensville,Ontario
|
|
Posted: Sun Mar 04, 2012 6:34 am |
|
|
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 !! |
|
![](templates/subSilver/images/spacer.gif) |
Dave_25152
Joined: 11 Dec 2010 Posts: 60
|
|
Posted: Sun Mar 04, 2012 8:13 am |
|
|
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? ...
Thanks for all the answers! |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Mar 04, 2012 9:25 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Mar 04, 2012 9:42 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Dave_25152
Joined: 11 Dec 2010 Posts: 60
|
|
Posted: Sun Mar 04, 2012 9:58 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9172 Location: Greensville,Ontario
|
|
Posted: Sun Mar 04, 2012 11:03 am |
|
|
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(.....) ). |
|
![](templates/subSilver/images/spacer.gif) |
Dave_25152
Joined: 11 Dec 2010 Posts: 60
|
|
Posted: Sun Mar 04, 2012 11:38 am |
|
|
OK, thanks.
I will then keep the set_tris () correctly.
However,'ve tried again and the LED lit, but the card still empty. Why? |
|
![](templates/subSilver/images/spacer.gif) |
miro
Joined: 15 Jan 2011 Posts: 62
|
|
Posted: Sun Mar 04, 2012 12:04 pm |
|
|
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); |
|
![](templates/subSilver/images/spacer.gif) |
Dave_25152
Joined: 11 Dec 2010 Posts: 60
|
|
Posted: Sun Mar 04, 2012 12:22 pm |
|
|
Hum... OK.
Quote: | ERROR INITIALIZING FAT f= -1 |
It may be that the card is not suitable for this? |
|
![](templates/subSilver/images/spacer.gif) |
miro
Joined: 15 Jan 2011 Posts: 62
|
|
Posted: Sun Mar 04, 2012 12:24 pm |
|
|
Welcome in the club ![Razz](images/smiles/icon_razz.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Dave_25152
Joined: 11 Dec 2010 Posts: 60
|
|
Posted: Sun Mar 04, 2012 12:34 pm |
|
|
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); |
|
![](templates/subSilver/images/spacer.gif) |
|