Internal EEPROM

Internal EEPROM

When we write programs for microcontrollers, we usually do all the memory jobs on flash memory. Flash memory has very fast response time. It is like RAM on a computer. And it is volatile! Means that it is cleared when power is gone or after a cpu reset.

But sometimes one might need to bring back some important values like IDs, device parameters, sensor logs, after reset or circuit is powered. In these cases, we need a non-volatile memory area and it is very common to use EEPROMs.

EEPROM stands for Electrically Erasable Programmable Read Only Memory. It can be written or read electrically (in our case programmatically.) It is non-volatile! But it has slower response time.

There are serial and parallel(beatiful old days) versions of EEPROMs. You can find an EEPROM IC up to 1Mbit. If you need more storage area, it is better to use CF,SD,MMC,etc.

Internal EEPROM on Microcontrollers

External EEPROMs can be connected to microcontrollers by serial(SPI,I2C). But many microcontrollers has a built-in EEPROM area. It is usually a small area but enough for storing IDs, parameters, or long intervaled logs.

We are going to use PIC16F628 as example. The configuration registers and implementation may vary on other PICs. Read datasheets carefully when using different devices.

Configuration bits

To configure EEPROM module we use EEADR, EEDATA, EECON1, EECON2 and interrupt registers.

EEADR register:

This register holds the address of the memory. Only the first 7bits used on PIC16F628.

EEDATA register:

This register holds the 8bit data read or to be written.

EECON1 register:

EECON1

Bit 4-7 -Not implemented
WRERREEPROM Error Flag Bit :
Write Operation Terminated(1)/No error(0)
WREN EEPROM Write Enable Bit:
Allow write(1)/Write operation completed(0)

WRWrite Control Bit :
Start write(1)/Write operation completed(0)
RD- Read Control Bit :
Start read(1)/–(0)

EECON2 register:

This register is not a physical register. It is only needed at write procedure.

SPBRG register :

This is the register which holds the baud rate generator value. It can be calculated using the below formula :

Write and Read Operations

Write Operation :

EEWRITE:
BSF STATUS, RP0 ; Bank 1
BSF EECON1, WREN ; Enable write
MOVLW h’55′
MOVWF EECON2 ; Write 55h
MOVLW h’AA’
MOVWF EECON2 ; Write AAh
BSF EECON1,WR ; Set WR bit

; begin write
BCF STATUS, RP0 ; Bank 0
EEWR_WAIT: ;Wait for write cycle completed
BTFSS PIR1,EEIF ;Write cycle completed?
GOTO EEWR_WAIT ;No, wait
BCF PIR1,EEIF ;Yes, we can clear write flag
RETURN

The above procedure writes EEDATA to EEADR. EEDATA and EEADR must be entered before this procedure is called. EEDATA and EEADR are defined at BANK1 so don’t forget to switch BANK1 before setting these registers.

The values written to EECON2 is a must on write procedure. Probably this is a security necessity before writing EEPROM. Because h’55′ and h’AA’ are one bit zero and one bit set respectively and they are common patterns in security issues.

We have used EEIF (EEPROM write cycle completed) flag of PIR1 (Peripheral Interrupt Register) to be sure of the write cycle has been completed.

Read Operation :

EEREAD:
BSF STATUS, RP0 ; Bank 1
MOVWF EEADR ; Address to read
BSF EECON1, RD ; EE Read
MOVF EEDATA, W ; W = EEDATA
BCF STATUS, RP0 ; Bank 0
RETURN

Read procedure is relatively simpler then write procedure. Only we need to move address value to W(accumulator) before calling this routine.

Example :

In the following example, we will write to EEPROM address starting from 15 to 1. We will put the data corresponding to each address. We will put 15 to 15. address, 8 to 8. address and so on.

After writing all 15 values, we will read each value starting from 15 to 1 and then write it to PORTB. We will have a one second delay between each reading.

