PDA

View Full Version : PIC18 (452) DATA or EEPROM Command



Acetronics2
- 15th August 2007, 14:42
Hi everybody,

A small surprise this afternoon with a 18F452 :

Clearing ( With 0s ) locations in the EEPROM gives such results ...

DATA (0), 0 (10) ... writes 10 Times 0 from location 0 to 09 ... quite normal !!!

but

DATA (1), 0 (10) ... writes 11 Times 0 from location 1 to location ...0B or 12 !!!


EEPROM equivalent Command gives exactly the same result ...

Not THE big bug ... , but take care when presetting EEPROM values ...

Alain

Bruce
- 15th August 2007, 16:53
It's not actually a bug. The block write size to EEPROM is done in blocks of two bytes,
so when you have an odd number of bytes, the most significant byte written will be 0.

If you change DATA (1), 0 (10) to DATA (1), 1 (10) you'll see how the msb is 0, and you
have only 10 1's actually being written.

Acetronics2
- 16th August 2007, 15:22
Hi, Bruce

Thanks for the reply ...

If I undestood it , in your example there's Always 11 Writes: 10 "1"'s and 1 x "0" ... The original "FF" @ Loc. 12 is then overwritten by a "0"

Alain

Bruce
- 16th August 2007, 16:14
Hi Alain,

With DATA (1), 0 (10). Skip location 0 then write 10 0's. The total is 11. 1 byte skipped, and
10 bytes to be written.

PBP produces asm that looks like this;

ORG EEPROM_START + 00000h
DW 0000000FFh ' locations 0 and 1. Note FF is the skipped location 0.
DW 000000000h ' locations 2 and 3
DW 000000000h ' location 4 and 5
DW 000000000h ' locations 6 and 7
DW 000000000h ' locations 8 and 9
DW 000000000h ' locations 10 and 11

Which looks like this in EEPROM;

0000- ff 00 00 00 00 00 00 00
0008- 00 00 00 00

DW defines a data word, so each word is made up of two bytes. If you write an odd number
of bytes, the most significant byte will be cleared because DW forms everything as a word
value. On the 18F series, program memory (where EEPROM is) is all word aligned.

It's a bit easier to see if you do something like this;

DATA 1,2,3,4,5,6,7,8,9

Which produces;

ORG EEPROM_START + 00000h
DW 000000201h
DW 000000403h
DW 000000605h
DW 000000807h
DW 000000009h

And in EEPROM it looks like this;

0000- 01 02 03 04 05 06 07 08
0008- 09 00 <-- note the msb here is 0

You just have to remember that there will always be an even number of bytes written to
EEPROM. The 18F reference manual shows how this works.