« Getting up to speed with the PIC16F737 | Main | Eagle Postscript Cam Job »

EEPROM read/write code for CC5X (January 30, 2008)

CC5X may already include internal-EEPROM handling code, but I couldn't find any. With this in mind, here are two short methods for writing and reading from the EEPROM in many PIC microcontrollers. (Based on Microchips datasheets and a forum post.)

    void write_eeprom(char addr, char data)                         //Function to write to EEPROM memory
    {
            EEADR=addr;
            EEDATA=data;
            EECON1.2 = 1;                           //WREN: enable writing to memory
        PIR1.7 = 0;                                 //EEIF: Clear this bit which gets set after each write

            EECON2 = 0x55;                          //Required before writing to memory
            EECON2 = 0xAA;                          //Required before writing to memory
            EECON1.1 = 1;                           //WR: Writes data EEDAT to address EEADR
            while(EECON1.1==1);                     //Wait for write to complete (becomes 0 when complete)

            EECON1.2 = 0;                           //WREN: disable write cycles
    }

    char read_eeprom(char addr){
            EEADR=addr;             //load the address
            EECON1.0 = 1;   //start the read
            return EEDATA;
    }


Posted by spiffed at January 30, 2008 12:00 AM

Comments

Post a comment




Remember Me?