Download EEPROM source file.

The assembly code :

LIST P=16F628
#include <P16F628.inc>

CNT15 EQU h’20′ ;General register for address count

;Register definitions for 1 second delay
CNT1 EQU h’21′ ;General register for delay
CNT2 EQU h’22′ ;General register for delay
CNT3 EQU h’23′ ;General register for delay

;Program start
ORG 0×00
GOTO main

main:
;PORTB configuration
CLRF PORTB
BSF STATUS,RP0 ;BANK1
CLRF TRISB ;All bits as output
BCF STATUS,RP0 ;BANK0

LOOP: ;Main EEPROM loop
MOVLW D’15′ ;Start counter from 15
MOVWF CNT15

LOOP_WR: ;EEPROM Write loop
MOVF CNT15,W ;Move counter value to W
BSF STATUS,RP0 ;Switch to Bank1
MOVWF EEADR ;Put counter value to EEADR
MOVWF EEDATA ;Put counter value to EEDATA
BCF STATUS,RP0 ;Switch to Bank0
CALL EEWRITE ;Call EEPROM write routine
DECFSZ CNT15,F ;Decrement counter and check if zero
GOTO LOOP_WR ;Still to countdown

;Counter reached to zero
;We have finished to write all 15 values

MOVLW D’15′ ;Reset counter to 15
MOVWF CNT15

LOOP_RD: ;EEPROM Read loop
MOVF CNT15,W
BSF STATUS,RP0 ;Switch to Bank1
MOVWF EEADR ;Put counter to EEADR
BCF STATUS,RP0 ;Switch to Bank0
CALL EEREAD ;Call EEPROM read routine
MOVWF PORTB ;Put the read value to PORTB
CALL delay_1s ;1 second delay
DECFSZ CNT15,F ;Decrement counter and check if zero
GOTO LOOP_RD ;Still to countdown
;Counter reached to zero
;We have finished to read all 15 values

GOTO LOOP ;Do the same again

EEREAD:
BSF STATUS, RP0 ; Bank 1
MOVWF EEADR ; Address to read
BSF EECON1, RD ; EE Read
MOVF EEDATA, W ; W = EEDATA
BCF STATUS, RP0 ; Bank 0
RETURN

EEWRITE:
BSF STATUS, RP0 ; Bank 1
BSF EECON1, WREN ; Enable write
MOVLW h’55′
MOVWF EECON2 ; Write 55h
MOVLW h’AA’
MOVWF EECON2 ; Write AAh
BSF EECON1,WR ; Set WR bit
; begin write
BCF STATUS, RP0 ; Bank 0
EEWR_WAIT: ;Wait for write cycle completed
BTFSS PIR1,EEIF ;Write cycle completed?
GOTO EEWR_WAIT ;No, wait
BCF PIR1,EEIF ;Yes, we can clear write flag
RETURN

;——1s Delay Routine————-
delay_1s:
MOVLW d’5′
MOVWF CNT1
LOOP1:
MOVLW d’255′
MOVWF CNT2
LOOP2:
MOVLW d’255′
MOVWF CNT3
LOOP3:
DECFSZ CNT3,F
GOTO LOOP3
DECFSZ CNT2,F
GOTO LOOP2
DECFSZ CNT1,F
GOTO LOOP1

RETURN

END

Hardware

Just power PIC and connect 4 leds (we count only to 15) to PORTB<4:0>. Don’t forget to configure oscilator bit as internal oscillator (pic programming tutorial).

inteeprom

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Reddit
  • Add to favorites
  • Email
  • RSS

One thought on “Internal EEPROM

  1. huseyin g on

    hello
    I really want to thank this topics writer. I have been working over 3 days in writing eeprom of 16f628 according to the microchip datasheet but coudnt performed untill the see this topic, i done now :) thnak you alot. (datasheet of 16f628 is useles about writing eeprom i can say)

Leave a Reply to huseyin g Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>