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.

SD card code with atmega16A

Status
Not open for further replies.

foxbrain

Full Member level 2
Full Member level 2
Joined
Feb 1, 2010
Messages
142
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,298
Location
Damas
Visit site
Activity points
2,322
how can i put the code to interface sd card to atmega 16A?
i want to read and write from it...
thanks
 

the first one doesn't seems to have a good code.
the second looks good , i'll try it.
the third one : i saw it before it has good explaining but the code have got some errors while compiling on avr studio , i think it missed some defines......
thanks
 

This is the code of the link #3, it compiles fine, you probably didn't put the include at the start of the code, I have no idea if it works.
The FOSC should probably change depending on the clock speed of your mcu.
There was also a return 0 at the end of the main, I have added an endless while loop.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <avr/io.h>
#include <avr/interrupt.h>
 
#define FOSC 6400000
 
char chars[512];
 
#define DI 6                         // Port B bit 6 (pin7): data in (data from MMC)
#define DT 5                        // Port B bit 5 (pin6): data out (data to MMC)
#define CLK 7                     // Port B bit 7 (pin8): clock
#define CS 4                        // Port B bit 4 (pin5): chip select for MMC
 
//SPI Initialization:
 
void ini_SPI(void) {
 
    DDRB &= ~(_BV(DI));                     //input
    DDRB |= _BV(CLK);                     //outputs
    DDRB |= _BV(DT);                     //outputs
    DDRB |= _BV(CS);                     //outputs
    SPCR |= _BV(SPE);                     //SPI enable
    SPCR |= _BV(MSTR);                     //Master SPI mode
    SPCR &= ~(_BV(SPR1));                    //fosc/16
    SPCR |= _BV(SPR0);                    //fosc/16
    SPSR &= ~(_BV(SPI2X));                    //speed is not doubled
    PORTB &= ~(_BV(CS));                     //Enable CS pin for the SD card
}
 
//Functions for sending and receiving one byte through SPI:
 
char SPI_sendchar(char chr) {
    char receivedchar = 0;
    SPDR = chr;
    while(!(SPSR & (1<<SPIF)));
    receivedchar = SPDR;
    return (receivedchar);
}
 
 
 
//Function to send a command frame Command:
 
char Command(char cmd, uint16_t ArgH, uint16_t ArgL, char crc ) {
 
    SPI_sendchar(0xFF);
    SPI_sendchar(cmd);
    SPI_sendchar((uint8_t)(ArgH >> 8));
    SPI_sendchar((uint8_t)ArgH);
    SPI_sendchar((uint8_t)(ArgL >> 8));
    SPI_sendchar((uint8_t)ArgL);
    SPI_sendchar(crc);
    SPI_sendchar(0xFF);
    return SPI_sendchar(0xFF);                // Returns the last byte received
}
 
//Initialization card:
 
void ini_SD(void) {
 
    char i;
    PORTB |= _BV(CS);                    //disable CS
    for(i=0; i < 10; i++)
        SPI_sendchar(0xFF);                // Send 10 * 8 = 80 clock pulses 400 kHz
    PORTB &= ~(_BV(CS));                 //enable CS
    for(i=0; i < 2; i++)
        SPI_sendchar(0xFF);                // Send 2 * 8 = 16 clock pulses 400 kHz
    Command(0x40,0,0,0x95);              // reset
 
idle_no:
    if (Command(0x41,0,0,0xFF) !=0)
        goto idle_no;                        //idle = L?
    SPCR &= ~(_BV(SPR0));                //fosc/4
}
 
///Writing to the card:
//The function returns 1 if an error occurs  else returns 0 if successful
 
int write(void) {
 
    int i;
    uint8_t wbr;
 
    //Set write mode 512 bytes
    if (Command(0x58,0,512,0xFF) !=0) {
 
        //Determine value of the response byte 0 = no errors
        return 1;
 
        //return value 1 = error
    }
 
    SPI_sendchar(0xFF);
    SPI_sendchar(0xFF);
    SPI_sendchar(0xFE);
 
    //recommended by posting a terminator sequence [2]
 
    //write data from chars [512] tab
 
    uint16_t ix;
    char r1 =  Command(0x58,0,512,0xFF);
 
    for (ix = 0; ix < 50000; ix++) {
        if (r1 == (char)0x00) break;
        r1 = SPI_sendchar(0xFF);
    }
 
    if (r1 != (char)0x00) {
        return 1;
 
        //return value 1 = error
    }
 
    //recommended by the control loop [2]
 
    SPI_sendchar(0xFF);
    SPI_sendchar(0xFF);
    wbr = SPI_sendchar(0xFF);
 
    //write block response and testing error
    wbr &= 0x1F;
 
    //zeroing top three indeterminate bits 0b.0001.1111
    if (wbr != 0x05) { // 0x05 = 0b.0000.0101
 
        //write error or CRC error
        return 1;
    }
 
    while(SPI_sendchar(0xFF) != (char)0xFF);
 
    //wait for the completion of a write operation to the card
    return 0;
}
 
//Reading from the card:
//The function returns 1 if an error occurs  else returns 0 if successful
 
int read(void) {
 
    int i;
    uint16_t ix;
 
    char r1 =  Command(0x51,0,512,0xFF);
    for (ix = 0; ix < 50000; ix++) {
        if (r1 == (char)0x00) break;
        r1 = SPI_sendchar(0xFF);
    }
 
    if (r1 != (char)0x00) {
        return 1;
    }
 
    //read from the card will start after the framework
 
    while(SPI_sendchar(0xFF) != (char)0xFE);
 
    for(i=0; i < 512; i++) {
        while(!(SPSR & (1<<SPIF)));
        chars[i] = SPDR;
        SPDR = SPI_sendchar(0xFF);
    }
 
    SPI_sendchar(0xFF);
    SPI_sendchar(0xFF);
 
    return 0;
}
 
 
int main(void) {
 
    ini_SPI();
    ini_SD();
    sei();
    write();
    read();
 
    //return 0;
   while(1);
}



Alex
 
the code you had written : what does the function write() does?what does it writes on the sd card?
it doesn't takes parameters to put what i want on the sd card!!!
 

As a said when I wrote the code
This is the code of the link #3, it compiles fine, I have no idea if it works.

I have no idea what it does, how it works or if it is correct, I have just placed all the given code parts in order so that they can be compiled.
The write function seems to be filling the card with 0xff, SPI_sendchar(0xFF); assuming that it works correctly.

Alex
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top