You are on page 1of 19

Chapter 8 Part 2

C program to erase the message of GOOD BYE from


flash addresses 0x1200 and replaces it with HELLO
#include <p18Cxxx.h>
void Delay(unsigned int itime)
#pragma romdata const_table = 0x1200
const rom char my_const_array[10]=GOOD BYE;
#pragma romdata
Void main()
{
unsigned char x;
//erase program memory
TBLPTR =(short long) 0x1200;
EECON1bits. EEPGD = 1;
EECON1bits.CFGS = 0;
EECON1bits.WREN =1;
EECON1bits.FREE =1;
INTCONbits.GIE =0;

EECON2 = 0x55;
EECON2=0xAA;
EECON1bits.WR =1;
_asm NOP endasm
INTCONbits.GIE = 1;
EECON1bits.WREN =0;
TBLPTR =(short long) 0x1200;
TABLAT = H
_asm TBLWTPOSTINC _endasm
TABLAT = E
_asm TBLWTPOSTINC _endasm
TABLAT = L
_asm TBLWTPOSTINC _endasm
TABLAT = L
_asm TBLWTPOSTINC _endasm
TABLAT = O
_asm TBLWTPOSTINC _endasm

//long write
TBLPTR =(short long) 0x1200;
EECON1bits.EEPGD = 1;
EECON1bits.CFGS = 0;
EECON1bits.WREN =1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR =1;
_asm NOP _endasm
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;

//read from program memory send to PORTB


TBLPTR =(short long) 0x1200;
for (x=0;x<8;x++){
_asm TBLPTRPOSTINC _endasm
PORTB = TABLAT;
Delay(250);
}
}

8.3 Reading and Writing to Data EEPROM in the PIC18

The vast majority of the members of the PIC18


family come with some EEPROM memory.
The amount varies from 256 bytes to a few K
depending on the family member.
For example, PIC18F4520 has 256 bytes of
EEPROM while PIC18F4585 = 1024bytes.
Flash can store both code and fixed data
storage
EEPROM can only store data
SRAM can only store data

Writing data to EEPROM


4 registers associated with the
EEPROM.
1) EEADR : An 8bit register, used as
pointer to EEPROM location
2) EEDATA: An 8 bit register, holds data
to be written to EEPROM
3) EECON1: Used by both EEPROM and
Flash
4) EECON2: The dummy register. Used
by both EEPROM and Flash

Step in writing to EEPROM


To write a byte of data to a location in the EEPROM
memory, below are the 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
(a) EEPGD =0 (b) CFGS = 0; (c) WREN = 1
4) Disable all interrupt globally INTCONbits.GIE =0
5) Write 55H to the EECON2 dummy register
6) Write AAH to the EECON2 dummy register

7) Set WR# to 1 with the instruction EECON1bits.WE


=1. With WE =1, the write cycle begins.
8) Upon completion of the write cycle , the WE# bits
will be cleared automatically to indicate the write
cycle is finished
9) Re-enable the interrupt globally using
INTCONbits.GIE =1
10) The WREN bit should be cleared to prevent an
accidental write to the EEPROM by some runaway
program. (Important to make WREN =0, because
the PIC18 will not do that automatically.

Program to writes a single ASCII


letter of H to EEPROM address 10H
MOVLW 0X10
MOVWF EEADR
MOVLW AH
MOVWF EEDATA
BCF EECON1,EEPGD
BCF EECON1,CFGS
BSF EECON1,WREN
BCF INTCON,GIE
MOVLW 0X55
MOVWF EECON2
MOVLW 0XAA
MOVWF EECON2
BSF EECON1,WR
; now write to Flash
BSF INTCON, GIE
; enable all interrupt
BCF EECON1,WREN ;disable write to memory

Steps in reading from


EEPROM
1) Load the EEADR register with the address
of the EEPROM location we want to read.
2) Set the EECON1 register for the EEPROM
read.
(a) EEPGD = 0, (b) CFGS = 0 and (c) RD
=1
3) Within the next instruction cycle, the
PIC18 will automatically fetch the data
from the EEPROM location and place in the
EEDATA register.

Example shows how to read a byte


from EEPROM and place it in PORTB
MOVLW 0X10
MOVWF EEADR
BCF EECON1,EEPGD
BCF EECON1, CFGS
BSF EECON1,RD
NOP
MOVFF EEDATA,PORTB

Example in C language
This program
(a)Writes the message YESto
EEPROM memory
(b)Reads the same data from EEPROM
and sends it to PORTB one byte at a
time

#include<p18F458.h>
void EE_WRT(void);
unsigned char EE_READ(void);
void Delay(unsigned int itime);
Void main()
{
unsigned char x;
TRISB =0;

//write to EEPROM
EEADR =0x0;
EEDATA =Y;
EE_WRT();
EEADR =0x1;
EEDATA =E;
EE_WRT();
EEADR =0x2;
EEDATA =S;
EE_WRT();
EECON1bits.WREN=0; //disable write

//read from EEPROM and place it on PORTB


EECON1bits.RD=1;
EEADR = 0x0;
x= EE_READ();
PORTB =x;
Delay(250);
EEADR=0x1;
x= EE_READ();
PORTB =x;
Delay(250);
EEADR=0x2;
x= EE_READ();
PORTB =x;
}

void EE_WRT()
{
EECON1bits.EEPGD=0;
EECON1bits.CFGS=0;
EECON1bits.WREN=1;
INTCONbits.GIE=0;
EECON2=0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1;
INTCONbits.GIE =1;
while (!PIR2bits.EEIF);
//if EEIF still 0 write operation
not //complete and will loop here forever.
PIR2bits.EEIF=0; //once complete, EEIF = 1, it is
complete //and need to be cleared in software
}

Unsigned char EE_READ()


{
EECON1bits.EEPGD=0;
EECON1bits.CFGS=0;
EECON1bits.RD=1;
return(EEDATA);
}

You might also like