PDA

View Full Version : WRITECODE or not????



Jumper
- 17th August 2006, 10:35
Hi,

This might seem as a strange thing to do but I want to write to the program memory i.e as a Bootloader would do. I have located my code so it will not be overwritten but I get some weird results. So i decided to try this simple code that is put away far from my target write adresses. All variables are defined as Words.

for i =0 to 31 step 2
flash=i
temp=$ABCD
WRITECODE flash,temp
next i
HSEROUT ["DONE",10,13]
pause 2000
stop

Every time I run it I get a small printout "DONE" . Then I readback the code into MPLAB using Programmer->READ and look in the Program Memory window. The thing is that I can write to ANY address and get the correct value stored EXCEPT if I write to adr $0000, $0002 and $0004. These adresses only accepts $0000 as value if they will store it right. If I use $ABCD as value it looks like this in the Program memory readback:

0000: 2A04
0002: AB40
0004: A044
0006: ABCD
and the rest are ABCD as expected.

Other values give similar results and sometimes adr $0004 reads back OK but never 0000 and 0002 if I don't write 0000 to them.

Anyone? Any guess?
The PIC is a 18F4620 and no code protection and Tablewrite fuses are OFF. Neither manual (PBP or 18F4620) mentions anything about this from what I can see.

This is killing my project for the moment, I would be very happy if someone could come up with any ideas and answers.

I also printed the values stright from the PIC using READCODE and they show the same as MPLAB, so it is not a MPLAB problem!

best regards

/me

Darrel Taylor
- 17th August 2006, 20:30
Jumper,

Did you erase "block 0" first?

Also the 4620 needs to write 64 bytes at a time, instead of 32.
<br>

Jumper
- 18th August 2006, 04:07
Hi,

I forgot to erase the code first.

It is allowed to write either 64 BYTES or 32 WORDS, I went for writig words


/me

Jumper
- 19th August 2006, 08:01
When using ERASECODE don't forget this (a part from an e-mail from Melab support)
The erase blocks of 64 bytes do, in fact, have specific start/end
addresses that are arranged starting at address 0. The datasheet
refers to these blocks as "rows".

Each block (0-63, 64-127, etc.) will be erased in its entirety when
you use ERASECODE at any address within the block.

If you want to preserve part of the data in a block, use READCODE to
save the information before you erase, then WRITECODE to restore the
block with modified data afterward.

The best way is to learn from other's misstakes :-)

/me