EEPROM

The 18F452 and the 18F4520 have a built-in EEPROM (Electrically Erasable Programmable Read-Only Memory). This non-volatile memory array is accessible during operation and can be used similar to RAM. However, the data stored will not be erased due to shutdown.

The following is geared for the 18F4520. This tutorial will also work with the 18F452 and the 18F4525. Before continuing, be sure to read through the first tutorial in this series, PIC Programming Basics

Understanding the PICs EEPROM

According to the 18F4520 datasheet, page 85 of 392, there are 4 control registers associated with the EEPROM.

  • EECON1
  • EECON2
  • EEADR
  • EEDATA

Inside the PIC, there is a block of memory allocated to EEPROM. In the EEPROM memory block, each register can be used to store an 8 bit word. There are 256 available addressable registers in the EEPROM block (0x00 to 0xFF).

Write to EEPROM Memory Address

The first operation is to write data to an EEPROM address location. According to the datasheet, page 87 of 392, the data and address need to be loaded into the EEADR and EEDATA control registers appropriately. Therefore, create a variable and store that variable into the EEDATA control register. Also, load the EEADR control register with 0x00.

This will setup the transfer in order to have 0xAA stored in EEPROM memory location 0x00.

1: int main()
2: {
3:     char data = 0xAA;
4:
5:     EEADR = 0x00;
6:     EEDATA = data;
7: }

Now the EEPROM address is 0x00 and the value to be stored there is 0xAA.

The EECON1 control register handles the data transfer to and from EEPROM memory registers. We have a choice in writing to the EEPROM memory block or the program memory. Therefore, clear the EEPGD bit and clear the CFGS bit to access the EEPROM memory.

 1: int main()
 2: {
 3:     char data = 0xAA;
 4:
 5:     EEADR = 0x00;
 6:     EEDATA = data;
 7:
 8:     EECON1bits.EEPGD = 0;
 9:     EECON1bits.CFGS = 0;
10: }

Since we are interested in writing to this memory, we also need to set the WREN bit.

 1: int main()
 2: {
 3:     char data = 0xAA;
 4:
 5:     EEADR = 0x00;
 6:     EEDATA = data;
 7:
 8:     EECON1bits.EEPGD = 0;
 9:     EECON1bits.CFGS = 0;
10:     EECON1bits.WREN = 1;
11: }

Before we continue, it is important that all interrupts are disabled. Therefore, we will clear the global interrupt enable (GIE) bit… for now.

 1: int main()
 2: {
 3:     char data = 0xAA;
 4:
 5:     EEADR = 0x00;
 6:     EEDATA = data;
 7:
 8:     EECON1bits.EEPGD = 0;
 9:     EECON1bits.CFGS = 0;
10:     EECON1bits.WREN = 1;
11:
12:     INTCONbits.GIE = 0;
13: }

According to the datasheet, the next set of steps must be performed in a particular order. These steps will initiate a write directly to the EEPROM memory register.

  • Write 0x55 to EECON2 control register
  • Write 0xAA to EECON2 control register
  • Set the WR bit in the EECON1 control register
  • Wait until WR is clear

Afterwards, the global interrupts can be turned on.

 1: int main()
 2: {
 3:     char data = 0xAA;
 4:
 5:     EEADR = 0x00;
 6:     EEDATA = data;
 7:
 8:     EECON1bits.EEPGD = 0;
 9:     EECON1bits.CFGS = 0;
10:     EECON1bits.WREN = 1;
11:
12:     INTCONbits.GIE = 0;
13:         EECON2 = 0x55;
14:         EECON2 = 0xAA;
15:         EECON1bits.WR = 1;
16:         while(EECON1bits.WR);
17:     INTCONbits.GIE = 1;
18: }

The data 0xAA has been written to EEPROM memory register 0x00. Now, let’s read the memory from this address.

Read from EEPROM Memory Address

Once data has been stored in the EEPROM memory block, it is important to read from it. Therefore, our second operation is to read from a register in EEPROM memory.

We will attempt to store data from an EEPROM memory register to the variable ‘data’. Begin by storing the EEPROM memory register address (0x00) into EEADR.

1: int main()
2: {
3:     char data;
4:
5:     EEADR = 0x00;
6:
7: }

The EECON1 control register handles the data transfer to and from EEPROM memory registers. We have a choice in reading from the EEPROM memory block or the program memory. Therefore, clear the EEPGD bit and clear the CFGS bit to access the EEPROM memory however, this time, we will set the read (RD) bit.

 1: int main()
 2: {
 3:     char data;
 4:
 5:     EEADR = 0x00;
 6:
 7:     EECON1bits.EEPGD = 0;
 8:     EECON1bits.CFGS = 0;
 9:     EECON1bits.RD = 1;
10: }

Now the EEDATA control register contains the value of the EEPROM register at address 0x00. Store the EEDATA register in ‘data’.

 1: int main()
 2: {
 3:     char data;
 4:
 5:     EEADR = 0x00;
 6:
 7:     EECON1bits.EEPGD = 0;
 8:     EECON1bits.CFGS = 0;
 9:     EECON1bits.RD = 1;
10:
11:     data = EEDATA;
12: }

The variable ‘data’ now contains the value of EEDATA.

duty free alcohol airport duty free cigs uk buy duty free cuban cigars where to buy cosmetics duty free fragrances buy tobacco duty free