The EEPROM


Page 23
INDEX

The PIC16C84 and PIC16F84 has a 64 byte EEPROM inside the chip. This is an area where data can be stored. 
It takes knowledge to be able to write and read this area. This discussion will explain how to prepare the necessary sub-routines. 
There are two types of storage in the PIC16F84: Volatile and non-volatile.
The Volatile storage is the content of the files. The files can be used as temporary storage but their content is lost when power is removed. 
The other form of storage is the 64 bytes in the EEPROM. The content remains after power is restored. 
The files in the chips can be used for temporary storage and have unlimited read/write cycles. The 'C84 has files from 0C to 2F and 'F84 has 0C to 4F. Any files not used in the running of a program can be sued for temporary storage. 
The 64 bytes in EEPROM can be used at any time.
The only problem with storing values in the EEPROM is the time taken to "burn" them into a location and the extra routines required to carry out the "burn" and "write" operations. In addition, you can only "burn" (write) each location about one million times and so it is not suitable for constantly-changing values. After a write command is executed, a delay of approx 20mS should be allowed before reading the location. This will give the cell time to be "burnt" (written into) and settle down after the write operation. 
If the EEPROM is needed, this article will provide the necessary routines. 

THE FILES
The PIC16C84 has files from 0C to 2F and the PIC16F84 has files from 0C to 4F. 
The PIC12c508A and PIC12C509 does not have EEPROM memory and has files 07 to 1F. 
The files common to all these chips are:  0C to 1F so that if you are writing a program that will be transferred to a PIC12C508A, these are the only files you can use. 
If you want to transfer a program from a PIC16C84 to a PIC12C508A, the EEPROM feature cannot be used. 

If you need to collect input data (up to 64 bytes) and store it, the EEPROM area can be used. 
One of the advantages of EEPROM data is its retention. The data remains in the chip after the supply voltage is removed. 
Any data stored in the files is lost when power is removed.   

STORING DATA IN  EEPROM
The following program places the value 100 in the first location in the EEPROM during "burn," using
the DE directive.  The program flashes a LED on and off and decrements the value in EEPROM. The program continues to flash the LED on  output 0 of port B and decrement the location in EEPROM. When the location is zero, the program halts. 
Note: Even if the power is turned off prior to the completion of 100 flashes, the last value in EEPROM  will be available when the power is returned. The chip must be re-programmed (burnt) to repeat the 100 flashes.
The program Illustrates how to initialize EEPROM using the assembler, how to read from EEPROM and write to EEPROM. 
The circuit for the program is shown below:


The Flashing LED circuit

PROCESSOR 16C84  or 16F84
LIST p=16c84
#include <p16c84.inc>
__CONFIG 03FF3 ;RC oscillator

 


File name: File No: Comments:
PCL
STATUS
PORTB
EEDATA
EEADR
INTCON
OPTREG
TRISB
EECON1
EECON2
equ 2
equ 3
equ 6
equ 8
equ 9
equ 0Bh
equ 081h
equ 086h
equ 088h
equ 089h
;Program Counter Low-bits
;Status Register
;Port B
;EEPROM Data Register
;EEPROM address Register
;Interrupt and Timer bits
;Option Register
;Make Port B bits input or output
;EEPROM Control Register 1
;EEPROM Control Register 2
 
Bit name: Bit No: Comments:
RP0
Z
GIE
T0IE
T0IF
WREN
WR
RD
equ 5
equ 2
equ 7
equ 5
equ 2
equ 2
equ 1
equ 0
;Register Bank select RP0=clear=bank0 RP0=set=bank1
;Z in Status file.  Z=set=1 when result of operation is zero!!
;Global Interrupt Enable bit.  0=Disables all interrupts
;Timer0 Overflow Interrupt Enable bit
;Timer0 Overflow Interrupt Flag bit
;EEPROM Write Enable bit
;Write Control bit
;Read Control bit

PROCESSOR 16C84  or 16F84
LIST p=16c84
#include <p16c84.inc>
__CONFIG 03FF3 ;RC oscillator

 


ReadEE reads EEPROM data at location specified in EEADR and returns with value in W
WriteEE writes the value in W to location specified in EEADR
Labels: Mnemonics: Alternate mnemonics: Comments:

SetUp 




DecCount






Delay1

Delay1X






ReadEE





WriteEE














Main













Halt
ORG 000H
BSF 03,5
CLRF 06
BCF 03,5
GOTO Main

MOVLW 00
MOVWF 09
CALL ReadEE
ADDLW 0FFh
CALL WriteEE
RETURN

MOVLW 13h
MOVWF 1B
NOP
DECFSZ 1A,1
GOTO Delay1X
DECFSZ 1B
GOTO Delay1X
RETLW 00

BSF 03,5
BSF 08,0
BCF 03,5
MOVF 08,0
RETURN

