PIC18F4520 Data Eeprom Memory

PIC18F4520 Data Eeprom Memory
Çeşme:
http://www.exploreembedded.com/wiki/PIC_Internal_Eeprom
  1. PIC18F4520
#include <xc.h> unsigned char EEPROM_ReadByte(unsigned char eepromAddress); void EEPROM_WriteByte(unsigned char eepromAddress, unsigned char eepromData); int main() { unsigned char eeprom_address = 0, data; data = 45; EEPROM_WriteByte(eeprom_address, data); // Write the data at memoryLocation 0x00 data = EEPROM_ReadByte(eeprom_address); // Read the data from memoryLocation 0x00 while (1); return 0; } unsigned char EEPROM_ReadByte(unsigned char eepromAddress) { while(EECON1bits.RD || EECON1bits.WR); // check the WR&RD bit to see if a RD/WR is in progress EEADR=eepromAddress; // Write the address to EEADR. EECON1bits.RD = 1; // Set the RD bit to trigger the eeprom read operation. return(EEDATA); // Return the data read form eeprom. } void EEPROM_WriteByte(unsigned char eepromAddress, unsigned char eepromData) { unsigned char gie_Status; while(EECON1bits.WR); // check the WR bit to see if a previous Write operation is in progress EEADR=eepromAddress; // Write the address to EEADR. EEDATA=eepromData; // load the 8-bit data value to be written in the EEDATA register. EECON1bits.WREN=1; // Set the WREN bit to enable eeprom operation. gie_Status = INTCONbits.GIE; // Copy the current Interrupt state INTCONbits.GIE = 0; // Disable the interrupts EECON2=0x55; // Execute the special instruction sequence EECON2=0xaa; // Refer the datasheet for more info EECON1bits.WR=1; // Set the WR bit to trigger the eeprom write operation. INTCONbits.GIE = gie_Status; // Restore the interrupts EECON1bits.WREN=0; // Disable the EepromWrite }