Continue to Site

Welcome to EDAboard.com

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

[SOLVED] Questions about EEPROM.

Status
Not open for further replies.

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
void send_to_mem(unsigned char s_address, unsigned char s_data)
i will just do like this to save someting
send_to_mem(1, 'y')
and when I want to read I just retrieve it to address 1 where i placed the character '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:

maybe you have to understand the I2C protocol for that case which has frame format for read and write of data to eeprom or slaves..

write routine:

1. start
2. slave device address (0xA0) .. in your case of eeprom
3. Ack -> from slave
4. Slave memory address ( from the location you want to write)
5. Ack for every byte od data you write into memory
6. Stop bit ( to stop writing the data)

read routine:

1. Start bit
2. Slave device address -> in write mode ( 0xA0)
3. Ack
4. slave memory address from where you have to read or the initial memory address where you stored data)
5. Ack
6. Slave device address -> in read mode (0xA1)
7. Ack
8. read all the data you have stored.
9.Ack for every byte of data _> from master
10. stop reading and stop the process...
 
hi ckshivaram,

yes I already done that, that is why the code above is done because I followed that steps..

Although I can save and retrieve data from my eeprom but Im very confused in saving the data to my eeprom if it is the efficient way.

let say for example I will save 'R' 'O' 'M' 'E' 'L'

does it mean i will save it like this?
PHP:
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

This is working according to my simulation
I save the data from keyboard via uart
"ROMEL" is save temporality in a array then after the uart dected the enter key from user it will now save in the eeprom like the way posted above.. its working but I dont know if that is the proper saving..
 

you dont have to increment the memory address or pass the memory address everytime.. the increment is done automatically.. So you have to give only the starting address of the memory.... there is no problem in passing the memory location manually, but since the device takes care of incrementing the user need not do it...

the memory gets incremented automatically and you just have to write the data successively...
 
the memory gets incremented automatically and you just have to write the data successively...

yes.. it now clear.. it is stated also in the datasheet..

Please take a look with my simple writing function.



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

    
 }


so if I will edit this funtion let say I want to single write to fill the 4KB of memory I will just give the starting address and continue writing without issuing stop command until I reached the boundary of 4KB.. Am I correct?
sorry I just want to learn EEprom how it woks..
send_to_mem(1, msg[x]);
 

yes you have to keep writing the data till you reach the end of memory address or keep the string or character data in an array and pass it as a continuously...

you you should keep track of how much data you write or else after once it reaches the top most or the last location it returns to 00 or first location and over writes the data....

If any clarification please come back... hope i answered you properly...
 
24C04 is 4kB EEPROM according to datasheet.

4kB = 4096 = 15bits of addresses right?

so the address would be 0x000 to 0x2FFF ??

The datasheet says
organized with 32 pages of 16 bytes each, the 4K require a 9bit data word address for random word addressing

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
 
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


thanks so much.. I am about to ask this but you answered already..

but I dont understand what does the datasheet mean bout pages..

if I read that word the thoughts came to my mind is these

4bits - nibble
8bits - byte
16 - word
 
Last edited:

Sorry i overlooked your first post.. alex thanks for pointing it out..
24c04 is 4Kbits so i have 512 bytes (0x0200 addresses) and the highest address is 1`1111`1111 (0x01FF) .
24c04 will have 32 pages because you have to divide the 512bytes by 16bytes ................
 
so if I will write to EEPROM a value of 64 and the starting address is from 000 that is 0b0100 0000 in binary (8bits of data) then if you still write data, the device increment automatically the address as we all know. Then how about if I will write in the second time(assuming I just write 1byte in previous) I will initiate again a write command I know that address 000 was already filled with 64 in the previous write.

my question is:

Is the increment of address is just +1 from 000 then my second write would be started in 0x002?
 

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
 
The memory is just 1 byte in eeprom, so the address will increment by 1 everytime you write the data.... and you need to give address after every page....

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...
 
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...

I hope that you don't mean sequential write beyond the page boundries because it is not possible, you can only write one byte or one page (16 bytes), then you have to start a new write.

Alex
 
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



I don't know where I am confused.. :sad:
 

from 0 - 32 pages and each page is 16Bytes.
so address 00 is 16bytes right? 32*16 = 512 all in all

There are 32 pages, there is no page 0, there are 512 byte positions address 0x000 to 0x1FFF.
What are you confused about?
The pages are just a way to provide sequential writes, you can ignore it if you want and write random bytes to any position (send address and data).

Alex
 
What are you confused about?
Im so much confused of sequential writing.. hehe


anyways thanks so much guys I will just experiment in writing.. hehe

---------- Post added at 11:09 ---------- Previous post was at 11:05 ----------

last question..(I hope it's the last)

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?

---------- Post added at 11:12 ---------- Previous post was at 11:09 ----------

sorry if Im asking something weird..
 

You can either send the address+data every time you want to store a byte or you can use sequential write inside a page area.
You can write up to 16 bytes assuming that you start writing from the first byte of each page.
The pages are

Code:
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

Alex

---------- Post added at 12:28 ---------- Previous post was at 12:25 ----------

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?

You can only write 8bit values, if you want to write more you have to use more 8bit spaces but you have to send the data broken in 8bit values, like byteH and byteL in PIC registers

Alex
 
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


thanks so much... now clearer to me.

1 more doubt before I will mark this thread as solved.

i2c.GIF

write-i2c.png



according to the i2c protocol every after a byte transferred the slave will reply an acknowledge (0/Low).

So the question is How do I write to my slave(24c04) a 9bit address?
 
Last edited:

The datasheet says that for the 4K eeprom bit1 of the device address is the page address (think of it as bit8 of the memory address) and then you send the rest of the address bits7-0 and then the data

https://www.dzsc.com/uploadfile/company/56751/200711816370623.pdf
take a look at page 5, the A0 of the device address is replaced with bit 8 of the memory address (described as page address bit)
also check page 7, in the bit explenation in the start of the page , it is shown as P0, it is the MSB of the 8bit address

Alex
 
Maybe I can explain better with a picture, you can see where you have to write bit8 and then bits 7-0 of the 9bit address

Snap1.gif

Alex
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top