18F External EEPROM Example

External 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.
Like most
peripherals, I've made my own
Swordfish Library for
the 24LC series EEPROM's. Pins 1, 2, 3 set the
address of the EEPROM. As you can see in the diagram below, the
highest
24LC256 has one connection to earth (Pin 3), and two
connections to 5V (Pins 1 & 2).. this defines it as %011 (don't
forget that binary starts from the right!) on the I2C Bus.

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!
Here is an example of how to use the Library with a single EEPROM
(24LC256)
Device = 18F452
Clock = 20
// import libraries...
Include "ExtEEPROM.bas"
Include "usart.bas"
Include "Convert.bas"
Dim
Variable As Byte, // Variable used for data
Device_Address As Byte, // Register for I2C Address reference
Memory_Address As Byte, // Register for EEPROM memory address
Family As Byte // Register for External EEPROM family setting
USART.SetBaudrate(br19200)
Variable = 123 // Place the value "1" in the variable register
Device_Address = %000 // Set the EEPROM I2C address
Memory_Address = 10 // Set the EEPROM address location to 0
Family = %1010 // Set the Family configeration as the binary number 1010
// Write the value 123 to EEPROM 000 at memory location 10
ExtEEPROM.Write(Variable, Memory_Address, Device_Address, Family)
// Read from EEPROM 000, memory location 10
Variable = ExtEEPROM.Read(Memory_Address, Device_Address, Family)
USART.Write(Convert.DecToStr(Variable))
While True
Wend
Video Tutorials:
Download Spency's
External EEPROM Library
Link:
What is a Swordfish
Library?
Where you can get the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
|  | 18F PIC Examples |
| |  | 7 Segment Displays |
| |  | 7 Segment Displays |
| |  | RS232 and UART |
| |  | Code Snippets |
|  | Handy Tips |