Read/Write on EEPROM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Read/Write on EEPROM

4,139 Views
jesortsan
Contributor I
Hi,

I'm working with MC9S08DZ60 and I have a problem that I can't solve because I don't find the information in the data sheet (MC9S08DZ60.pdf). On page 52, paragraph 4.5.3. "Program and erase comand execution", the first step says "Write a data value to an address in the FLASH or EEPROM array". How can I do this with CodeWarrior and C code (not assembly)?

For example, if I want to write the data 0x33 in the EEPROM address 0x1650, I have tried the following:

0x1650 = 0x33;

But when I compile, the application goes into error.

Other question for you is: how can I read directly from an EEPROM address?

I know this issues are quite simple, but I'm starting to program microcontrollers and those kind of troubles are the worst for me, because they don't appear in the datasheet and I can't solve them by reading the documentation.

Thanks

Regards
Labels (1)
0 Kudos
Reply
3 Replies

566 Views
peg
Senior Contributor IV
Hi jesortsan,
 
It is not the job of the device's datasheet to teach you how to implement the operation being descibed in any particular language. Its job is to tell you "to do this put this in there or set this bit here" any more information only serves to obfuscate.
Also often these very hardware specific, low level tasks are not very well catered for in high level languages which then makes their implementation compiler specific as well.
 
So, especially when you are new at this, when you need to know to do something, check the datasheet. When you need to know how to implement it in the language being used, check the manuals/ books for the language. When you need to know how to implement the languge check the complier/etc manuals.
 
As you become more experienced steps two and three are rarely used and even step 1 mainly only is necessary when changing between different devices.
 
Trying to put all the information into one datasheet may seem benificial for beginners, but would make it more troublesome for the great majority.
 
That is what "Quick Start Guides" and the like are for!
 
 

Message Edited by peg on 2007-03-1710:03 AM

0 Kudos
Reply

566 Views
CompilerGuru
NXP Employee
NXP Employee
writing
1=0;


tells the compiler to change the meaning of the number 1 to be zero, well, no compiler can really do that Smiley Wink.
Instead you have to take the address, cast it to a pointer and dereference it.
E.g.
*(volatile unsigned char*)1= 0;


As this looks a bit ugly, I would hide it behind a macro:
#define WRITE_BYTE_TO_ADDRESS(addr, value) (*(volatile unsigned char*)(addr)= (value))

WRITE_BYTE_TO_ADDRESS(1,0);

Or use a pointer to the byte:
const volatile unsigned char* ptr_to_one= (const volatile unsigned char*)1;
*ptr_to_one= 0;


BTW: If you expect some side effects from such accesses, use volatile as I did. Just to get it written, volatile is not mandatory.

Reading is the same, just take the part from the left size of the = and place it on the right.
unsigned char t;
t= *(volatile unsigned char*)1;
0 Kudos
Reply

566 Views
jesortsan
Contributor I
Thank you very much for your help.
0 Kudos
Reply