2005/08/01 21:40:13
Guest
I am having a problem reading from the internall EEPROM memory. I am using the learning version of the C18 compiler, ICD2 programmer, and my target is the 18F452 on the PICDEM2+ board.

My problem is that I am unable to read the internal eeprom until after I have unplugged the board from the programmer and removed power from the board. Until I do this, my read function just returns 0. It does not seem to matter whether the data is going into the eeprom from my program or if it is being programmed by the programmer.

This is my code to write to the eeprom:

void eeprom_write (char *buffer) // write string to eeprom
{
unsigned int eeaddress = 0;
char letter;
PIE1bits.TMR1IE = 0; // disable timer1 interrupt
EECON1bits.EEPGD = 0; // address eeprom, not program
EECON1bits.WREN = 1; // Write enable
while(*buffer) //
{
EEADR = eeaddress;
letter = (*buffer);
EEDATA = letter;
EECON2 = 0x55; // I'm told you must do this
EECON2 = 0xAA; // and this too
EECON1bits.WR = 1;
while (EECON1bits.WR)
Nop (); // wait for write to complete
eeaddress++; // point to next storage location
buffer++; // Increment buffer
}
EEADR = eeaddress;
EEDATA = (*buffer);
EECON1bits.WR = 1; // store null character at end of string
PIE1bits.TMR1IE = 1; // enable timer1 interrupt
while (EECON1bits.WR)
Nop (); // wait for write to complete
return;
}

This is the code to read from the eeprom:

void eeprom_read (void) // read string from eeprom
{
unsigned char j = 0;
char read;


do {

EEADR = j;
EECON1bits.EEPGD = 0; // address eeprom, not program
EECON1bits.RD = 1;
read = EEDATA;
incoming[j] = read;
j++; // point to next storage location
}
while(read); // read data from eeprom up through null

return;
}


Has anyone else encountered this? The source file is attached (I think).

Thanks,
-Maarten
2005/08/05 03:08:27
Guest
I 've some questions before I plan a small database with my PIC 18F242:

Is it necessary to erase the program data before write it ?
Is there a diffrence between write 0xFF and erase ?
So it would be possible to erase less thean 64kbytes ?

What will happen after 100.000 cyles of write-program-memory operation ?

Thanks for help!
2005/08/05 05:36:50
jspaarg
You have to perform an erase operation prior to performing a write; writing 0xff is not the same thing.

You don't erase 64K at a time from internal code. The block size changes based on the PIC, but 64 bytes is typical. Read the datasheet.

After 100,000 write cycles, the ability of the memory to be erased and hold new data is not guaranteed. There is nothing magical about the 100,001st write. The chip may work to 150,000; but there is no guarantee.

Read the datasheet for the 242, it will provide sample code on erasing and writing FLASH.
2005/08/05 08:00:54
Guest
I read the datasheet for th ePIC 18F2424 100 times, but I couldn`t find any note, that I have to erase before I write.
But in AN910 on page 12 I found:
"For all devices except the PIC18 family, erase is generally
handled as part of the programming command. All
devices use an “Erase Program” command, which
automatically blanks the target location before writing
new data to it."
So I guess only the PIC 18.. have to be erased before write.

Can I use "memcpyram2pgm" to handle this very easy, or do I have to use the inline assembly ?
2005/08/05 08:54:38
jspaarg
Try reading sections 5.4 and 5.5 (especially 5.5.1) for the 101st time.
2005/09/13 22:00:02
chsekhar
Hi,

I wrote C functions for writing data to flash memory and used in my project successfully.

/**************************************************
FUNCTION TO WRITE WHOLE BLOCK OF 64 BYTES
**************************************************/

rom unsigned char* startblock = 0x5000; // any number divisible by 64
rom unsigned char* buf; // temporary rom pointer
unsigned char mem[64]; // data holding buffer

void WriteDataToMemory(int block_no)
{
unsigned char i;

TBLPTR = (unsigned int)(startblock + block_no * 64);

buf = (rom unsigned char*)TBLPTR;

EECON1bits.EEPGD = 1; // erase sequence
EECON1bits.WREN = 1;
EECON1bits.FREE = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1;
Nop();
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;

for(i = 0;i < 64;i++){
buf = mem;
if(((i + 1) % 8) == 0){
EECON1bits.EEPGD = 1; // write sequence for every 8 bytes
EECON1bits.WREN = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1;
Nop();
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;
}
}
}


/**************************************************
FUNCTION TO WRITE A BYTE TO SPECIFIC LOCATION
**************************************************/

void WriteByteToMemory(rom unsigned char* addr,unsigned char b)
{
unsigned char i;

// identify block(64 bytes)
unsigned char off = (unsigned int)addr % 64; // offset
TBLPTR = (unsigned int)(addr - off); // base

// backup 64 bytes
buf = (rom unsigned char*)TBLPTR;
for(i = 0;i < 64;i++){
mem = buf;
}
mem[off] = b; // modify byte at base + off

EECON1bits.EEPGD = 1; // erase sequence
EECON1bits.WREN = 1;
EECON1bits.FREE = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1;
Nop();
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;

for(i = 0;i < 64;i++){ // write back 64 bytes after modification
buf = mem;
if(((i + 1) % 8) == 0){
EECON1bits.EEPGD = 1; // write sequence for every 8 bytes
EECON1bits.WREN = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1;
Nop();
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;
}
}
}

HOWEVER FOR READING FLASH MEMORY IT IS STRAIT FORWARD

data = buf[0]; // read byte from the address location (buf + 0)


-Sekhar
2005/10/02 20:10:49
Guest
I reported earlier about having to do a cold reboot on my project before I could write to the EEPROM data memory.

I am using an 18F452 and programming with an ICD2. I dilligently copied the assembly code in section 6 "DATA EEPROM MEMORY" into C. But until I disconnected the programmer and removed power from the board, I was unable to read anything but garbage back from the EEPROM memory. After removing power for a while (5 seconds) the board would start behaving as expected.

I finally guessed that some register bit somewhere was being set during programming, and needed to be cleared, which the cold reboot accomplished. The most likely was bit 6 of the EECON1 register. By adding the line

EECON1bits.CFGS = 0; // access data registers

I got the board to function as expected even while connected to the ICD2.

-Maarten
2005/10/02 20:23:30
ric
ORIGINAL: mkorringa

...I finally guessed that some register bit somewhere was being set during programming, and needed to be cleared, which the cold reboot accomplished. The most likely was bit 6 of the EECON1 register. By adding the line

EECON1bits.CFGS = 0; // access data registers

I got the board to function as expected even while connected to the ICD2.

Do not assume the power-on state of any SFR that you depend upon, and you'll never get bitten by this sort of problem.
That said, your post will be useful to other people striking a similar problem :)
2005/11/02 23:09:21
lukez
Trampas, thanks for the sample code you posted here, as well as elsewhere in the forums. Your posts are always useful.

I have a question though in your Datatypes.h file. You have these two typedefs:
typedef signed char BYTE;
typedef signed char UBYTE;

Don't you mean the second one to be:
typedef UNsigned char UBYTE;

??


Luke
2005/12/05 14:27:22
Guest
ORIGINAL: dcs1212

I'm sure that I should know why this is but I don't. I have tried several of the EEPROM routines posted to see which I like best. The one the is in the "Attachement (1)" file I am using right now but I get a suspicous pointer conversion from this line in the *memcpyde2ram function. Do you know why?

*(s1+n) = readDataEE(s2+n);


This solves your warning
*(s1+n) = readDataEE(s2[n]);

Pointer math :P
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account