MOVWF 08
BSF 03,5
BCF 0B,7
BSF 08,2
MOVLW 55h
MOVWF 09
MOVLW 0AAh
MOVWF 09
BSF 08,1
CALL Delay1
BCF 08,2
BSF 0B,7
BCF 03,5
RETLW 00

CALL DecCount
SUBLW 00
BTFSC 03,2
GOTO Halt
BSF 06,0
MOVLW 64h
MOVWF 1B
CALL Delay 1X
BCF 06,0
MOVLW 64h
MOVWF 1B
CALL Delay 1X
GOTO Main

NOP
GOTO Halt

ORG 2100h
DE 64h

END
ORG 000H
BSF Status, RP0
CLRF TRISB
BCF Status, RP0


MOVLW 00
MOVWF EEADR
CALL ReadEE
ADDLW 0FFh
CALL WriteEE
RETURN

MOVLW 13h
MOVWF 1B
NOP
DECFSZ 1A,1
GOTO Delay1X
DECFSZ 1B
GOTO Delay1X
RETLW 00

BSF Status, RP0
BSF EECON1, RD
BCF Status, RP0
MOVF EEDATA, W
RETURN

MOVWF EEDATA
BSF Status, RP0
BCF INTCON, GIE
BSF EECON1, WREN
MOVLW 55h
MOVWF EECON2
MOVLW 0AAh
MOVWF EECON2
BSF EECON, WR
CALL Delay1
BCF EECON1, WREN
BSF INTCON, GIE
BCF Status, RP0
RETLW 00

CALL DecCount
SUBLW 00
BTFSC Status, Z
GOTO Halt
BSF PortB,0
MOVLW 64h
MOVWF 1B
CALL Delay 1X
BCF 06,0
MOVLW 64h
MOVWF 1B
CALL Delay 1X
GOTO Main

NOP
GOTO Halt

ORG 2100h
DE 64h

END
;program code to start at 000h
;switch to bank1
;Make all bits on port B all output
;Back to data bank 0


;Load W with 0
;look at EEPROM address 0
;Get count value
;Adding FF = subtracting 1!
;Put value into EEPROM
;Return

;Create a 20mS delay
;20 loops

;4uS x 256 loops=approx 1mS



;Return

;Switch to bank 1
;Set the read bit
;Back to data bank 0
;Put value of EEdata into W
;Return with value in W

;W to EEDATA
;Switch to bank 1
;Disable interrupts
;Write Enable bit set
;Toggle EEPROM Control  
             Register bits
;Toggle EEPROM Control  
             Register bits
;Begin write sequence
;Call Delay1
;Disable any further writes
;Enable Interrupts
;Back to data bank 0
;Return

;Call decrement count sub-routine
;Test for zero
;
;End flashing
;Turn on LED
;Create 250mS delay


;Turn off LED





;Create endless loop


;Start of  EEPROM
;Put 100 into first location

;Tells assembler to finish

EEPROM DATA
Data can be loaded into the 64 EEPROM cells during "burn" time, or during "set-up" or during the running of a program.
The following table shows how to load the 64 cells during "burn" time.
Any location(s) not required to hold a value are burnt as 00h, as shown in row 7.
The data is programmed into EEPROM on program download (burn time) using the assembler's "define EEPROM" (DE) directive.

ORG 2100H
DE 80h, 12h, 64h, 81h, 0AAh, 55h, 32h, 0E2h
DE 64h, 13h, 80h, 80h, 0FFh, 32h, 98h, 0AAh
DE 64h, 13h, 80h, 80h, 0FFh, 64h, 98h, 0AAh
DE 80h, 80h, 64h, 81h, 0AAh, 55h, 64h, 0E2h
DE 0AAh, 80h, 64h, 81h, 0AAh, 55h, 32h, 0E2h
DE 80h, 13h, 80h, 80h, 0FFh, 32h, 98h, 0FFh
DE 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h
DE 0FFh, 80h, 64h, 81h, 0AAh, 55h, 64h, 0E2h
;Start of EEPROM

The following program puts 0AAh into location 0,  0BBh into location 1 and 0CCh into location 2:

Labels: Mnemonics: Comments:

SetUp
















Delay1

Delay1X






WriteEE

ORG 000H
BSF 03,5
CLRF 06
BCF 03,5
MOVLW 00
MOVWF 09
MOVLW 0AAh
CALL WriteEE
MOVLW 01
MOVWF 09
MOVLW 0BBh
CALL WriteEE
MOVLW 02
MOVWF 09
MOVLW 0CCh
CALL WriteEE
GOTO Main etc

MOVLW 13h
MOVWF 1B
NOP
DECFSZ 1A,1
GOTO Delay1X
DECFSZ 1B
GOTO Delay1X
RETLW 00

MOVWF 08
BSF 03,5
BCF 0B,7
BSF 08,2
MOVLW 55h
MOVWF 09
MOVLW 0AAh
MOVWF 09
BSF 08,1
CALL Delay1
BCF 08,2
BSF 0B,7
BCF 03,5
RETLW 00

