PJRC.COM Offline Archive, February 07, 2004
Visit this page on the live site

skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: 8051 Tools Code Library Serial EEPROM Search PJRC

PJRC Store
8051 Dev Board, $79
LCD 20x2 Display, $11
Serial Cable, $5
12 Volt Power, $8
More Components...
8051 Tools
Main Page
Software
PAULMON Monitor
Development Board
Code Library
89C2051 Programmer
Other Resources

Serial EEPROM (93C46 / 93CS46) Routines

Don't like reading docs, why not just Skip Down to the Code?

Contents


What is it, Why use a serial EEPROM?

Serial EEPROMs are small Electronically Erasable Programmable Read Only Memory chips. These devices are usually used to store user configurable parameters and device serial numbers. They use a serial bus interface, which allows them to be packaged in inexpensive 8 pin packages.

To you, the 8051 system designer, these little chips offer a nifty way to store a small amount of data in non-volatile memory, using only a few of the port pins, and without raising the system's cost much. They are usually specified to retain the data for 10 years and to endure 100,000 write operations before failure. They only require a 5 volt power supply (some 3V only versions exist too).

Because these chips use a serial interface, they can not be read quickly enough to serve as conventional memory. In addition, a considerable length of time (milliseconds) is required to perform a write operation. They typically hold less than 1024 bytes of memory. However, they usually cost less than $1.00 (US) for single piece quantities, making them pretty desirable memory devices for storing configuration parameters or other bits of information that should be retained when the power is lost.

Types of Serial EEPROM Chips

There are several types of Serial EEPROMs, but most of them fall into either a 2-wire or 3-wire interface category. Usually, the 3-wire devices require an addition wire (beyond the 3 for data transfer) for each chip to be used. The 2-wire interface, called I2C or IIC or "I squared C" uses only two wires, regardless of how many chips are attached. I2C is a trademard of Philips. The three wire interfaces include SPI and Microwire, which is a trademark of National Semiconductor.

As the pressure on engineers to make products smaller has grown, semiconductor manufacturers have introduced several new interfaces, usually aimed at lower a product's size and cost... and undoubtedly many more will appear in the future.

As a practical matter, the code offered below only works with the Microwire 3 Wire interface, and is specifically intended to work with the 93C46, which is a standard part available from a variety of distributors.

Where to get a Serial EEPROM?

The code below works with the 93CS46 Serial EEPROM. It can be used without much difficulty with a 93C46, by avoiding calls to the routines that manipulate the extended features of the 93CS46.

Manufacturers

Atmel has data sheets on-line for most of their EEPROM and microcontroller products. They have also added example 8051 code similar to the code below, as well as for I2C parts they offer.

National Semiconductor has lots of data sheets on-line. I used their 93CS46 data sheet to write the code. Their literature number is 800-272-9959... or at least is was some time ago when I put their sticker on my phone.

MicroChip now has data sheets on-line for most of their products. They offer the 93C46, as well as a variety of other EEPROMs.

SGS Thompson made the chips I used when I wrote this code. I used the National datasheet to write the code, but the SGS Thompson parts worked flawlessly. They offer quite a few different types of serial eeproms, including of course the 93CS46 and 93C46.

Xicor offers 2-Wire interface and SPI interface serial EEPROMs, but apparantly nothing which will work with the code below. Xicor once had a considerable collection of example code on-line, but they appear to have removed it.

Distributors

Within the United States, the easiest way to get ahold of a 93C46 is to call DigiKey 800-344-4539 (MicroChip, maybe National) or Mouser Electronics 800-346-6873 (SGS Thompson). Both have a minimum order, appox $25, and these serial EEPROMs are only about $1 each. There are lots of other distributors too, but Mouser and DigiKey are probably the easiest, since they give out a free catalog

What this 8051 family code does

This code was written to support the 93CS46. This 3-Wire interface chip actually has 6 wires. As mentioned above, four wires are actually required, and the 93CS46 has two additional lines that work together with a special write protection register. This code should work "out of the box" with a 93CS46 chip.

Compability with other 3-Wire serial EEPROMs

The 93C46 chip should be usable, but care should be taken not to make calls to the functions which access the protection register within the 93CS46. The two extra pins not required for the 93C46 are manipulated by all the routines. To reclaim these pins, remove all the instructions that use "pe" and "pre" symbols. A simpler approach may be to set the bit addresses of these bits in the .equ statements to a bit within the 16 byte bit-addressable space, or perhaps one of the general purpose flag bits in the PSW, if you're not using it. It's up to you. I might make another version of the code specifically for the plain 93C46, if anyone wants it enough to let me know. Please explain the difficulty you have using the code with a 93C46 (or the trouble you think you may have).

Early versions of this code worked with the 93C56 and 93C57, which have more memory. Unfortunately, these chips use an 8 bit address field, whereas the 93C46 uses only 6. The code can be adapted to work with these larger chips... good luck. The early versions are long since gone.. they were buggy anyways.

The code is basically designed to manipulate a single 93CS46 chip, connected to six of the port pins. Two versions of the code are available, one with a little menu driven user interface (via a terminal connected to the UART) and the other with only the routines to include in your existing program. The user interface is simple and shouldn't need documentation. The routines available for your code are:

Routines for both the 93C46 and 93CS46

READ
Reads the sixteen bit value from the location pointed to by R0. Data is returned in R2 (LSB) and R3 (MSB). This routine should always work regardless of calls to WEN and WDS, but it should not be called immediately following a call to PREN. Valid address range for R0 is 0 to 63.
WEN
Enable writing. This operation must be performed before a write may be issued. This applies to both the 93C46 and 93CS46. Unlike PREN below, writing should remain enabled until a WDS is called.
WRITE
Writes the sixteen bit data in R2 (LSB) and R3 (MSB) to the location pointed to by R0. Writing must have been enabled using WEN. For the 93CS46, if this location is "protected", no data will be written, regardless of calls to WEN and WDS. Writing takes a considerable length of time (milliseconds). This routine just waits for it to finish before returning... hope you don't have any polled I/O you wanted to keep doing!
WDS
Disable writing. This disables write operations. It applies to both the 93C46 and 93CS46 chips. It is advisable to disable writing if no additional write operations are expected soon. In this state, the chip may have better resistance to accidental data loss due to problems on the power supply, noise on signal lines, etc.

Routines specific to the 93CS46 chip

PREN
Enable the protection register. The next access should be to this register instead of the memory. This call is required immediately before each PRCLEAR, PRWRITE, or PRDS. In addition, WEN may have to have been called, check your data sheet (or just do it to be on the safe side). PREN should only enable use of the protection register for the next access, so it should be called before every protection register access.
PRCLEAR
Clear the protection register, allowing write access to all memory locations. Of course, WEN must still be called before writing.
PRWRITE
Write a value into the protection register. All memory locations before and including the value in R0 will become write protected. As with READ and WRITE, the address in RO refers to a 16 bit memory location, valid range is 0 to 63. Unprotected memory locations are still subject to WEN and WDS, but protected locations are unwritable unless the protection register is changed or cleared.
PRDS
Permenantly freeze the protection register to its current value. It can never be modified again after this operation. All protected memory locations will forever be unwritable, but the locations not protected will remain subject to WEN and WDS. The fact that you have frozen the protection register is stored in the non-volatile memory (10 year min retention). There is no way to undo this operation... all future PRCLEAR and PRWRITE operations will have no effect.
The menu-driven example code includes a copy of these routines and calls them properly to allow the user to view and edit the values within a 93CS46.

It is not necessary to erase memory locations before writing. The erase functions provided in the 93C46 are unnecessary (for the chips I've used, double check your data sheet). The erase functions are not implemented here.

Disclaimer

These 93C46 Serial EEPROM devices are used in many commercial products where the data contained within the chip, though only 128 bytes, is considered confidential and proprietary. This code library may not be used to "break into" the data stored within devices in commerical products. Use of this code library to modify the behavior of commercial products, without the expressed written authorization of the manufacturer, is also prohibited. (e.g. modifing ethernet card hardware addresses, etc)

Of course, I, Paul Stoffregen, give no warranty, expressed or implied for the software and/or documentation provided, including, without limitation, warranty of merchantability and fitness for a particular purpose.

On a lighter note, I have tried to provide you with a ready-to-run 93CS46 interface library, which I hope you will find useful as a design component in your own 8051 based projects/products. This code library is in the public domain. You are free to use it within your projects/products. You may use it within commercial for-profit products, without concern of licensing restrictions, copyright, royalties, etc. If you do use it, I hope that you'll give me some feedback.

Files to Download


Paul's 8051 Code Library, Paul Stoffregen
http://www.pjrc.com/tech/8051/serial-eeprom.html
Last updated: November 28, 2003
Status: complete
Suggestions, comments, criticisms: <paul@pjrc.com>