/*******************************************************************************
 *	TestEeprom.c		Greenpower CarComputer Test Program
 *				T.Barnaby,	BEAM Ltd,	2008-02-28
 *
 *	Copyright (c) Beam Ltd and Chipping Sodbury School
 *******************************************************************************
 *
 * Function:	Writes and Reads from the EEPROM displays data on the LCD panel
 * Version:	1.0
 */
#include <string.h>
#include <pic18f2520.h>
#include <picLib3.c>

#define	CLOCK		4000000		// Clock frequency

#pragma udata eedata	ed1,ed2
char	ed1;
char	ed2;


void main(void) {
	UInt8	d;
	UInt8	d1[] = { 0x11, 0x22, 0x33, 0x44 };
	UInt8	i;
	UInt16	w;
	
	// Set up Oscillator
	OSCCON = 0x62;				// Internal 4MHZ
	TRISB = 0x0F;           		// PORTB top 4 bits Output

	PORTB = 0;				// LED's off

	PORTBbits.RB7 = 1;			// Red LED on

	i2cInit();
	lcdInit();
	
	lcdWriteStr(0,0, "Testing");
	
	eepromWrite8(0, 0x55);
	d = eepromRead8(0);
	lcdPutHexByte(d);
	
	eepromWrite16(0, 0x1122);
	w = eepromRead16(0);
	lcdPutHexByte(w >> 8);
	lcdPutHexByte(w);
	
	// Write data to EEPROM
	d = 0xAA;
	i2cWrite(0xA0, 0, I2cFast16, &d, 1);

	i2cWrite(0xA0, 1, I2cFast16, d1, sizeof(d1));

	// Read data from EEPROM
	d = 0;
	i2cRead(0xA0, 0, I2cFast16, &d, 1);
	lcdPutHexByte(d);
	
	i2cRead(0xA0, 1, I2cFast16, d1, sizeof(d1));
	lcdWriteStr(0, 1, "");
	for(i = 0; i < sizeof(d1); i++)
		lcdPutHexByte(d1[i]);
	
	// Loop forever
	while(1){
	}
}
