I've modified the code to learn it a little better and it is saving the data and bringing it back.
Code:
//==================================
// Include files
#include <p18f1320.h>
//==================================
// Function prototypes
unsigned char ReadEEPROM(unsigned int address);
void WriteEEPROM(unsigned int address, unsigned char data);
//==================================
// Declare EEPROM data and other global variables
// Note that EEPROM is mapped to program memory address 0xF00000
#pragma romdata dataEEPROM=0xF00000
/*
rom unsigned char FirstByte = 0x55;
rom unsigned char SecondByte;
rom unsigned char FirstArray[] = {0x00, 0x01, 0x02};
rom unsigned char SecondArray[3];
*/
#pragma udata
//unsigned char TestVar;
unsigned char TestVar1;
unsigned char TestVar2;
unsigned char TestVar3;
//==================================
// Main routine
#pragma code
void main(void)
{
// Configure the device to access Data EEPROM memory
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
/*
// Read each of the EEPROM bytes defined in FirstByte and FirstArray[]
TestVar = ReadEEPROM((unsigned int)&FirstByte);
TestVar = ReadEEPROM((unsigned int)&FirstArray[0]);
TestVar = ReadEEPROM((unsigned int)&FirstArray[1]);
TestVar = ReadEEPROM((unsigned int)&FirstArray[2]);
// Write to each of the EEPROM locations defined in SecondByte and SecondArray[]
WriteEEPROM((unsigned int)&SecondByte, 0xaa);
WriteEEPROM((unsigned int)&SecondArray[0], 0x07);
WriteEEPROM((unsigned int)&SecondArray[1], 0x08);
WriteEEPROM((unsigned int)&SecondArray[2], 0x09);
*/
TestVar1 = ReadEEPROM(0x00);
TestVar2 = ReadEEPROM(0x01);
TestVar3 = ReadEEPROM(0x02);
WriteEEPROM(0x00, 0x07);
WriteEEPROM(0x01, 0x08);
WriteEEPROM(0x02, 0x09);
// Wait forever
while (1);
}
//==================================
// EEPROM read routine
unsigned char ReadEEPROM(unsigned int Address)
{
//EEADRH = (unsigned char)(Address>>8); // Load the high byte of the EEPROM address
EEADR = (unsigned char)Address; // Load the low byte of the EEPROM address
EECON1bits.RD = 1; // Do the read
return EEDATA; // Return with the data
}
//==================================
// EEPROM write routine
void WriteEEPROM(unsigned int Address, unsigned char Data)
{
static unsigned char GIE_Status; // Variable to save Global Interrupt Enable bit
//EEADRH = (unsigned char)(Address>>8); // Load the high byte of the EEPROM address
EEADR = (unsigned char)Address; // Load the low byte of the EEPROM address
EEDATA = Data; // Load the EEPROM data
EECON1bits.WREN = 1; // Enable EEPROM writes
GIE_Status = INTCONbits.GIE; // Save the Global Interrupt Enable bit
INTCONbits.GIE = 0; // Disable global interrupts
EECON2 = 0x55; // Required sequence to start the write cycle
EECON2 = 0xAA; // Required sequence to start the write cycle
EECON1bits.WR = 1; // Required sequence to start the write cycle
INTCONbits.GIE = GIE_Status; // Restore the Global Interrupt Enable bit
EECON1bits.WREN = 0; // Disable EEPROM writes
while (EECON1bits.WR); // Wait for the write cycle to complete
}
I was having an error with EEADRH so I checked the data sheet and the 18F1320 didn't have a high bit. Does that mean that each address is one byte, or that I need to do something else to access the higher byte? I'm trying to store a lot of data in the 256 bytes I get access to, so I can't waste half of them. Besides blocking that out to get it to compile for the 18F1320 all I did was to create a few variables and address them directly, I also used the same ones so I could run it a few times and see that it had written and loaded the code. It seems to have worked. So now the question is, how to most effectively store data that way?
If anyone is not sure what I'm dealing with code in the PIC for, I already have making the PIC an HID device figured out. So it possible I would have the user plug in the PIC and bulk transfer data into an array, which would then be loaded into the eeprom by the PIC to save it, to avoid wasting space this array would be the same one that the data was stored in for running the light show.
Another thing I was thinking about is how best to balance the use of these bytes. Can I fill the whole 256 it says I have in the data sheet? Or is some of that the program memory? If I can use all 256 I think I could save a light show routine into it, it would just need to be carefully done. I would probably want to let them specify effects to the second, and if one byte of each effect code is time that would leave me with under 5 minutes, on the other hand I don't think I need 256 effect codes:
8 levels of brightness for each of the 3 colors (lowest level turns off, or cancels effect) = 24
Fast strobe on for all 4 sets (blue, green, red, and ultra bright strobes) = 4
slow Pulse for all 3 colors, slow strobe for the ultra brights = 4
the strobe is turned off by turning on any other effect, so strobe off needs no code
So that's 32 codes. Maybe I can narrow it down.
So if I use my byes in pairs I could have 128 effect commands, 10 bits for the timing, giving me up to 1024 seconds = 15 minutes, and that would still leave me 6 bits for 64 possible effect codes. That would be decent.
But maybe I'm counting my chickens before they hatch here because I don't know that I can use all the EEPROM like that. But given that, the question is, how do I make an array of ints (16 bit) store them, and retrieve them, I can use this storage method on bytes but I'm not sure how to break up the data and then put it back together.