Sie sind auf Seite 1von 37

Chapter 8

USING FLASH AND


EEPROM MEMORIES FOR
DATA STORAGE

8.1 Semiconductor Memory


8.2 Erasing and Writing to Flash in the
PIC18F
8.3 Reading and Writing to Data
EEPROM in the PIC18

8.1 Semiconductor Memory


-types of semiconductor memories
- Characteristics such as capacity, organization, access
time
- For all microprocessor based systems ,
semiconductor memories used as primary storage for
code and data.
- semiconductor memories: connected directly to the
CPU and the memory that the CPU first asks for
information (code and data).
- Primary memory.
- Most widely used semiconductor memories RAM and
ROM.

Memory capacity
The number of bits that a
semiconductor memory chip can
store is called chip capacity
Unit = Kbits (kilobits), Mbits (megabits)
Different from storage capacity of
computer systems
Memory capacity of a memory IC chip
= bits
Storage capacity of computer system
= bytes

Memory organization
Memory chips organized into a number of locations
within the IC
Each location can hold 1 bit, 4 bits, 8 bits or even 16 bits.
The number of bits that each location within memory
chip can hold = number of data pins on the chip.
How many location exist inside a memory chip? Depends
on the number of address pins.
The number of locations within a memory IC always
equal 2 ^number of address pins.
The total number of bits that a memory chip can store =
number of locations X number of data bits per location.

Memory chip contains 2x location


where x = number of address pins
Each location contain y bits, y =
number of data pins on the chip
The entire chip will contain
2x x y bits
Where x = number of address pins
y = number of data pins

Speed
Most important characteristic of memory chip =
speed at which its data can be accessed.
To access the data, the address is presented to the
address pins, the READ pin is activated, and,
after a certain amount of time has elapsed, the
data shows up at the data pins.
The shorter this elapsed time, the better. the
more expensive the memory chip.
The speed of the memory chip is commonly
referred to as its access time. It varies from a
few nanosecond to hundreds of nanosecond.

Example
1) Memory chip has 12 address pins
and 4 data pins. Find:
(a)The organization (b) the capacity
2) A 512K memory chip has 8 pins for
data. Find
(a) The organization (b) number of
address pins for this memory chip.

Types of memory
1) ROM (read Only Memory)
2) PROM (Programmable ROM) and OTP
3) EPROM (Erasable programmable
ROM) and UV-EPROM
4) EEPROM (Electrically Erasable
Programmable ROM)
5) Flash Memory EPROM
6) Mask ROM

1) RAM
2) SRAM
3) NVRAM (Nonvolatile RAM)
4) DRAM(Dynamic RAM)

8.2 Erasing and Writing to Flash in the PIC18F

8F has three types of memory


M
h
ROM

for general purpose usage


OM use for storing data
memory - used primarily to storage program (code).
o for storing fixed data such as look up table

o write to Flash memory.


D instruction to read the fixed data stored in program Flash

2 ways to write info to the Flash or erase its


content.
(a) using external Flash programmer (burner)
such as PICSTART
(b) using instruction such as TBLWR.
How to use TBLWR instruction to write to Flash
memory?
TBLRD and TBLWR have similarities. So need to
understand TBLRD first.
TBLRD read data stored in the Flash ROM.

TBLRD
How to access fixed data residing in
the program ROM spaceof PIC18.
First learn how to store fixed data in
the program ROM space using DB
(define byte) directive.

DB and fixed data in


program ROM
The DB data directive is widely used to
allocate ROM program (code)memory
in byte sized chunks.
DB used to define a 8 bit fixed data

Reading Table elements in the PIC18


TBLPTR = 21 bit register- used to point to the byte
to be fetched.
Problem: do not have instruction to load the 21 bit
address into TBLPTR.
TBLPTR divide into three 8 bit parts.
(TBLPTRL, TBLPTRH and TBLPTRU)
TABLAT (Table LATch) reg is used for keeping the
byte once it is fetched into the CPU.
We only load TBLPTRL and TBLPTRH register
because the table elements are stored in the first
64K of the PIC18 address space (0000-FFFFH).

Program example : Assume the word USA is burned into ROM location
starting at 500H, and that the program is burned into ROM location
starting at 0. Analyze how the program works and state where USA is
stored after this program is run

HERE

