romel_emperado
Advanced Member level 2
- Joined
- Jul 23, 2009
- Messages
- 606
- Helped
- 45
- Reputation
- 132
- Reaction score
- 65
- Trophy points
- 1,318
- Location
- philippines
- Activity points
- 6,061
Hi, im working with 24c04 eeprom I have done already a working code but I cannot understand exactly about the terms of addresses..
I just did the code with trial and error method and it worked but I want to know exactly the right approach..
24C04 is 4kB EEPROM according to datasheet.
4kB = 4096 = 15bits of addresses right?
so the address would be 0x000 to 0x2FFF ??
My doubts is I don't know what is the proper addressing of vacant memory in my eeprom..
as you can see in my code in saving data
This is my code:
I just did the code with trial and error method and it worked but I want to know exactly the right approach..
24C04 is 4kB EEPROM according to datasheet.
4kB = 4096 = 15bits of addresses right?
so the address would be 0x000 to 0x2FFF ??
My doubts is I don't know what is the proper addressing of vacant memory in my eeprom..
as you can see in my code in saving data
i will just do like this to save sometingvoid send_to_mem(unsigned char s_address, unsigned char s_data)
and when I want to read I just retrieve it to address 1 where i placed the character 'y' ..send_to_mem(1, 'y')
This is my code:
PHP:
void send_to_mem(unsigned char s_address, unsigned char s_data)
{
start_s_eeprom(); // sending start condition to eeprom
send_byte_s_eeprom(0XA0); // A0 = 10100000 = sending device address word for write
acknowledge();
send_byte_s_eeprom(s_address); // sending data address
acknowledge();
send_byte_s_eeprom(s_data); // sending data
acknowledge();
stop_s_eeprom(); // sending stop condition to eeprom
// acknowledge();
}
// fxn to get the data back frm the serial eeprom
// just give the adress from where the data is to be retrieved
unsigned char get_from_mem(unsigned char s_address)
{
unsigned char i;
//-------dummy write seq----+ word address------------------------------------
start_s_eeprom(); // sending start condition to eeprom
send_byte_s_eeprom(0XA0); // sending A0 = 10100000 = device address word for write
acknowledge();
send_byte_s_eeprom(s_address); // sending data address
acknowledge();
//----------------dummy over----------------------------------------------------
start_s_eeprom();
send_byte_s_eeprom(0XA1); // sending A1 =10100001 = device adress word for read
acknowledge();
i = get_byte_s_eeprom(); // sending data
noacknowledge();
stop_s_eeprom(); // sending stop condition to eeprom
return(i);
}
/* fxn to transmit a byte to the eeprom
this fxn just send the 8 bits serialy on the SDA line
just pass the byte to be transmitted as parameter to this fxn */
void send_byte_s_eeprom(char s_byte)
{
//unsigned char temp ;
char i ;
for(i = 7 ; i >= 0 ; i--) //scan data do be place in SDA
{
/* note SCL is low during transitions on SDA */
if( ((s_byte >> i) & 0x01) == 0) //check individual bits
sda = 0;
else
sda = 1;
sclk = 1;
wait();
sclk = 0;
}
}
// fxn to receive 8 bits serialy from sda line
// this is not a fxn to read from eeprom
// it just receives 8 bits serialy and retuns the byte received to the calling fxn
unsigned char get_byte_s_eeprom()
{
char temp, temp_h, i;
temp = 0;
temp_h = 1;
sda = 1; // making SDA as input pin for microcontroller
sclk = 0;
for(i = 7; i >=0 ; i--)
{
sclk = 1;
if(sda == 1)
{
temp = temp | temp_h<<i ;
}
wait();
sclk = 0;
}
sclk = 0;
return(temp);
}
// fxn to send the start condition
void start_s_eeprom()
{
sda = 1;
sclk = 1;
wait();
sda = 0;
sclk = 0;
}
// fxn to send stop condition
void stop_s_eeprom()
{
sda = 0;
sclk = 1;
wait();
sda = 1;
sclk = 0;
}
// fxn for acknowledging the eeprom
// this fxn actualy does not read the acknowledge signal
// it just waits for sufficient time and assumes that the eeprom has given tha ack by the time the wait gets over
void acknowledge()
{
sda = 0;
sclk = 1;
wait();
sclk = 0;
}
// a small delay fxn to ensure the line settles down after transition
void wait()
{
//char i;
//for(i=0;i<=20;i++)
//i++;
}
void noacknowledge()
{
sda = 1;
sclk = 1;
wait();
sclk = 0;
}
Last edited: