Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Problem with reading data from eeprom 24c32

Status
Not open for further replies.

anoop kr

Member level 4
Joined
Aug 6, 2010
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
kerala
Activity points
1,885
Hai all....after a long time i am joining with you....

For the last four days i was in trouble with one program..It was interfacing the serial memory 24c32 (I2C) with P89V51RD2.The microcontroller P89V51RD2 is based on 8051 architecture...The whole protocol that i wrote in assembly and it worked.But the same thing when i write in C language, it is not working completely.That is in assembly, i can perform both writing and reading to and from the EEPROM(24C32).When the program that wrote in C, only writing to the memeory is happening,the reading couldn't take place..I tried a lot in different ways and when i am searching in internet it shows the same code that i had written...Somebody please help me.. I am giving the code that i wrote and the picture of the proteus simulation..:oops:


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
182
183
184
185
186
187
188
189
190
191
192
#include <REGX51.H>
#define SCL P0_0
#define SDA P0_1
 
void INIT_I2C();
void DELAY();
void SEND_DATA(int);
void SEND_DATA1(int);
void GET_DATA();
//void ACK();
//void NAK();
//void MACK();
void START();
void STOP();
 
/************ MAIN PROGRAMME STARTS HERE ***************/
 
void main()
{
INIT_I2C();
START();
SEND_DATA(0XA0);
SEND_DATA(0X00);
SEND_DATA(0X33);
STOP();
START();
SEND_DATA(0XA0);
SEND_DATA(0X00);
START();
SEND_DATA(0XA1);
 
GET_DATA();
STOP();
while(1);
}
 
/**********************              ******************/
 
 
/**** DELAY PGM ****/
 
void DELAY()
{
unsigned int p;
for(p=0;p<3;p++);
}
 
/*****    *****/
 
void INIT_I2C()
{
SDA=1;
SCL=1;
}
 
 
void START()
{
SDA=1;
DELAY();
SCL=1;
DELAY();
SDA=0;
DELAY();
SCL=0;
}
 
void STOP()
{
SCL=1;
DELAY();
SDA=0;
//DELAY();
SDA=1;
DELAY();
SCL=0;
}
 
void SEND_DATA(int x)
{
 int Z,i,k;
Z=x;
k=x;
for(i=0;i<8;i++)
{
Z&=(0x80);
if(Z==0x80)
{
SDA=1;
SCL=1;
DELAY();
SCL=0;
}
else
{
SDA=0;
SCL=1;
DELAY();
SCL=0;
}
k<<=1;
Z=k;
}
while(SDA==1);
SCL=1;
DELAY();
SCL=0;
SDA=0;
 
}
 
 
void SEND_DATA1(int x)
{
 int Z,i,k;
Z=x;
k=x;
for(i=0;i<8;i++)
{
Z&=(0x80);
if(Z==0x80)
{
SDA=1;
SCL=1;
DELAY();
SCL=0;
}
else
{
SDA=0;
SCL=1;
DELAY();
SCL=0;
}
k<<=1;
Z=k;
}
 
}
 
void GET_DATA()
{
        unsigned char i,Data=0X00;
        for(i=0;i<=8;i++)
        {
              //  SCL = 0;
                SCL = 1;
            //  DELAY();
                Data|=SDA;
                Data<<=1;
                SCL=0;
            //  SCL=1;
        }
    //   SCL = 0;
        SDA = 1;
        SCL=1;
        DELAY();
        SCL=0;
        
        P2=Data;
        SDA=0;
}
/*
void ACK()
{
while(SDA==1);
SCL=1;
DELAY();
SCL=0;
}
 
void NAK()
{
SCL=1;
DELAY();
SDA=1;
DELAY();
SCL=1;
DELAY();
SCL=0;
 
}
 
void MACK()
{
SDA=0;
SCL=1;
DELAY();
 
SCL=0;
}
*/



Please format your image attachments properly, use gif, png, or jpeg extension instead of BMP to reduce the size of the file.
Image replaced with .png
i2c.png
 
Last edited by a moderator:

dean_winchester

Advanced Member level 4
Joined
Oct 19, 2010
Messages
111
Helped
22
Reputation
44
Reaction score
21
Trophy points
1,298
Location
India
Activity points
1,826
try adding delay in reading and writing cycle. give 10ms delay. It will work nice and smooth. Also take care of delay when Restarting (0xA1)..
 

anoop kr

Member level 4
Joined
Aug 6, 2010
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
kerala
Activity points
1,885
regarding about your answer i have one doubt.The doubt is about the delay. ie the delay timing calculation. In assembly language we can easily calculate the delay by knowing the MACHINE CYCLE of each instruction. But my doubt is that how can i calculate the same time delay in Embedded C. What is time taken for incrementing a variable value. ie suppose "i" is an unsigned integer variable and is declared as i=0.if i increment the value of the variable "i" by using "i++",what is the time taken for it.Especially in LOOP.In Embedded C for normal time delay generation we are always using "for loop".In that how the time delay is calculated..I had made a long search and i asked to many peoples,but no one gave me exact answer.Please help me......
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,198
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,528
...In Embedded C for normal time delay generation we are always using "for loop".In that how the time delay is calculated..I had made a long search and i asked to many peoples,but no one gave me exact answer.Please help me......
In C language, even compiling optimization levels could eventually interfere on amont of machine intrucions used.
Actually, there are no precise answer to that question due this cannot be defined deterministically.

