Writing variables to nonvolitile memory using xdata
My MCU is ADuC841 it had (working with SDK)
62 kBytes on-chip Flash/EE program memory 4 kBytes on-chip Flash/EE data memory
I want to store variable that i get from the serial port and write them to the EEPROM then turn off, turn on the MCU and read them some how (print the stored variable to screen = Hyperterminal) or using interrupt that prints it to screen.
my code is working on the the simulator of keil i see the memory window but on then target MCU it doesn't work
My code:
unsigned char xdata large_array[20] _at_ 1500; unsigned int xdata timer_tick; void INT_EX0() interrupt 0 { puts(large_array); } void main (void) { while(1) { T3CON = 0x87; // 4800 T3FD = 0x08; SCON = 0x52; IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2) EX0 = 1; // Enable EX0 Interrupt EA = 1; puts("\nEnter string :"); getline(large_array); // get string from serial port puts(large_array); // read from memory and output to serial port } }
the output of the MCU on Hyperterminal is something like all the char in ascii - "abcdefg...!@#$%^&*()..."
WHAT IS THE PROBLEM ??
Thanks in advance !!!
Mayer
You really have to start reading datasheets before writing code. The data flash on this device is not, repeat: NOT, accessed like xdata.
Hans thanks for your answer
I read the data sheet and there is about 6 sfr that handle with the data\program memory
My question is that - can i do wiritng to the EEPROM without all this registers by using XDATA or i must use those register (ECON,EADRH\L,EDATA1-4) ??
Exmple code from data sheet www.analog.com/.../ADUC841_842_843.pdf (page 34)
program tha data F3H into the second byte on Page 03H of the Flash/EE data memory space
ECON = 6; // Erase all memory EADRH = 0; // high byte EADRL = 3; // low byte ECON = 1; // Read Page into EDATA1-4 EDATA2 = 0x0F3; //overwrite the second byte ECON = 5; // ERASE Page ECON = 2; // WRITE Page ECON = 4; // VERIFY Page if (ECON == 0) // Check if ECON=0 (OK!) ERROR(); // fuction that handle the error
Is that the only way to program the EEPROM ???
How many other ways are listed in the Datasheet...?
My question is that - can i do wiritng to the EEPROM without all this registers by using XDATA or i must use those register
those amazing designers of chips just add all those SFRs because they think it is fun to do so.
Do you REALLY ASS U ME that all that is there "just to confuse you"?
Anyhow, there is, somewhere, a "manhandled" banking routine that makes an EE appear as a (painfully slow) bank of XRAM. Since I do not use it, my recollection is a bit fuzzy, but I think it is for an external EE so you will have to modify it to use and thus you must "use those registers".
Now I brought the subject up, I guess someone using it will locate it for you and, maybe, have some comments on it.
Erik
Andy, Hans
I read the datasheet as you said (i did it before i started the thread)
In the datasheet i saw that there is only one way to program the EE/FLASH data memory.
But this method of programing is very (relatively) complex. Using 6 sfr to program one char (i.e 'A') is too much in my opinion ?!?!?
My question is that :
All MCU that i want to program there EE/FLASH data memory is in that "fashion" ?? (using few sfr do to a little change in the EE/FLASH data memory)
Using an array of varibles in C (int for example) is very easy (opposed to ammebler to an array relatively is harder)
What is the advantges of "C" on assembler in this issue ?? (writing to the EE/FLASH data memory)
The solution for my first question (writing to the EE/FLASH data memory - with few changes)
http://www.keil.com/download/docs/184.asp
Using 6 sfr to program one char (i.e 'A') is too much in my opinion ?!?!?
Sorry to burst your bubble, but your opinion is entirely irrelevant in this regard. As is anybody else's, lest they happen to be MCU designers.
And BTW, writing a single char doesn't take 6 SFRs --- 4 are quite sufficient.
All MCU that i want to program there EE/FLASH data memory is in that "fashion" ??
How could anyone possibly answer that? Nobody but you knows what "All MCU that i want to program" means.
What is the advantges of "C" on assembler in this issue ??
None. So what?
"Anyhow, there is, somewhere, a 'manhandled' banking routine that makes an EE appear as a (painfully slow) bank of XRAM."
Yes: look in your Keil\C51\EXAMPLES\FarMemory folder - there are specific examples for accessing EEPROM on 80C51RD2 and T89C51RD2.
From the README:
"The 89C51RD2 EEPROM DEMO program shows you how to use the new features of C51 Version 6.20 and LX51 Linker/Locater Version 3.20 to access the data in the EEPROM area.
This files can be used as template for own projects."
Check if it's also suitable for your chip:
http://www.keil.com/support/man/docs/c51/c51_ap_xbanking.htm
"The far memory type allows you to address special memory areas like EEPROM space or strings in code banking ROM. Your application accesses these memory areas as if they are a part of the standard 8051 memory space. Example programs in the folder C51\EXAMPLES\FARMEMORY show how to use the C51 far memory type on classic 8051 devices. If an example that fulfills your requirements is not provided, you may adapt the access routines..."
Note that the LX51 linker/locator is required - this is not supported by BL51.
Andy, Hans thanks for your answers
How can i declare my EEPROM to be access like an array
I didn't succeed to do it with the FAR declaration as you said in the example - Keil\C51\EXAMPLES\FarMemory
My EEPROM is : (working with ADUC841)
The 4 kBytes of Flash/EE data memory are configured as 1024 pages, each of 4 bytes. (4 kBytes on-chip Flash/EE data memory)
my final purpose is to save some struct of char in the EEPROM and it can't be done (or you have some other good idea) with the solution that above (in the Thread)
Thanks,
"How can i declare my EEPROM to be access like an array"
If you want it to be directly accessed as any sort of standard 'C' variable - whether an array or something else - then you will have to use FAR - there is no other way.
"I didn't succeed to do it with the FAR"
Then you need to investigate the cause of your failure!
"my final purpose is to save some struct of char in the EEPROM"
Is it really?
Think about it: a struct is just one for the to program handle the data - it doesn't necessarily have to be stored in that way, does it?
A common way to handle this is to write a couple of functions: one to write to EEPROM, and one to read from EEPROM. As far as the functions (and the EEPROM) are concerned, they are just writing data - they neither know nor care whether the rest of the program treats it as a struct, or an array, or a float, or whatever...
For an analogy, think about writing data to files, and reading data from files...
"one to write to EEPROM, and one to read from EEPROM"
Which is exactly what this example does: