16F EEPROM Example

EEPROMS are great for storing data, and to interface with them is
not all that hard. Most use the I2C protocol. In this example
the
24LC256 is used. I2C communication is great as it can allow
serial data communication between many devices on a single 2 wire
bus. These two lines are referred too as SDA and SCL. Basically SDA
is the serial data and SCL maintains the clock/frequency in which
data is transferred. Don't be too concerned if you can't understand
the I2C examples for the
24LC256 below, we'll break it down.
Write example;

Read example;

Looks
like a bunch of 1's and 0's being smashed out right.. well lets
break it down;
- Control Byte: contains 8 bits.

-
"Device" is comprised of bit 4-7. Each
device (eg; EEPROMS/Digital Thermistors/other I2C devices)
have a device code. This is how the PIC communicates with
the correct device. The device code is specified in the
datasheet of your component. There will be a whole section
on how to interface with it via I2C. You may ask, what if 2
devices are on the bus with the same device code (eg; your
using 2 of the same EEPROM's on the same bus), answer,
assign the address for which device you want to talk to.
-
"Address" is comprised
within bits 3-1 of the control byte. Every I2C device has 3
pins on it labeled "A0, A1, A2". Tying them all to ground
will give the address of the device "000" or tying A1 too
+5V will give it the address of "010" on the I2C bus (while
A0 and A2 are tied to earth). This allows up to 8 different
devices with the same "Device" code to be on the same bus.
Handy. An example using 4 EEPROMS:
Pin 4 on
24LC256 = Vss (Earth)
Pin 8 on
24LC256 = Vdd (5V)
Note the PIC's power supply/oscillator are not shown
Click here to see this circuit in action!
All of the devices share the same device code (unchangeable),
but to define each ones address, simply set the address pins
(A2, A1, A0) to the address that you would like to refer to them
over the I2C bus. The upper 2 resistors are connected to the bus
and tie it to +5V, these are called 'pull-up' resistors, and
must be used on any I2C data bus, you now have 128K bytes of
storable memory.
- Finally R/W: This single bit is used to
control whether a read or write is going to be performed.
- Address High/Low Byte contains the data for
the address within the EEPROM you want to read/write. As each
device contains 32K x 8 (256 Kbit) of memory, the address of
each value can't be stored in a single byte. To access them all,
the address is broken up into 2 bytes, address high byte and
address low byte (eg 10000000 00000000 = byte 32768, 00000000
00000001 = byte 1 and 00000000 10000000 = byte 128 etc..). With
that in mind, its good practice to use a word(16 bit register)
to store the address.
Now to communicate with the
24LC256 via I2C.
Before each read or write, a 4
bit device code along with a 3 bit
address code and a
read or write bit must be sent (control byte).
The 4 bit device code for the
24LC256 is %1010 (found in the
datasheet), and we will just talk to the 1st EEPROM in the
diagram, who's address 001 (set via A2, A1, A0). With all that in
mind, the control bit we will send is 1010001x where x is a
1/0 depending if its a read/write.
This example shows how to interface to the EEPROM with address 001
and fill 20 bytes of data, then read it back;
Device = 16F876
XTAL 20
ALL_DIGITAL = True
Declare SCL_PIN PORTB.0
Declare SDA_PIN PORTB.1
Dim TX As PORTB.2
Dim DATA_OUT As Byte
Dim DATA_IN(20) As Byte
Dim Address As Word ' 16-bit address required
For Address = 1 To 20
DATA_OUT = Address
BStart ' Send the start command on I2C
' Send the byte to the EEPROM
BusOut %10100010,Address,[DATA_OUT]
BStop ' Send the stop command on I2C
DelayMS 5 ' Allow time for allocation of byte
Next Address
For Address = 1 To 20
BStart ' Send the start command on I2C
' Send the address to read
BusOut %10100010,[Address]
Brestart ' Send a restart command on I2C
' Grab data at the above address
BusIn %10100011, [DATA_IN[Address]]
BStop ' Send the stop command on I2C
Next Address
Stop
Note: After writing the data,
the data is then read from the EEPROM, it is all stored in the
array DATA_IN. Also take note that the register
Address
is actually a Word, this is so that the High Address byte and
Low Address byte are sent (a simple way to
combine the two
bytes).
Finally, you must
think of the PIC as the MASTER on the I2C bus. It tells
everything on the bus what to do. Nothing talks unless the
PIC asks it to. You control the bus, and to do so there are some
commands like BSTART/BRESTART/BSTOP to tell everything on the bus
when to listen up, and when to stop. These are shown above in
the example. You should read the datasheet on every device to see
how to communicate with it via I2C!! Some devices require unusual
setting up (eg, to initialize it on the I2C bus, you must send a
BSTOP, BSTART, BSTOP..), this is only on some devices, but its
definitely worth your time to check it out.
Where you can get the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
| |  | Motors |
| |  | Thermometers |
| |  | Code Snippets |
|  | 18F PIC Examples |
|  | Handy Tips |