Writing to EEPROM Memory

ukrainianlegalΗλεκτρονική - Συσκευές

2 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

125 εμφανίσεις

Objectives

To get familiar with using the EEPROM of PIC for permanent storage


Theory

EEPROM

An
EEPROM
(also called an
E
2
PROM
) or
E
lectrically
E
rasable
P
rogrammable
R
ead
-
O
nly
M
emory is a non
-
volatile storage chip used in computers and other devices to
store small
amounts of volatile (configuration) data.

Some microcontrollers use built
-
in (internal) EEPROM, while other microcontrollers don

t
have built in ones. Otherwise, they use external ones connected via parallel or serial
communication.

PIC18F4550
has 256 bytes of EEPROM memory locations on addresses from 00h to FFh
those can be written to or read from. The most important characteristic of this memory is that
it does not loose its contents during power supply turned off. That practically means that
what was written to it will be remaining even if microcontroller is turned off. Data can be
retained in EEPROM without power supply for up to 40 years (as manufacturer of
PIC18F4550 microcontroller states), and up to 10000 cycles of writing can be executed
.

EEPROM memory is placed in a special memory space and can be accessed through special
registers.

These registers are:


EEDATA
: which holds read data or that to be written.


EEADR
: which contains an address of EEPROM location being accessed.


EECON1:
which contains control bits.


EECON2:
The dummy register does not exist physically and serves to protect

EEPROM from accidental writing.


Lab 8

EECON1 Register
: is a control register with seven implemented bits, Bit 5 is not used.

Interpretation of EECON1

register bits follows.




bit 0
RD
(Read Control bit)

Setting this bit initializes transfer of data from address defined in EEADR to EEDATA
register.

Since time is not as essential in reading data as in writing, data from EEDATA can already be
used furth
er in the next instruction.

1=initializes reading

0=does not initialize reading

bit 1
WR
(Write Control bit)

Setting of this bit initializes writing data from EEDATA register to the address specified
trough

EEADR register.

1=initializes writing

0=does not
initialize writing

bit 2
WREN
(EEPROM Write Enable bit) Enables writing to EEPROM

If this bit was not set, microcontroller would not allow writing to EEPROM.

1=writing allowed

0=writing disallowed

bit 3
WRERR
(Write EEPROM Error Flag ) Error during writing

to EEPROM

This bit was set only in cases when writing to EEPROM had been interrupted by a reset
signal or

by running out of time in watchdog timer (if it's activated).

1=error occurred

0=error did not occur


Lab 8

INTCON Register:
Interrupt Control Regist
er

Bit 7
GIE (
Global Interrupt Enable).

0= Disable All Interrupts, no Interrupt is acknowledged, even if they are Enabled
individually.

1=Interrupts are allowed to happen.

PIR2 Register:
(Peripheral Interrupt Flag Register 2)

bit 4
EEIF
(EEPROM Write Opera
tion Interrupt Flag bit) Bit used to inform that writing
data to EEPROM has ended.

When writing has terminated, this bit would be set automatically. Programmer must clear
EEIF bit in his program in order to detect new termination of writing.

1=writing term
inated

0=writing not terminated yet, or has not started

Writing to EEPROM Memory

In order to write data to EEPROM location, programmer must first write address to EEADR
register and data to EEDATA register. Only then is it useful to set WR bit which sets the
whole action in motion. WR bit will be reset, and EEIF bit set following a wri
ting what may
be used in processing interrupts. Values 55h and AAh are the first and the second key whose
disallow for accidental writing to EEPROM to occur. These two values are written to
EECON2 which serves only that purpose, to receive these two values

and thus prevent any
accidental writing to EEPROM memory.

Therefore, it is very important to turn off interrupts which could change the timing needed for
executing instructions. After writing, interrupts can be enabled again.

Bit 6
CFGS
(Flash program/Dat
a EE or Configuration Select bit).

1= Access Configuration Registers

0=Access Program Flash or Data EEPROM memory

Bit 7
EEPGD
(Flash program or Data EEPROM Memory Select bit).

1= Access Program Flash memory

0=Access Data EEPROM memory


8

Steps in writing
to EEPROM

To write a byte of data to a location in the EEPROM memory, we go through the following
steps:

1.
Load the EEADR registers with the address of the EEPROM location we want to write the
data byte to.

2.
Load the EEDATA registers with the data byte
we want to write to EEPROM.

3.
Set the EECON1 register for the EEPROM write by making

(a) EEPGD=0, (b) CFGS=0, and (c) WREN=1.

4.
Disable all interrupts globally using
“BCF INTCON, GIE”.

5.
Write 55H to the EECON2 dummy register.

6.
Write AAH to the EECON2

dummy register.

7.
Set WR to 1 with the instruction

BSF EECON1, WR

, with the WR=1, the write cycle
begins.

8.
Upon completion of the write cycle, the WR bit will be cleared automatically to indicate
that the write cycle is finished.

9.
Re
-
enable the interrupts globally using
“BSF INTCON, GIE”.

10.
The WREN bit should be cleared to prevent an accidental write to the EEPROM by some
runaway program.

In the above steps notice the last one. It is recommended that WREN be turned off the whole
time except when writing data to EEPROM, so that possibility of accidental writing would be
minimal.

Reading from EEPROM Memory

Steps in Reading from EEPROM:

Reading a byte from the EEPROM memory is simple and straightforward as shown in the
following
steps:

1.
Load the EEADR register with the address of the EEPROM location we want to read from.

2.
Set the EECON1 register for the EEPROM write by making

(a) EEPGD=0, (b) CFGS=0, and (c) RD=1.

3.
Within the next instruction cycle, the PIC18 will automatica
lly fetch the data from the
EEPROM location and place it in the EEDATE register.

The only thing we have to do is to move data from the EEDATA register to a safe place
before we do another read.

Setting the RD bit initializes transfer of data from address
found in EEADR register to
EEDATA register. As in reading data we don't need so much time as in writing, data taken
over from EEDATA register can already be used further in the next instruction.



EEPROM Library

EEPROM data memory is available with a numbe
r of PICmicros. mikroBasic includes
library for comfortable work with EEPROM.

Library Routines


Eeprom_Read


Eeprom_Write





Lab 8

Lab Exercises

Part 1


Write assembly program to write 38 H to Location 12 H in EEPROM then output it to
PORTD.


Simulate the circuit using Proteus ISIS program.


Use the Debug tool in Proteus ISIS program to see the values written to the EEPROM & the

values written to EECON1, EECON2, EEADR, EEDATA, PIR2, and PORTD Registers.


Now, write a basic program that do the

same thing.

Part 2:


Write assembly program to send data from PORTC to EEPROM then output it to PORTD.


Simulate the circuit using Proteus ISIS program.


Use the Debug tool in Proteus ISIS program to see the values written to the EEPROM & the

values

of EECON1, EECON2, PIR2, and PORTD Registers.


Now, write a basic program that do the same thing.

Part 3:

Write assembly code to write numbers from 1 to 5 in EEPROM addresses 5,6,7,8 and 9
respectively.

Simulate the circuit using Proteus ISIS program.

Now, write a basic program that do the same thing.