Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How can read / write to SD card ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Friends,

How can I read / write a data into SD card ?
Should I make it a file ? or just read an array of hex from SD card ?
How can I write an array into SD card ?

For example I have a bitmap (*.bmp) file, then how can I write it into SD card without a file ( only an array ) ? and then how to read it ?

Thanks
 

Hello!

It depends on how you want to use the SD card. If you want to do a raw read / raw write, then
you can do it almost directly without file system (i.e. you just have to initialize the SD card
properly and then access it by 512 bytes blocks. But you have to care of all the data management
yourself (i.e. if you use icons, then you have to decide yourself that icon 1 starts at 0 and ends
at 1FF, icon2 strarts at 200 and ends at 3FF, etc...
You can program raw files with a PC using HxD (that's the program name, it is free).
Now if you want to write bmp files from a PC and then read them from a micro controller,
then you need a FAT file system program for your micro controller.
There are myriads of file system versions on the net.

Dora.
 

Hello!

It depends on how you want to use the SD card. If you want to do a raw read / raw write, then
you can do it almost directly without file system (i.e. you just have to initialize the SD card
properly and then access it by 512 bytes blocks. But you have to care of all the data management
yourself (i.e. if you use icons, then you have to decide yourself that icon 1 starts at 0 and ends
at 1FF, icon2 strarts at 200 and ends at 3FF, etc...
You can program raw files with a PC using HxD (that's the program name, it is free).
Now if you want to write bmp files from a PC and then read them from a micro controller,
then you need a FAT file system program for your micro controller.
There are myriads of file system versions on the net.

Dora.
Dora,

Which file system do you use usually ? If I have plenty of files I think I prefer using FAT system, but if it's only hexa format I'm gonna use raw format...
So the software in the flash, must have the ability to read a file system, is the software big enough? since my flash is limited.
May be I will try both system, I read awhile ago about interfacing SD card with SPI method, have you done it before ?
**broken link removed**

Thanks
 

Hello!

I always use FAT (16/32) because it allows me to write on PC and it's easier when dealing with multiple files.
What do you mean by hexa format? There is no hexa format for files. Hexa is only a representation of binary
that is more user readable.
To deal with an SD card:
1. First write a driver that allows you to write / read raw blocks on your card;
2. Try to port a file system. Or write one yourself, it's a very good exercise.

Dora.
 

What do you mean by hexa format?
Dora,
I mean only hexadecimal code format, ( raw format ) so SD card acts as a memory, like RAM or internal flash.
For easy way, may be write one hexadecimal for example FA into SD card and read it, then display into LCD, for example FA is translated into "Hexadecimal A" in LCD.

Do you have function for reading SD in raw ?
Thanks
 

This HxD ?
HxD.jpg
 

Dora,

What filename should I give if it's a raw write, because the software asking me filename ?
I'm using card reader attached to PC, for writing a raw write and this SD had been formatted on FAT32, otherwise window can not detect it,
Now, can I read this file with uC using SPI method ?
filename.jpg
file in window explorer :
file.jpg
 

Hello!

I know (more or less) what is hexadecimal. What I meant is if you have, say, an array like this:

uint8 data[] = {
0x00, 0x01, 0x02, 0x03.... 0xFF
}

It's an array written in hexadecimal in your code.

If you define a second array like this:

uint8 data2[] = {
0, 1, 2, 3, 4, ... 255
}

Then the array is written in decimal, but these 2 arrays contain exactly the same data.
If you write one or the other to the SD card, it will yield the same result, and if you look
inside the SD card, there is no way to know how it was written in the source code. Hexa
is a representation which is more user-friendly in some cases, but the actual data is not
hexadecimal at all, it's plain binary.
If you use an hexa editor, it reads the data in the card and produces hexadecimal code
to show it in a clean way, ordered and aligned.

Now as for writing to SD card, no I don't have a function to do that right now, but it's easy
to do. Read the SD specs and you will find out (as far as I remember) that there is a
write command after which you send a 512 byte block (command usually called cmd24).

And there is a command for read (CMD17).

There are also 2 other command for continuous multi block write and read.

Dora.
 

Hello!

I know (more or less) what is hexadecimal. What I meant is if you have, say, an array like this:

uint8 data[] = {
0x00, 0x01, 0x02, 0x03.... 0xFF
}

It's an array written in hexadecimal in your code.

If you define a second array like this:

uint8 data2[] = {
0, 1, 2, 3, 4, ... 255
}

Then the array is written in decimal, but these 2 arrays contain exactly the same data.
If you write one or the other to the SD card, it will yield the same result, and if you look
inside the SD card, there is no way to know how it was written in the source code. Hexa
is a representation which is more user-friendly in some cases, but the actual data is not
hexadecimal at all, it's plain binary.
If you use an hexa editor, it reads the data in the card and produces hexadecimal code
to show it in a clean way, ordered and aligned.

Now as for writing to SD card, no I don't have a function to do that right now, but it's easy
to do. Read the SD specs and you will find out (as far as I remember) that there is a
write command after which you send a 512 byte block (command usually called cmd24).

And there is a command for read (CMD17).

There are also 2 other command for continuous multi block write and read.

Dora.

Yes I know that hexadecimal will be written in binary in my card, 1010 1010....FF is 1111 1111,
Thanks for the info, I'll get back to you if I have figured out one function for reading..
 

can I use AT89LV55 for reading SD card ? Is the internal RAM enough ? internal RAM = 256 Bytes
 

can I use AT89LV55 for reading SD card ? Is the internal RAM enough ? internal RAM = 256 Bytes
You should be able to read and write some data, implementing any kind of file system with write capability is effectively impossible under this conditions, because you can't write smaller entities than a 512 Byte sector. To read/modify/write any sector, e.g. a directory or FAT entry, it most be completely buffered.
 

you can't write smaller entities than a 512 Byte sector
Do you mean, I can not use AT89LV55 for reading and writing SD card ?
I don't get what you mean ?
 

To understand the problem, you may need to consult the SD command details. Please consider how you want to change e.g. a directory entry without buffering a directory sector in RAM.
 

To understand the problem, you may need to consult the SD command details. Please consider how you want to change e.g. a directory entry without buffering a directory sector in RAM.

I simply want to read a character from SD card and put it into array then display it onto my LCD...so the uC is only reading and displaying the content of SD card...
Is it possible without RAM..?
or only with 256 bytes RAM ?
 

I simply want to read a character from SD card and put it into array then display it onto my LCD...so the uC is only reading and displaying the content of SD card...
Is it possible without RAM..?
Yes. You have been talking about "reading and writing SD card" and "using FAT system".

I mentioned before, that reading some data should work. You can also basically decode a FAT structure, unless you don't need to write to it. It's probably more easy to read data from a file with known absolute sector, e.g. the first sector of the first file copied to a newly formatted SD card.
 

Hello!

According to what I developed in the past, the strict minimum to access a FAT16 in read and write
mode is about 800 bytes of RAM.
You need 512 bytes for the SD sector and you need management variables.
So basically, you need a processor with 2 K memory. I suppose it may be feasible with 1K, but
keep in mind that some part of the RAM is used for the stack and in some cases for the
interrupt routine addresses. And also if you do something else than accessing the SD card,
then you will need some RAM.

Dora.
 

Hello!

According to what I developed in the past, the strict minimum to access a FAT16 in read and write
mode is about 800 bytes of RAM.
You need 512 bytes for the SD sector and you need management variables.
So basically, you need a processor with 2 K memory. I suppose it may be feasible with 1K, but
keep in mind that some part of the RAM is used for the stack and in some cases for the
interrupt routine addresses. And also if you do something else than accessing the SD card,
then you will need some RAM.

Dora.

Hi Dora,
So if I'm using AT89LV55, I need to use external RAM chip for reading sd card ?
because the internal RAM is 256 bytes ?
I want to access raw hexadecimal only, and making SD card as a SPI, is it possible with 256 bytes internal RAM ?

---------- Post added at 03:32 ---------- Previous post was at 02:09 ----------

can I do this in AT89LV55 ?
Code:
unsigned char xdata DATA[512];
.
.
.
.
SdReadBlock(DATA,AddTemp+(j*512),512);
 

Hello!

I have never used AT chips, so I don't know. If you have an EMIF interface, then yes
you can add an external RAM. If you don't have an EMIF interface, then it's probably
better to choose a chip that has enough RAM. With recent processors, you can have
for less than 5 USD some processors with +/- 4 or 8 K RAM.

Now for your question:
If you define unsigned char date[512], it means that you tell the compiler to reserve
512 bytes in RAM. If you don't have 512 bytes, then I think it will likely not compile.
An if it does compile, it will not work.

Dora.
 

Hello!

I have never used AT chips, so I don't know. If you have an EMIF interface, then yes
you can add an external RAM. If you don't have an EMIF interface, then it's probably
better to choose a chip that has enough RAM. With recent processors, you can have
for less than 5 USD some processors with +/- 4 or 8 K RAM.

Now for your question:
If you define unsigned char date[512], it means that you tell the compiler to reserve
512 bytes in RAM. If you don't have 512 bytes, then I think it will likely not compile.
An if it does compile, it will not work.

Dora.
from your suggestion, I'm gonna find a uC with 4Kbytes internal RAM then, do you have any ideas ? which uC ? how about STC89C516RD+
STC89C516RD.jpg
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top