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

Sun Oct 13 2019, 02:02 AM
Aline Hutchinson
Hey there,
ExperimenterUK
Sat Oct 12 2019, 10:23 PM
Log in.. then post your code in the 8051 forum
Sat Oct 12 2019, 10:18 AM
Bhadra Sajikumar
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 07:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 08:48 PM
Caryn Kover
Thank you for your time,
ExperimenterUK
Mon Aug 05 2019, 07:09 PM
@Ismail ..post in forum, give details
Mon Aug 05 2019, 04:33 PM
Ismail balghmi
Hello. Please I won't download
ExperimenterUK
Wed Jul 24 2019, 12:47 AM
@BUSTER123 Please post in the forum
Tue Jul 23 2019, 06:13 PM
BUSTER123
hello! newbie here. i am kind of a guy who hates using ready - made existing libraries, so this helped a lot as i was trying to control and write to a character lcd display. however, i can't figure out how to set the cursor position. for example, i want to set it to the 5th column in the first row, so position 5. so according to the table, i am sending 10000101 on the 8 data lines of the lcd (register select pulled low). the cursor does not move there however. and i cant move the cursor left or right too. is it something wrong that i am doing? thank you.
ExperimenterUK
Thu Jul 11 2019, 07:55 PM
@ramos https://www.theengineeringprojects.com/2017/09/l298-motor-driver-library-proteus.html

Downloads

Comments

ilylily07
Tue Oct 15 2019, 04:32 PM
hientran123
Tue Oct 15 2019, 02:27 AM
Sat Oct 05 2019, 02:38 PM
zexalo
Thu Oct 03 2019, 09:53 AM
thanhle12345
Thu Oct 03 2019, 07:37 AM
ramon_vsc
Fri Sep 20 2019, 04:53 PM
youssoufba
Thu Sep 19 2019, 11:57 AM
Taiphan000
Tue Sep 17 2019, 04:21 AM

Online

Guests: 42, Members: 0 ...

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

Members: 38097
Newest member: ilylily07
Forum Activity
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:09 PM
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:02 PM
sir, when i on display ,sometime 3rd display is no...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 02:53 PM
sir, when i on display ,sometime 3rd display is no...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:13 PM
please find video attachment. 3rd & 4th displa...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:05 PM
sir, 3rd and 4th dot matrix display flickering, ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Oct 18 2019, 09:18 PM
Hopefully you understand some of the program.Which...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Thu Oct 17 2019, 09:38 PM
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Oct 17 2019, 08:35 PM
Do you understand how this code works?If so, addin...