END
;program code to start at 000h
;switch to bank1
;Make all bits on port B all output
;Back to data bank 0
;Load W with 0
;Look at EEPROM address 0
;Put 0AAh into location 0









;This is only a part-program

;Create a 20mS delay
;20 loops

;4uS x 256 loops=approx 1mS



;Return

;W to EEDATA
;Switch to bank 1
;Disable interrupts
;Write Enable bit set
;Toggle EEPROM Control  
             Register bits
;Toggle EEPROM Control  
             Register bits
;Begin write sequence
;Call Delay1
;Disable any further writes
;Enable Interrupts
;Back to data bank 0
;Return

;Tells assembler to finish

The following program detects button A and increments EEPROM location "0." This is an incomplete program as overflow of location "0" is not detected. The program contains the all-important SWITCH DEBOUNCE. The program creates a short time-interval after the button has been released, before the button is read again. This prevents false reading. 

 
Circuit for button A

Program puts buttonA-presses into EEPROM:

Labels: Mnemonics: Comments:

SetUp 







IncCount






Delay1

Delay1X






ReadEE





WriteEE














Main



Main1
Main2
ORG 000h
BSF 03,5
BSF 05,4
BCF 03,5
MOVLW 00
MOVWF 09
CALL WriteEE
GOTO Main 

MOVLW 00
MOVWF 09
CALL ReadEE
ADDLW 01
CALL WriteEE
RETURN

MOVLW 13h
MOVWF 1B
NOP
DECFSZ 1A,1
GOTO Delay1X
DECFSZ 1B
GOTO Delay1X
RETLW 00

BSF 03,5
BSF 08,0
BCF 03,5
MOVF 08,0
RETURN

MOVWF 08
BSF 03,5
BCF 0B,7
BSF 08,2
MOVLW 55h
MOVWF 09
MOVLW 0AAh
MOVWF 09
BSF 08,1
CALL Delay1
BCF 08,2
BSF 0B,7
BCF 03,5
RETLW 00

BTFSS 05,4
GOTO Main1
CALL Delay1
GOTO Main
CALL IncCount
MOVLW 64h
MOVWF 1B
CALL Delay 1X
BTFSS 05,4
GOTO Main2
CALL Delay1
GOTO Main

END
;program code to start at 000h
;switch to bank1
;Make all bits on port B all output
;Back to data bank 0
;Load W with 0
;Look at EEPROM address 0
;Put 00h into location 0


;Load W with 0
;look at EEPROM address 0
;Get count value
;Add1
;Put value into EEPROM
;Return

;Create a 20mS delay
;20 loops

;4uS x 256 loops=approx 1mS



;Return

;Switch to bank 1
;Set the read bit
;Back to data bank 0
;Put value of EEdata into W
;Return with value in W

;W to EEDATA
;Switch to bank 1
;Disable interrupts
;Write Enable bit set
;Toggle EEPROM Control  
             Register bits
;Toggle EEPROM Control  
             Register bits
;Begin write sequence
;Call Delay1
;Disable any further writes
;Enable Interrupts
;Back to data bank 0
;Return

;Look at buttonA. Clear=pressed
;ButtonA pressed
;20mS delay between looks
;Loop. Not pressed

;Create 125mS switch-debounce



;Pressed
;Released


;Tells assembler to finish

Reading EEPROM values
The following program reads the value of a specific byte in EEPROM: 





ReadEE
MOVLW 00
CALL ReadEE
(
Next instruction)

MOVWF 09
BSF 03,5
BSF 08,0
BCF 03,5
MOVF 08,0
RETURN
;Look at location 0  (use 00 to 40h)
;Get value of EEPROM location 0
;W will have value of location 0

;Get ready to read address 0
;Switch to bank 1
;Set the read bit
;Back to data bank 0
;Put value of EEdata into W
;Return with value in W


Writing EEPROM values
Writing takes 10mS - 20mS to complete. You do not have to "do nothing" while the byte is being written. Simply carry out other operations for 20mS or pole the WR bit in EECON1 register. It will be zero when the write cycle to the data EEPROM is complete. The sub-routine below sets bit0 in flag file "1F" when the write cycle is complete. Before implementing the CheckWrite sub-routine, flag bit0 must be 0 and another sub-routine is needed to check the bit and return it to zero after it has been set.

CheckWrite BSF 03,5
BTFSS 08,1
BSF 1F,0
BCF 03,5
;Switch to bank 1
;Bit will be zero when write complete (EECON1,WR)
;Write complete. Set bit 0 in a "flag" file
;Back to data bank 0

Or the Write Operation Interrupt Flag can be polled. It will go HIGH when the write operation is complete:

CheckWrite BSF 03,5
BTFSC 08,1
BSF 1F,0
BCF 03,5
;Switch to bank 1
;Bit will be 1 when write complete (EECON1,EEIF)
;Write complete. Set bit 0 in a "flag" file
;Back to data bank 0


The information in this article has already been used successfully to solve a readers problem. Click HERE to see the problem.

NEXT