PDA

View Full Version : SD card library?



Alexey
- 4th December 2011, 05:54
Hi All,

Is there a library for reading/writing files on SD card?

Thank you

mackrackit
- 4th December 2011, 06:33
6159
http://melabs.com/samples/PBP-mixed/sdfs3.htm

Alexey
- 4th December 2011, 06:52
Wow! Thank you Mackrackit, will try. I was afraid may have to use another language which I always try to avoid...

mackrackit
- 4th December 2011, 06:57
Here is a project example
http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING

Alexey
- 6th December 2011, 17:18
Hello Macrackit,

Thank you very much for the very good data logging sample! Also thank you for attaching the file as the link http://www.melabs.com/resources/samples/pbp/sdfs3.zip (http://www.melabs.com/resources/samples/pbp/sdfs3.zip) seems to be no longer available (page not found)

Best regards

KVLV
- 14th December 2011, 23:44
a quick search found this http://melabs.com/samples/PBP-mixed/sdfs3.htm

mackrackit
- 14th December 2011, 23:57
a quick search found this http://melabs.com/samples/PBP-mixed/sdfs3.htm
And you only had to search as far as post #2 of this thread :)

KVLV
- 15th December 2011, 01:46
And you only had to search as far as post #2 of this thread :)
oh, already posted.