ORG 0000H
CLRF TRISB
MOVLW 0X00
MOVWF TBLPTRL
MOVLW 0X05
MOVWF TBLPTRH
TBLRD*
; TABLAT = U char pointed to by TBLPTR.
MOVFF TABLAT,PORTB
INCF TBLPTRL,F ; TBLPTRL =01 pointing to next (501)
TBLRD*
; TABLAT =S char pointed to by TBLPTR
MOVFF TABLAT, PORTB
INCF TBLPTRL, F ; TBLPTRL =02 pointing to next (502)
TBLRD*
;TABLAT =A char pointed to by TBLPTR
MOVFF TABLAT, PORTB
GOTO HERE

Using TBLWR to write data


to Flash
TBLRD and TBLWR has major
similarities.
TBLRD
TBLWR
TBLPTR as pointer to the TBLWR instruction
data in Flash
write data held in the
TABLAT register to the
Flash ROM location
whose address is
pointed to by the
TBLPTR register
TABLAT register as a
temporary place to store
the data fetch from
Flash.
Autoincrement /autodecrement : TBLWR and
TBLRD instructions are exactly the same

PIC18 Table Write


Instruction
Instruction

Function

Description

TBLWT*

Table Write

After write, TBLPTR


stays the same

TBLWT*+

Table Write with postinc

Write and increment


TBLPTR

TBLWT*-

Table Write with postdec

Write and decrement


TBLPTR

TBLWT+*

Table Write with pre inc

Increment TBLPTR
and then write

Major different between TBLRD and


TBLWR
TBLRD

TBLWR

Read individual bytes from Flash

Write a block of 8 bytes to Flash

Read one byte at a time from the


Flash into the TABLAT registers
(which means we must save the
contents of the TABLAT before the
next read wipes it out)

Use concept of short write and


long write to write to Flash.
Short write : use TBLWR
instruction to write a block of 8
bytes of data into 8 TABLAT
registers one byte at a time.
After short write is done, we use
the long write to actually store
(write or burn) the entire block of
8 bytes into the Flash.
Long write is done with the help
of a register called EECON1.
(EECON1 use for both Flash and
EEPROM memory)

Contrast between Flash and EEPROM


Flash write or erase process is done on a block of data.
EEPROM write or erase one byte at a time
(byte accessible memory)
Readings for both Flash and EEPROM memories are in byte
size.
The block size for Flash memory varies among the Flash
memories depending on the size and intended application.
The block size for write/erase in the PIC18F4580 = 8 bytes.
Other Flash memories = 64 or 256 bytes.
The breaking of PIC18F Flash into blocks of 8 bytes means
the memory addresses must be on the byte boundaries.

EECON1 (EEPROM Control Registers, also used for Flash)

Flash memory 8 byte


boundaries

Although there are 8 TABLAT registers


for the short write, they are not
accessible individually.
These 8 TABLAT registers are internal
and are used for the purpose of the
short write using TBLWRT instruction.

Comparison between Reading


/Writing from/to Flash

Reading from Flash

Writing to
flash

The write will not initiate if the above


sequence is not exactly followed
(write 55h to EECON2, write 0AAh to
EECON2, then set WR bit) for each
byte.

Steps in writing to Flash


memory
Assuming that an area of Flash memory is erased, we
can use the following steps to write a block of 8
bytes of data to the Flash memory
1) Load the TBLPTR registers with the address of the
first byte being written
2) Using the TBLWR instruction, write 8 bytes of data to
the TABLAT registers one after another. This is the
end of the short write
3) Set the EECON1 register for the write operation by
setting (a) EEPGD =1, (b) CFGS =0, and (c) WREN
=1

4) Disable all interrupts globally with GIE =0


5) Write 55H to the EECON2 dummy register. This is the
start of a long write.
6) Write AAH to the EECON2 dummy register
7) Set WR# to 1 with the instruction EECON1bits.WR =1.
With WR =1, the write cycle begins.
8) It will take about 2ms to finish writing the 8 bytes to Flash.
During this write cycle, the CPU is stalled and will not allow
fetching any opcode. Upon completion of write cycle, the
WR# bit will go back low automatically to indicate that the
write cycle is finished. This step concludes the end of longwrite cycle.
9) Reenable the interrupt globally with INTCONbits.GIE =1

Steps in erasing Flash


memory
Although we can use external Flash
programmers to erase the Flash
memory contents, the PIC18 allows us
to write a program to erase the Flash
memory.
The erasure process works on blocksize, not byte size data.
The minimum block size for the erasure
is 64 bytes.

