Project
Read/write data in a SDCard with a microcontroller ATMega32
Electronic
The SD Card has 2 communication protocols. One of them is a serial protocol. It is easily interfaced on the SPI port of a microcontroller.
The SD card uses 3.3V IOs but usually the microcontrollers works at 5V. We can use resistor bridge to convert microcontroller outputs to 3.3V. And the 3.3V DO pin of the sdCard can be connected directly to the MISO input of the microcontroller that will trigger even at 3.3V
On the following schematic, I use a Maxim 604 3.3V regulator.
SD Card pinout
|
Pin
| Name
| Type
| Description
| Atmel
|
1
| |CS
| Input
| Chip selection in low status
| |SS
|
2
| DI
| Input
| Data input
| Master Out/Slave In (MOSI)
|
3
| VSS
| Power
| Ground
| GND
|
4
| VDD
| Power
| Power supply (2.7V 3.6V)
|
|
5
| SCLK
| Input
| Clock
| SCK
|
6
| VSS2
| Power
| Ground
| GND
|
7
| DO
| Output
| Data output
| Master In/Slave Out (MISO)
|
8
| RSV
|
| Reserved
|
|
9
| RSV
|
| Reserved
|
|
|
MicroSD Card pinout
|
Pin
| Name
| Type
| Description
| Atmel
|
1
| NC
|
| Not connected
|
|
2
| |CS
| Input
| Chip selection in low status
| |SS
|
3
| DI
| Input
| Data input
| Master Out/Slave In (MOSI)
|
4
| VDD
| Power
| Power supply (2.7V 3.6V)
|
|
5
| SCLK
| Input
| Clock
| SCK
|
6
| VSS
| Power
| Ground
| GND
|
7
| DO
| Output
| Data output
| Master In/Slave Out (MISO)
|
8
| RSV
|
| Reserved
|
|
|
Software
Source code
The zip file above contains all my source code to read/write the sdCard blocks and can be compiled with WinAVR.
The sd card is composed of many blocks of 512 bytes. In the
sdCard.cpp/.h files, there are commands to:
- Initialize the SPI port of the atmel
- Initialize the connection with de SD card
- Read the 512 bytes of a block
- Write 512 bytes in block
- Read the status register of the SDCard
The program also changes the SPI port speed between the initialization and the read/write phase because the SDCard can communicate faster after initialization. The SPI port of an atmel at 8MHz cannot reach the maximum speed of the SDCard. With this program and an atmel at 8MHz (internal oscillator of the ATMega32/AtMega324), it is possible to write at 85 kBytes/s and read at 182 kBytes/s
Links
SD Card communication protocol description and SPI command description:
Simplified_Physical_Layer_Spec2.0.pdf
ProdManualSDCardv1.9.pdf
Basic example of SD card communication with an ATMega32 that inspired me:
http://www.captain.at
SD card comminucation through SPI with a maxim microcontroller:
Maxim application note 4068