Alexey
- 15th December 2011, 02:56
Makrackit, KVLV, thanks again, I really enjoy SDFS3.zip (http://www.picbasic.co.uk/forum/attachment.php?attachmentid=6159&d=1322980372) and http://www.picbasic.co.uk/forum/cont...USB-SD-LOGGING (http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING)

Alexey

Alexey
- 19th January 2012, 05:30
Hello Mackrakit,

The code you advised works just fine, I only have one issue - when I reset MCU my program can not reinitialize the SD card until I replace it. Is this something related to the card itself or correct software should do it? I use SDC_UseHardSPI = FALSE if this matters

Than you,

Alexey

mackrackit
- 19th January 2012, 09:21
I will guess that the SD card is not booted fully before your code tries to access it.

Alexey
- 19th January 2012, 19:44
tried to add pause at start and it did not help, but then I learnt that I was resetting the controller at the time when the card is being accessed and this was hanging the card. Adding indication when not to reset helped

micromaster
- 20th January 2012, 05:22
Hi all

i also have been working with sd filing system with number of lang: including pic basic and hi-tec c . all of them worked very well with proteus simulation environment(isis). but all of them were very unstable when i build them in practical world. Can anyone give me some tips to make them stably working?? ive used home made p.c.bs for my systems. Are there any specific potential dividing resisters to be used at port outputs and sd card input? ive been using 1k + 10k combination.any caps mabe?? im using pic 18f 2550 for all of my sd card based systems.

pedja089
- 20th January 2012, 09:46
How much current sd card is draw when writing file?

mackrackit
- 20th January 2012, 15:51
Run the whole system at 3.3 volts and then you will not have to use dividing resistors.

pedja089
- 21st January 2012, 20:35
I meant on power supply pin.
I'm waiting for test pcb to arrive from manufacturer....

micromaster
- 30th January 2012, 04:27
Run the whole system at 3.3 volts and then you will not have to use dividing resistors.

Thanks, but will pic 18f2550 can run on 3.3v? because its hard to find any other lo power versions in our electronic shops.:surprise:

thanks.

mackrackit
- 30th January 2012, 09:12
Yes, the 2550 and 4550 will run on 3.3 volt. See the link in post #4.

Use a 4MHz external OSC and the chip can be PLL'ed to 48 MHz if needed. The electrical section of the data sheet shows what can be done.

Charlie
- 5th February 2012, 17:33
I've been playing with this code for a couple days now with no joy. My biggest question revolves around my SD card. I have several. They ALL have 9 pins. As does the socket on my development board. Yet the code seems to be written for an SD card with 12 pins? I have no "Write Protect" or "card detect" lines, nor can I find reference to them anywhere but on this site. Do I fake the pins? High or low?
To further add to the confusion, I'm using an 18F2550 and I need the hardware serial port. Since a pin is shared with the hardware SPI port, I'm trying to use SHIFTOUT / SHIFTIN. Things seem to be getting to the SD card, but it does not reply (I get 0's).
Am I missing something simple? Response from an INIT is 6...

Alexey
- 6th February 2012, 04:31
Hi Charlie,

I use PIC18F25K22

and do it this way:

'--------------SD CARD----------------------

'SD_DO VAR PORTC.0
' Alias PIC pins and registers for SD/MMC cardYTE
SDI VAR PORTC.0 ' SPI data in SD #7 (SD_DO)
SDI_TRIS VAR TRISC.0 ' SPI data in direction
SCL VAR PORTC.1 ' SPI clock SD #5
SCL_TRIS VAR TRISC.1 ' SPI clock direction
SDO VAR PORTC.2 ' SPI data out uSD #3
SDO_TRIS VAR TRISC.2 ' SPI data out direction
SD_CS VAR PORTC.3 ' SD card chip select uSD #2
SD_CS_TRIS VAR TRISC.3 ' SD card chip select direction

'SD_CD VAR bit: SD_CD = 1 PORTC.0 ' SD card detect
'SD_CD_TRIS VAR TRISC.0 ' SD card detect direction

INCLUDE "SDFS.PBP"
SDC_UseHardSPI = FALSE ' Use hardware SSP port for SPI.

...


all lines in the SDFS.PBP that refer to the card write protection, card inserted and so on things I disabled like this:

' Subroutines for communicating with an SD/MMC card
' Subroutine to initialize SD card pins
InitIO:
SD_CS = 1 ' SD card not selected.
SD_CS_TRIS = 0 ' Output SD card chip select.
''' SD_CD_TRIS = 1 ' Input card detect.
''' SD_WE_TRIS = 1 ' Input write protect.
SDO = 1 ' Start SPI data out high.
SDO_TRIS = 0 ' Output SPI data out.
SDI_TRIS = 1 ' Input SPI data in.
SCL = 1 ' SPI clock idles high.
SCL_TRIS = 0 ' Output SPI clock.
Return




' Subroutine to initialize hardware SSP for SPI at less than 400kHz
OpenSPIM:
If (SDC_UseHardSPI) Then
''' SSPSTAT = 000000 ' Sample at middle of data output time, Mode 1, 1: Transmit on idle to active clock transition.
''' SSPCON1 = 010010 ' SPI master mode, clock = Fosc/64, Mode 1, 1: Clock idle high.
''' SSPEN = 1 ' Enable hardware SPI port.
Endif
Return




' Subroutine to initialize hardware SSP for SPI at full speed
OpenSPIMFast:
If (SDC_UseHardSPI) Then
''' SSPSTAT = 000000 ' Sample at middle of data output time, Mode 1, 1: Transmit on idle to active clock transition.
''' SSPCON1 = 010000 ' SPI master mode, clock = Fosc/64, Mode 1, 1: Clock idle high.
''' SSPEN = 1 ' Enable hardware SPI port.
Endif
Return




I use micro SD card. You might notice the pins used for card interface are shared with ICSP, but the card can not work while the programmer is connected. Programmer probably does not like such neighbor too (one needs pull up and the other has internal pull down, but it works.

I could not attach the file - error happens - "invalid file" I do not know why, but you see the idea - just comment everything you do not use

Best regards,

Charlie
- 6th February 2012, 16:43
Thanks, Alexey.

I had done something similar, but tried your solution anyway. Still not working. It's possible my SD card is simply not happy with this initialization sequence - I can see clock and "data in" pins (Pic "data out") moving as they should (as best as I can judge with LED's) but the device does not respond. If I put a pullup on it's "data out" line (PIC "data in") I can read FF, otherwise I read 00. To me this means the card is not driving the line.

I know I should post my code, but it's about 8K. After your post, I did try the SDFS3.PBP program with tweaks for my hardware specifics and it did not run either. I might have to go back to a 24L series device...

Alexey
- 7th February 2012, 00:42
Hi Charlie,

You need to have pull-ups on both, data and clock lines

Charlie
- 7th February 2012, 11:28
Just FYI, I finally resolved the issue... turns out my development board has a schematic error. I guess I should have bought the expensive one :surprise:. Wait... considering it just cost me several days of my life - looks like I did.