OK I wrote some code here for read/write eeprom. Yes I often reinvent the wheel for better understanding...
There is some interesting stuff in here for newbies like the datatypes.h. I always define my own datatypes as that as you start porting code to other processors you will find this will save you days of debugging. For example one compiler defines a int to be a unsigned byte, while others define it to be a signed byte....
By the way you are all welcome to use this code, maybe one day if I make it to the Microchip training someone can buy me a beer if they found it useful.
//Datatypes.h
*
* DESCRIPTION:
*
* AUTHOR : Trampas Stern
*
* DATE : 7/30/2004 2:34:24 PM
*
* FILENAME: datatypes.h
*
* Copyright 2004 (c) by Trampas Stern
*******************************************************************/
#ifndef __DATATYPES_H
#define __DATATYPES_H
typedef unsigned char UINT;
typedef signed char INT;
typedef char CHAR;
typedef unsigned short UWORD;
typedef signed char BYTE;
typedef signed char UBYTE;
typedef signed short WORD;
typedef unsigned long UDWORD;
typedef signed long DWORD;
typedef float FLOAT;
typedef union {
UWORD data ;
struct {
BYTE low;
BYTE high;
};
} UWORDbytes;
typedef union {
UDWORD data ;
struct {
BYTE byte0;
BYTE byte1;
BYTE byte2;
BYTE byte3;
};
struct {
UWORD word0;
UWORD word1;
};
} UDWORDbytes;
typedef union {
UBYTE byte;
struct {
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
} UBYTEbits;
#define MAX_UDWORD 0xFFFFFFFF
#endif //__DATATYPES_H
//eeprom.h header
#ifndef __EEPROM_H
#define __EEPROM_H
#include "datatypes.h"
//#include "system.h" //This is where I define stuff such as Fosc
BYTE EEpromGet(UWORD addr);
void EEpromPut(UWORD addr, BYTE data);
UBYTE EEpromRead(UWORD addr, UBYTE *data, UWORD len);
UBYTE EEpromWrite(UWORD addr, UBYTE *data, UWORD len);
#endif //__EEPROM_H
#include "eeprom.h"
/*******************************************************************
* FUNCTION: EEpromGet
* AUTHOR = TRAMPAS STERN
* FILE = eeprom.c
* DATE = 11/4/2004 8:24:41 AM
*
* PARAMETERS:
*
* DESCRIPTION: Gets a byte of data from EEPROM
*
* RETURNS:
*
* Copyright 2004 (c) by Trampas Stern
*******************************************************************/
BYTE EEpromGet(UWORD addr)
{
volatile BYTE eepTemp;
UWORDbytes temp;
temp.data=addr;
EEADRH = temp.high;
EEADR = temp.low;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
eepTemp = EEDATA;
return eepTemp;
}
/*******************************************************************
* FUNCTION: EEpromPut
* AUTHOR = TRAMPAS STERN
* FILE = eeprom.c
* DATE = 11/4/2004 8:55:20 AM
*
* PARAMETERS:
*
* DESCRIPTION:
*
* RETURNS:
*
* Copyright 2004 (c) by Trampas Stern
*******************************************************************/
void EEpromPut(UWORD addr, BYTE data)
{
UWORDbytes temp;
temp.data=addr; //could use cast instead
EEADRH = temp.high;
EEADR = temp.low;
EEDATA = data;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
DISABLE_INTERRUPTS();
_asm
MOVLW 0x55
MOVWF EECON2,0
MOVLW 0xAA
MOVWF EECON2,0
_endasm
EECON1bits.WR=1;
while (EECON1bits.WR == 1);
ENABLE_INTERRUPTS();
EECON1bits.WREN = 0;
}
/*******************************************************************
* FUNCTION: EEpromWrite
* AUTHOR = TRAMPAS STERN
* FILE = eeprom.c
* DATE = 11/5/2004 7:55:18 AM
*
* PARAMETERS:
*
* DESCRIPTION:
*
* RETURNS:
*
* Copyright 2004 (c) by Trampas Stern
*******************************************************************/
UBYTE EEpromWrite(UWORD addr, UBYTE *data, UWORD len)
{
UWORD i;
for(i=0; i<len; i++)
{
EEpromPut(addr++,data[i]);
}
return i;
}
/*******************************************************************
* FUNCTION: EEpromRead
* AUTHOR = TRAMPAS STERN
* FILE = eeprom.c
* DATE = 11/5/2004 7:56:40 AM
*
* PARAMETERS:
*
* DESCRIPTION:
*
* RETURNS:
*
* Copyright 2004 (c) by Trampas Stern
*******************************************************************/
UBYTE EEpromRead(UWORD addr, UBYTE *data, UWORD len)
{
UWORD i;
//printf("eeprom read %uhd\n\r",len);
for(i=0; i<len; i++)
{
data[i]=EEpromGet(addr++);
//printf("%x ", data[i]);
}
return i;
}