+++
 

dean_winchester

Advanced Member level 4
Joined
Oct 19, 2010
Messages
111
Helped
22
Reputation
44
Reaction score
21
Trophy points
1,298
Location
India
Activity points
1,826
The best solution to giving delay is using the counter of MCU.. rather than stopping the microcontroller for giving delay in a loop, just make it interrupt based.
 

anoop kr

Member level 4
Joined
Aug 6, 2010
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
kerala
Activity points
1,885
Hello..
I tried the above program with delay. But i didn't get result. It shows the same output as shown before.
I am giving the modified program here.please help me.I think the problem is related with the data reading subroutine(GET_DATA()).Whatever i write into the EEPROM the writing is happening perfectly..But when reading operation is performing it shows everytime "00" in PORT 2. What changes i have make.... Please view the uploaded image also.....




Code:
#include <REGX51.H>
#define SCL P0_0
#define SDA P0_1

void INIT_I2C();
void DELAY();
void DELAY1();
void SEND_DATA(int);
void SEND_DATA1(int);
void GET_DATA();
//void ACK();
//void NAK();
//void MACK();
void START();
void STOP();

/************ MAIN PROGRAMME STARTS HERE ***************/

void main()
{
INIT_I2C();
START();
SEND_DATA(0XA0);
//DELAY1();
SEND_DATA(0X00);
//DELAY1();
SEND_DATA(0X34);
DELAY1();
STOP();
DELAY1();
START();
SEND_DATA(0XA0);
//DELAY1();
SEND_DATA(0X00);
DELAY1();
START();
SEND_DATA(0XA1);
//DELAY1();
GET_DATA();
//DELAY1();
STOP();
while(1);
}

/**********************              ******************/


/**** DELAY PGM ****/

void DELAY()
{
unsigned int p;
for(p=0;p<10;p++);
}

/*****    *****/

/**** DELAY PGM (10 ms DELAY) ****/

void DELAY1()
{
unsigned int X,Y;
for(X=0;X<10;X++)
for(Y=0;Y<500;Y++);
}

/*****    *****/



void INIT_I2C()
{
SDA=1;
SCL=1;
}


void START()
{
SDA=1;
DELAY();
SCL=1;
DELAY();
SDA=0;
DELAY();
SCL=0;
}

void STOP()
{
SCL=1;
DELAY();
SDA=0;
//DELAY();
SDA=1;
DELAY();
SCL=0;
}

void SEND_DATA(int x)
{
 int Z,i,k;
Z=x;
k=x;
for(i=0;i<8;i++)
{
Z&=(0x80);
if(Z==0x80)
{
SDA=1;
SCL=1;
DELAY();
SCL=0;
}
else
{
SDA=0;
SCL=1;
DELAY();
SCL=0;
}
k<<=1;
Z=k;
}
while(SDA==1);
SCL=1;
DELAY();
SCL=0;
SDA=0;
 
}


void SEND_DATA1(int x)
{
 int Z,i,k;
Z=x;
k=x;
for(i=0;i<8;i++)
{
Z&=(0x80);
if(Z==0x80)
{
SDA=1;
SCL=1;
DELAY();
SCL=0;
}
else
{
SDA=0;
SCL=1;
DELAY();
SCL=0;
}
k<<=1;
Z=k;
}

}

void GET_DATA()
{
        unsigned char i,Data=0X00;
        for(i=0;i<=8;i++)
		{
              //  SCL = 0;
                SCL = 1;
				DELAY();
                Data|=SDA;
                Data<<=1;
				SCL=0;
			//	SCL=1;
        }
    //   SCL = 0;
        SDA = 1;
        SCL=1;
		DELAY();
		SCL=0;
		
		P2=Data;
		SDA=0;
}
/*
void ACK()
{
while(SDA==1);
SCL=1;
DELAY();
SCL=0;
}

void NAK()
{
SCL=1;
DELAY();
SDA=1;
DELAY();
SCL=1;
DELAY();
SCL=0;

}

void MACK()
{
SDA=0;
SCL=1;
DELAY();

SCL=0;
}
*/
 

Attachments

Last edited by a moderator:

dean_winchester

Advanced Member level 4
Joined
Oct 19, 2010
Messages
111
Helped
22
Reputation
44
Reaction score
21
Trophy points
1,298
Location
India
Activity points
1,826
one thing, you have left WP Pin floating.. may be that is the reason.. Check and reply. im looking into code.
 

anoop kr

Member level 4
Joined
Aug 6, 2010
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
kerala
Activity points
1,885
But the WP(Write Protect) pin is used for preventing the writing operation to EEPROM from certain level. The reading operaion is not related with that. When i did the same program in assembly, there is no problem. That time also i left the WP pin.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top