// Device is a PIC16F684
#include <pic.h>
__CONFIG(HS & WDTDIS & PWRTDIS & MCLREN & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
void init_pic16f684(void);
unsigned char ReadByteFromEE(unsigned char);
void WriteByteToEE(unsigned char, unsigned char);
void main()
{
init_pic16f684();
PORTC = ReadByteFromEE(0x00);
WriteByteToEE(0x04, 0x00);
while(1); // infinite loop
}
void init_pic16f684()
{
PORTA = 0x00; // Initialize PORTA
PORTC = 0x00; // Initialize PORTC
CMCON0 = 0x07; // comparators off, Digital I/O
ANSEL = 0x00; // Digital I/O
TRISA = 0x00; // PORTA Outputs
TRISC = 0x00; // PORTC Outputs
}
/////////////////////////////////////////////////////////////
// Reads one byte from the EEprom at the specified address //
// and returns it //
/////////////////////////////////////////////////////////////
unsigned char ReadByteFromEE(unsigned char address)
{
unsigned char byte; // Variable hold the data that is read
EEADR = address; // Read from this address
RD = 1; // Initiate a read cycle
byte = EEDAT; // Fetch byte from dataregister
return byte; // Return the read byte
}
////////////////////////////////////////////////////////////
// Writes one byte to the EEprom at the specified address //
////////////////////////////////////////////////////////////
void WriteByteToEE(unsigned char data, unsigned char address)
{
EEADR = address; // Address to write to 0x00 to 0xFF
EEDAT = data; // Data to write
WREN = 1; // Enable writes to the EEProm
while(GIE)
GIE = 0; // Disable interrupts during write
EECON2 = 0x55; // Register not implemented on 16F684
EECON2 = 0xAA; // strange that this is required
WR = 1; // Initiate a write cycle
while(!EEIF); // Wait for write to complete
WREN = 0; // Disable writes to EEProm
EEIF = 0; // Clear "write complete" flag
GIE = 1; // Reenable interrupts
}