Use the following steps to erase a


single 64 byte block of Flash memory
1) Load the TBLPTR registers with the address of the block being
erased.
2) Set EECON1 register for the erase operation by setting (a)
EEPGD = 1 (b) CFGS= 0 (c) WREN =1 and (d) FREE =1.
3) Disable all interrupts globally using INTCONbits.GIE = 0
4) Write 55H to the EECON2 dummy register.
5) Write AAH to the EECON2 dummy register.
6) Set WR# to 1 with the instruction EECON1bits. WR=1. With WR
=1, the erase cycle begins.
7) It will take about 2ms to finish erasing the block of 64 bytes.
During this erase cycle, the CPU is stalled and will not allow
fetching of any opcode. After complete erase cycle, WE become
HIGH automatically (indicate erase cycle is finished)
8) Reenable the interrupts globally using INTCONbits.GIE =1.

Example Program
Write the message GOOD BYE to Flash
memory starting at location 400H.
And
Reads the data from flash and places
in PORTB one byte at a time.

COUNT EQU 0X20


MOVLW 0X00
MOVWF TBLPTRL
MOVLW 0X04
MOVWF TBLPTRH
;Start a short write
MOVLW AG
MOVWF TABLAT
TBLWT*+ ; Perform short write increment address
MOVLW AO
MOVWF TABLAT
TBLWT*+ ; Perform short write increment address
MOVLW AO
MOVWF TABLAT
TBLWT*+ ; Perform short write increment address

MOVLW AD
MOVWF TABLAT
TBLWT*+
; Perform short write increment address
MOVLW A ; Load the space into working register
MOVWF TABLAT
TBLWT*+
; Perform short write increment address
MOVLW A B
MOVWF TABLAT
TBLWT*+
; Perform short write increment address
MOVLW A Y
MOVWF TABLAT
TBLWT*+
; Perform short write increment address
MOVLW A E
MOVWF TABLAT
TBLWT*+
; Perform short write increment address

;start a long write cycle


MOVLW 0X00
MOVWF TBLPTRL
MOVLW 0X04
MOVWF TBLPTRH
BSF EECON1,EEPGD
BCF EECON1, CFGS
BSF EECON1,WREN
BCF INTCON,GIE
MOVLW 55H
MOVWF EECON2
MOVLW AAH
MOVWF EECON2
BSF EECON1,WR
; Now write to Flash
NOP
BSF INTCON,GIE
BCF EECON1,WREN

;Read the back one byte at a time and examine the bytes on
PORTB
MOVLW 0X00
MOVWF TBLPTRL
MOVLW 0X04
MOVWF TBLPTRH
CLRF TRISB
MOVLW 0X8
MOVWF COUNT
OVER TBLRD*+ ;Read the byte to TABLAT and increment
MOVFF TABLAT, PORTB
CALL DELAY
DECF COUNT,F
DECFSZ COUNT,F
BRA OVER

C version
#include <p18Cxxx.h>
void Delay(unsigned int itime);
void main()
{
unsigned char x;
//write to program memory
TBLPTR = (short long)0x0400;
TABLAT = G
_asm TBLWTPOSTINC _endasm
TABLAT = O
_asm TBLWTPOSTINC _endasm
TABLAT = O
_asm TBLWTPOSTINC _endasm
TABLAT = D
_asm TBLWTPOSTINC _endasm

//short write
//short write
//short write
//short write

TABLAT =
_asm TBLWTPOSTINC _endasm
TABLAT = B
_asm TBLWTPOSTINC _endasm
TABLAT = Y
_asm TBLWTPOSTINC _endasm
TABLAT = E
_asm TBLWTPOSTINC _endasm
//LONG WRITE
TBLPTR = (short long)0x0400;
EECON1bits.EEPGD =1;
EECON1bits.CFGS =0;
EECON1bits.WREN =1;
EECON1bits.GIE =0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR=1;
_asm NOP _endasm
INTCONbits.GIE = 1;
EECON1bits. WREN = 0;

//short write
//short write
//short write
//short write

//read from program memory send to PORTB


TBLPTR = (short long) 0x0400; //reload
TBLPTR
for ( x=0;x<8;x++) {
_asm TBLRDPOSTINC _endasm
PORTB = TABLAT;
Delay (250);
}
}

Das könnte Ihnen auch gefallen