ritchie
Joined: 13 Sep 2003 Posts: 87
|
PIC18F1320 Internal EEPROM Problem? |
Posted: Tue Mar 16, 2004 6:04 pm |
|
|
Hello,
I have a problem with the internal eeprom of pic18f1320?
To view the complete picture... this what happend:
[1] I download the code to the PIC
[2] I execute the run button
[3] check the content to the eeprom then check if its ok
Next...
[4] I turn-off the power
[5] I read the content of the eeprom
[6] Unfortunately it read 0000 instead of 0009
Next....
[7] I plug the ICD cable and the other end to the PIC
[8] I execute the run button
[9] read the content of the eeprom and it read 0009 ---> this is weird?
Anyone have the same problem? can you share some insights.. I really need the internal eeprom if I can't make a workaround I might use a 64bytes external eeprom (microwire I guess).
My eeprom read and write routine below:
Code: |
#int_eeprom
void eeprom_isr() // write device address to data eeprom interrupt routine
{
while (!EEIF); // wait for eeprom write to complete
EEIF=0; // clear eeprom interrupt flag
WREN=0; // inhibits erase or write cycles?
}
void mtask_write_eeprom(unsigned int8 uiAddr, unsigned int8 uiData)
{
EEIF = 0; // make sure that interrupt flag is cleared
EEDR = uiAddr; // load address here
EEDATA = uiData; // load data here
EEPGD = 0; // point to data eeprom memory
WREN = 1; // allow erase or write cycles?
disable_interrupts(global); // required to disable interrupts
EECON2 = 0x55; // write 0x55 (a required sequence?)
EECON2 = 0xAA; // write 0xAA (a required sequence?)
EEWR = 1; // initiates a data eeprom erase/write cycle
#asm nop #endasm // one clock cycle delay
enable_interrupts(global); // restore all interrupts
while (EEWR); // wait for write cycle to complete
#asm nop #endasm // one clock cycle delay
return; // return to main
}
unsigned int8 mtask_read_eeprom(unsigned int8 uiAddr)
{
EEDR = uiAddr; // load address here
EEPGD = 0; // point to data eeprom memory
#asm nop #endasm // one clock cycle delay
EERD = 1; // initiates a eeprom memory read
return(EEDATA); // return EEDATA
}
write_long_int_eeprom(unsigned int8 uiAddr, unsigned int16 ui16Data)
{
unsigned int8 i;
for (i=0; i<2; i++) // write device address into two bytes location
mtask_write_eeprom(i+uiAddr, *(&ui16Data+i));
}
unsigned int16 read_long_int_eeprom(unsigned int8 uiAddr)
{
unsigned int8 i;
unsigned int16 ui16Result;
for (i=0; i<2; i++) // read device address from two bytes location
*(&ui16Result+i) = mtask_read_eeprom(i+uiAddr);
return(ui16Result);
}
|
I need your comments and suggestion on this guys and [spam]...
Thank u. |
|