Writing to EEPROM with your PIC
For me, the EEPROM is a powerful tool for debugging. Â Sure, there are debugging tools out there and even better “home-made” variety debuggers, but for me, it is simple and easy to use. Â With the PICkit 2 software, you are able to read what is currently on your PIC. Â If your PIC has written something to its EEPROM, you can easily see it by reading the PIC. Â There are a couple of issues with this; you have to physically have the PICkit 2 connected, reading the PIC resets it, the EEPROM only holds 8-bits per address space, and dealing with large amounts of information is difficult. Â But if you’re at the beginning of your project, it’s a good way to figure out what values your PIC is reading without hooking up an output or to what point your program made it to before it crashed.
But don’t think that the EEPROM is just for debugging, it obviously wasn’t designed for that. Â EEPROM allows you to store values and not have to worry about losing them when you lose power. Â Maybe I’m incorrect in saying this, but my view of the EEPROM is that it is the hard drive for your microcontroller. Â There’s typically much more of it than flash data space and it really can be used for whatever you think up.
So, how does it work? Â You basically put the information you want written in one register, the address of where you want it written in another register, and then set the write bit. Â Now! Â Microchip seems quite paranoid of “spurious writes” so there are a couple precautionary steps you’re required to take before writing. Â They seem nonsensical and that’s the point. Â The chances of these steps occurring randomly are quite small.
Let’s go through the different registers and bits first. Â These are from the PIC16F690 but other PICs should be extremely similar in their setup and usage.
   EEADR – EEprom ADRess: The address where you want to write the data.
   EEDAT  - EEprom DATa:  The data you want to write.
   EEPGD – EEprom ProGram Data: Selects whether you are accessing program or data memory.  0 is for data memory, 1 is for program memory.
   WREN – WRite ENable: Makes it so that setting the write bit actually does something.
   EECON2 – EEprom CONtrol register 2: A special register used to limit spurious writes.  When read, it will show “0″s.
   WR – WRite bit:  When this is bit is set, the data in EEDAT is written to the address at EEADR.  It cannot be cleared in software.
   EEIF – EEprom Interrupt Flag: Is set when the write is complete.  Must be cleared in software.
Â
To actually write, you simply use these registers and bits in the correct order. Â Steps six and seven have to be done exactly in that order and without any other commands between them.Â
- Put the address in the EEADR register.
- Put the data in the EEDAT register.
- Clear EEPGD so you actually write to the data memory.
- Set the WREN bit.
- Clear the Global Interrupt Bit (GIE) so you don’t have an interrupt in the middle of the next two steps.
- Put the value 0×55 into EECON2.
- Put the value of 0xAA into EECON2. Â
- Set the WR bit.
- Wait for EEIF to be set.
- Clear the EEIF bit and the WREN bit.
- Set the GIE bit if you want interrupts.