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
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')
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;
}
send_to_mem(0, 'R') // save to addres 0
send_to_mem(1, 'O') // save to addres 1
send_to_mem(2, 'M') // save to addres 2
send_to_mem(3, 'E') //save to addres 3
send_to_mem(4, 'L') // save to addres 4
the memory gets incremented automatically and you just have to write the data successively...
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
}
24C04 is 4kB EEPROM according to datasheet.
4kB = 4096 = 15bits of addresses right?
so the address would be 0x000 to 0x2FFF ??
organized with 32 pages of 16 bytes each, the 4K require a 9bit data word address for random word addressing
The datasheet says
This is a 4Kbit eeprom, not 4KByte, it can store 512 bytes, the addresses start from 0 which means that the range is 0-511, 0x000-0x1FF , it is a 9 bit address.
Alex
or just keep writing as you are implementing sequential write and not random write.... i think you can keep writing the data without any problem...
You can only write sequentially until you reach the end of the page, each page is 16bytes, if you reach the end of the page you have to start a new write and send the address again.
The page boundaries are 0-15, 16-31 etc until the end of the memory space, if you start writing from position 10 then you can only write sequentially 6byte (10-15) before you need to send the address again.
Alex
from 0 - 32 pages and each page is 16Bytes.
so address 00 is 16bytes right? 32*16 = 512 all in all
Im so much confused of sequential writing.. heheWhat are you confused about?
page decimal hex
-------------------------------------------------------------
1 0-15 000-00F
2 16-31 010-01F
3 32-47 020-02F
4 48-63 030-03F
5 64-79 040-04F
6 80-95 050-05F
7 96-111 060-06F
8 112-127 070-07F
9 128-143 080-08F
10 144-159 090-09F
11 160-175 0A0-0AF
12 176-191 0B0-0BF
13 192-207 0C0-0CF
14 208-223 0D0-0DF
15 224-239 0E0-0EF
16 240-255 0F0-0FF
17 256-271 100-10F
18 272-287 110-11F
19 288-303 120-12F
20 304-319 130-13F
21 320-335 140-14F
22 336-351 150-15F
23 352-367 160-16F
24 368-383 170-17F
25 384-399 180-18F
26 400-415 190-19F
27 416-431 1A0-1AF
28 432-447 1B0-1BF
29 448-463 1C0-1CF
30 464-479 1D0-1DF
31 480-495 1E0-1EF
32 496-511 1F0-1FF
how about if I will write a value of 1000 in address 000 of memory that is more than 8 bits. how will I transfer that to address 000 of my memory?
I will make it a chunk of 8bits?
The datasheet says
This is a 4Kbit eeprom, not 4KByte, it can store 512 bytes, the addresses start from 0 which means that the range is 0-511, 0x000-0x1FF , it is a 9 bit address.
Alex
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?