8051: writing to memory in the program

I'm learning the 8051 type microcontroller now and using the Atmel

89s52 microcontroller. There are 2kb of RAM and 8kb of flash memory.

I'd like my program to be able to write some data to memory while running, so these data can be remembered when power down and can be retrieved at power up.

Is there a way to do it? How do I do such a write with embedded c?

Thanks

Reply to
wanwan
Loading thread data ...

The AT89S52 does not have 2K RAM ? What I think you are seeking, is a self-pgm, or what is commonly called In Application Programming. AT89S52 cannot self-pgm, so you need to look at the AT89C51ED2, or go to

formatting link
and choose any device that has SelfPgmMemory column entry.

-jg

Reply to
-jg

In article , wanwan writes

It depends on which compiler you are using. When you write to flash the data stays after power down.

So all you have to do is store it to specific addresses in the 8K of flash. The syntax will be compiler specific.

The other minor point you have is knowing if it is a clean start or a re-boot. If a cold start you will have junk in the memory and you should probably zero it. However if it is a re-boot you will have valid data in the flash.

Normally you have a couple of bytes with values in to say cold or warm.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
/\/\/ chris@phaedsys.org      www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Reply to
Chris Hills

In article , -jg writes

It is the flash he needs to write to so the RAM is irrelevant here anyway,

Not at all. This is for updating the application in code space not storing data between powering down and up.

He is talking about DATA space not CODE space.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
/\/\/ chris@phaedsys.org      www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Reply to
Chris Hills

so does anyone know how I can read/write on the flash memory using Keil in C? the only information I found about it is an example code on the Keil site. With the example code , it shows memory write to idata, pdata, data, and xdata. I'm kind of confused by these variables. And it has no information about memory read.

(And oops, AT89s52 has 256 bytes of RAM, not 2kb)

Chris Hills wrote:

Reply to
wanwan

There are several ways to do it, the following are generic answers that may or not be applicable to your environment.

You can save the data to:

(A) Non-volatile memory. (typically FLASH or EEPROM, although other technologies are available.)

(B) Volatile memory (RAM), with back-up power to keep its contents intact while the rest of the system is unpowered.

And that memory can be:

(1) On-chip.

(2) Off-chip, on an external device.

(B-1) Volatile/on-chip. The simplest way - Some microprocessors have the capability to go into a low-power mode or shut down completely, while still providing power to the internal RAM. If following this approach, you need to make sure the RAM area you allocate for this purpose can not be overwritten or corrupted by the normal startup/ shut down processes.

(A-1) Non-volatile/on-chip Not so simple anymore. In most micros with internal FLASH memory you can program it under software control, but you can not access the contents of FLASH memory while it is being programmed.

That means you can not run the code that programs the FLASH from the FLASH itself. (Or at least, not from the sector that is being programmed) If all you have is one big sector of FLASH memory, the programming code needs to be copied to internal RAM and run from there. Interrupts need to be disabled while programming if there are ISRs using vectors and/or code in the FLASH memory

This problem disappears, of course, if you have both FLASH and EEPROM and use the EEPROM for data storage.

Two additional caveats: (1) You can not erase/reprogram individual bytes in FLASH memory, only whole sectors. To change only a few bytes you will have to save the contents of the whole sector in RAM, or keep a spare FLASH sector copying/modifying into it. (EEPROM also allows erasure of individual bytes.)

(2) FLASH and EEPROM devices have a limit on how many times they can be erased. This limit tends to be lower in on-chip devices since the manufacturing process is not optimized for FLASH or EEPROM due to presence of other circuitry on the chip (CPU, RAM, etc.) It is very easy to exceed this limit with careless programming.

(B-2) Volatile/off-Chip - Simple as B-1. May be simpler, since you can buy memories with a built-in battery, or chips with both volatile and non volatile areas that can be copied one on top of the other as a single operation.

(A-2) Non-volatile/off-chip - With FLASH, no problems with "disappearing" sectors (Well, you still can not read from FLASH while programming, but the programming code could run from internal FLASH)

With EEPROM: A common solution for small amounts of data is to use a serial interface (I2C, SPI) to communicate with an EEPROM chip. They are easy to use, both hardware and software-wise.

The best solution for you will depend on what hardware is available in your system, how much data you want to store, etc.

Roberto Waltman

Reply to
Roberto Waltman

In article , wanwan writes

Yes. By not TOP posting. Other would have flamed you for that.

Then that is your answer..... Keil are very good with their support answers and examples.

Then you need to start with the basic 8051 architecture guide BEFORE you start to program ANYTHING.

Try

formatting link

Document number 4 is

formatting link

Which is a primer for the 8051 with the Keil compiler.

When you understand the 8051 hardware architect you will understand the multiple memory spaces this Harvard Architecture part has and therefore the various i,b,p,a, Data and CODE spaces it has.

Then the method of writing to flash will depend on the target 8051. With some you have to write byte at a time and some it is larger blocks.

After you have declared the variable, it's size and location etc you can just read it (and write to it if it is RAM.)

We know.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
/\/\/ chris@phaedsys.org      www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Reply to
Chris Hills

and chip specific, and of course, as I said before, the AT89S52 does NOT allow writes to FLASH, and has no EEPROM, so that's a brick-wall.

The OP needs to go to the link I gave before,

formatting link
and look at the SelfPgmMemory and EEPROM columns.

If the task he wants is EE_XData storage, then choose any device with EE ability, if it is CODE space he wants to write to, choose any device with SelfPgmMemory One with both, can do any mix of the two.

-jg

Reply to
-jg

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.