Discussion in "Project Help" started by    nismo    Jul 31, 2008.
Thu Jul 31 2008, 04:28 PM
#1
Hello all, i am currently doing mp3 player as my assignment. I am facing a very big problem now where i am not able to initialize my sd card with my AT89C51ED2. As a novice, i am inexperience. I hope that anyone can help me with this. Below is my code...

#include <AT89C51ED2.H>
#define sd_input P1_7
#define sd_output P1_5
#define sd_clock P1_6
#define sd_cs P1_1
#define CMD0 0x00
#define CMD16 0x10
#define CMD17 0x11
#define CMD24 0x18
#define CMD55 0x37
#define CMD58 0x3A
#define ACMD41 0x29
#define in_idle_state (1<<0)
#define erase_reset (1<<1)
#define illegal_command (1<<2)
#define crc_error (1<<3)
#define erase_sequence_error (1<<4)
#define address_error (1<<5)
#define parameter_error (1<<6)
#define LCD_data P0
#define LCD_rs P2_1
#define LCD_en P2_0
void LCD_init (void);
void LCD_busy (void);
void LCD_sendData (unsigned char );
void LCD_sendString(unsigned char *);
void LCD_cursorOff (void);

void sd_init (void);

int main (){
LCD_init();
LCD_cursorOff();
sd_init ();
return 0;
}


void sd_init (void){
sd_clock = 400000;
sd_clock = 74;
sd_cs = 0;
sd_input = 0x00;
while(in_idle_state != 0){
sd_input = 0x37;
sd_input = 0x29;
}
if(sd_input = 0x3A)
LCD_sendString ("Yes\n");
else
LCD_sendString ("No\n");
sd_clock = 12000000;
}

void LCD_init(void){
LCD_data = 0X38;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
LCD_data = 0x0F;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
LCD_data = 0x01;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
LCD_data = 0x06;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
}

void LCD_busy (void){
unsigned char i,j;
for(i=0;i<50;i++)
for(j=0;j<255;j++);
}

void LCD_sendData(unsigned char var){
LCD_data = var;
LCD_rs = 1;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
}

void LCD_sendString(unsigned char *var){

while(*var){
LCD_sendData(*var++);
}
}

void LCD_cursorOff(void){
LCD_data = 0x0C;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
}


after compiling the code with sdcc... following errors appear...

sd.c:48: warning 126: unreachable code
sd.c:49: warning 126: unreachable code
sd.c:51: warning 126: unreachable code
sd.c:52: : warning 126: unreachable code




[ Edited Fri Aug 01 2008, 03:53 AM ]
Fri Aug 01 2008, 02:57 PM
#2
this warning means, code you wrote at line 48, 49, 51 and 52 is will not be executed at any time while running on hardware. So code is unrechable. This problem comes when you put a statement which is always true and the other part of the statement never executed like this..

if(1){  //always true
//some code
}
else {
// some code here
// this part of if-else loop is unreachable as condition is always true
}
Sat Aug 09 2008, 06:28 PM
#3
Dear sir, after having some minor modification on the source code. i similar error but in different line of the code... below is my source code and error messages. May i know how to get rid of this error. I am still unable to get my card initialized...


1 #include <AT89C51ED2.H>
2 #define sd_input P1_7
3 #define sd_output P1_5
4 #define sd_clock P1_6
5 #define sd_cs P1_1
6 #define CMD0 0x00
7 #define CMD16 0x10
8 #define CMD17 0x11
9 #define CMD24 0x18
10 #define CMD55 0x37
11 #define CMD58 0x3A
12#define ACMD41 0x29
13#define in_idle_state (1<<0)
14 #define erase_reset (1<<1)
15 #define illegal_command (1<<2)
16 #define crc_error (1<<3)
17 #define erase_sequence_error (1<<4)
18 #define address_error (1<<5)
19 #define parameter_error (1<<6)
20 #define ocr (3<<21)
21
22 void sd_init (void);
23
24 int main (){
25 sd_init ();
26 return 0;
27}
28
29 void sd_init (void){
30 sd_clock = 400000;
31 sd_cs = 0;
32 sd_clock = 74;
33 sd_input = CMD0;
34 sd_input = 0x95;
35 while (in_idle_state == 1){
36 sd_input = CMD55;
37 sd_input = ACMD41;
38 }
39 sd_input = CMD58;
40 sd_cs = 1;
41 sd_clock = 12000000;
42}
43


THE ERRORS ARE
sd.c:39: warning 126: unreachable code
sd.c:40: warning 126: unreachable code
sd.c:41: warning 126: unreachable code


[ Edited Sat Aug 09 2008, 06:41 PM ]
Tags at89c51ed2illegal commandaddress error
Mon Aug 11 2008, 06:24 AM
#4
Nismo, i have added the specification files in download area. check it..
Mon Aug 11 2008, 08:12 AM
#5
i had read the file...emm, quite confusing... haha ...Anyway, thanks for helping. i will try my best to understand. At least i have some clue...
Tue Aug 12 2008, 05:40 AM
#6
good.. try it or i am here... you have already seen the sample code i posted in one of the thread.. just take it as reference.
Tue Aug 26 2008, 05:17 AM
#7
i am still unable to get my sd card initialize after several weeks of trial and error. My lecturer told me that i have to activate the spi in the microcontroller before i initialized the sd card. I wonder how i need to write the c code to activate the spi although i have followed the steps in the data sheet, i am still not able to activate it and get my sd card initialized. Below is my code. This code will display the error mssg in the lcd if my sd card can not go to idle state. :mad Please help me with my problem as i have no clue at all. Thanks...

#include <AT89C51ED2.H>
#define LCD_data P0
#define LCD_rs P2_1
#define LCD_en P2_0
#define sd_input P1_7
#define sd_output P1_5
#define sd_clock P1_6
#define sd_cs P1_0
void sd_init (void);
void LCD_init (void);
void LCD_busy (void);
void LCD_sendData (unsigned char );
void LCD_sendString(unsigned char *);
void LCD_cursorOff (void);


int main (){
LCD_init();
LCD_cursorOff();
sd_init ();
return 0;
}

void LCD_init(void){
LCD_data = 0X38;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
LCD_data = 0x0F;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
LCD_data = 0x01;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
LCD_data = 0x06;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
}

void LCD_busy (void){
unsigned char i,j;
for(i=0;i<50;i++)
for(j=0;j<255;j++);
}

void LCD_sendData(unsigned char var){
LCD_data = var;
LCD_rs = 1;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
}

void LCD_sendString(unsigned char *var){

while(*var){
LCD_sendData(*var++);
}
}

void LCD_cursorOff(void){
LCD_data = 0x0C;
LCD_rs = 0;
LCD_en = 1;
LCD_en = 0;
LCD_busy();
}

void sd_init(void){
int i;
SPCON |= (1<<7);
SPCON |= (1<<6);
SPCON &= ~(1<<5);
SPCON |= (1<<4);
SPCON &= ~(1<<3);
SPCON |= (1<<2);
SPCON &= ~(1<<1);
SPCON &= ~(1<<0);
sd_cs = 1;
for(i=0;i<10;i++)
sd_input = 0xFF;
sd_cs = 0;
for(i=0;i<2;i++)
sd_input = 0xFF;
sd_input = 0x40;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x95;

for(i=0;i<=1000;i++){
if (sd_output == 0x01)
break;
if (i==1000){
goto error_1;
break;
}
}
error_1:
LCD_sendString ("0");
i=0;
while (sd_output != 0x01){
sd_input = 0x77;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0xFF;
sd_input = 0x69;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0xFF;
i++;
if(i>1000)
goto error_2;
}
error_2:
LCD_sendString ("error");
}
Tue Aug 26 2008, 02:12 PM
#8
How are you checking your SPI initialization?

Do you have proteus? If yes then make a simple program of SPI and check the spi bus on SPI debugger terminal. see if you are getting same thing you are sending.
Also i did not understand what are you trying to do here..

while (sd_output != 0x01){
sd_input = 0x77;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0xFF;
sd_input = 0x69;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0xFF;
i++;
if(i>
1000)
goto error_2;
}


If you are using hardware SPI, then you dont need to bitbang the data on o/p pins. I hope you are getting my point. You can download sample spi codes from Atmel's Website.
Tue Aug 26 2008, 04:59 PM
#9
Thanks for replying Ajay. What is proteus ? Actually i want to interface my sd card with spi but i do not know how to activate the spi in AT89C51ED2. Errmm, would you mind showing a c code example on how to make a simple program of SPI and check the spi bus on SPI debugger terminal to see if i am 0 getting same thing i am sending.


while (sd_output != 0x01){
sd_input = 0x77;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0xFF;
sd_input = 0x69;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0x00;
sd_input = 0xFF;
i++;
if(i>1000)
goto error_2;
}


For this part of my code, i am trying to send CMD55 (Notifies the card that the next command is anapplication specific command rather than astandard command.) and ACMD41(Activates the card’s initialization process.). If possible, would you mind to show me how to activate the spi in microcontroler in c code?Hope to hear from you soon.


[ Edited Tue Aug 26 2008, 05:02 PM ]
Thu Aug 28 2008, 06:32 AM
#10
Hi nismo, please take a look at the C51 samples from Atmel. Tell me if you did not understand anything. I will explain you.
http://atmel.com/dyn/products/tools_card.asp?tool_id=3419

Get Social

Information

Powered by e107 Forum System

Conversation

No messages yet.

Downloads

Comments

Mirza123
Thu Jun 25 2020, 03:46 PM
eunicelove124
Mon Jun 22 2020, 03:03 PM
cerouno
Tue Jun 16 2020, 05:10 PM
Marce
Sat Jun 13 2020, 09:43 PM
Davidthils
Thu May 21 2020, 12:44 PM
Jakeror
Thu May 21 2020, 01:28 AM
motorCar
Wed May 20 2020, 07:05 PM
Vordrync
Wed May 20 2020, 11:52 AM

Online

Guests: 148, Members: 1 ...
pH viewing chatbox_menu.php

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123