Actually, there are 20 16F's that can write to their own flash memory.
But there are 5 different methods, depending on the chip in use.
> I can't believe that absolutely no one else has chimed in on this topic, though.
After your first post, I started making a Flash movie, to show how to write to flash on a 16F88. And before I could even finish the first frame, this thread had already entered the "Confusion Zone", very similar to the "Twilight Zone".
So I decided to expand it to include all the 16F's. Big mistake, takes too long. Should have stuck with the 16F88. Here's one of the frames which shows the different varieties. At that point the flash splits into 6 different explanations. 16F88 is #6.
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2926" /><!-- -->
It'll take too long to finish to be of help to you, and I don't think you are any closer to writing to flash on a 16F88, so I'll try to put it in Words.<hr>
The only thing to really worry about, is that the Write to Flash only occurs when you write to the 4th word in the Block. Writing to the previous 3 words, only places the data in "Holding Registers".
For instance, the following code which attempts to write to the 1st word and read it back ...
Code:
FlashAddr VAR WORD
FlashData VAR WORD
FlashAddr = $800
FlashData = $123
WRITECODE FlashAddr, FlashData
READCODE FlashAddr, FlashData
Will return $3FFF in the FlashData variable, because the data was never written.
However, this code ...
Code:
FlashAddr = $803
FlashData = $123
WRITECODE FlashAddr, FlashData
READCODE FlashAddr, FlashData
Would return $123, because it wrote to the 4th word of the block.
If you wanted to only write data to the First address, then you must also write data to the 4th address. If the 4th data is $3FFF then nothing actually gets written to that location.
So this code ...
Code:
FlashAddr = $800
FlashData = $123
WRITECODE FlashAddr, FlashData
FlashAddr = $803
FlashData = $3FFF
WRITECODE FlashAddr, FlashData
FlashAddr = $800
READCODE FlashAddr, FlashData
Will return $123, and only 1 Word has been written to memory.
How do you know if it's the 4th word of the Block?
By the Lowest 2-bits of the address. If they are both 1's then it's the 4th.
Therefore, it's easy to make a subroutine to write a single word anywhere that's free.
Code:
WriteFlashWord: ; Set FlashAddr and FlashData before calling (Vars are Destroyed)
WRITECODE FlashAddr, FlashData
IF (FlashAddr & %11) != %11 THEN
FlashAddr = FlashAddr | %11
FlashData = $3FFF
WRITECODE FlashAddr, FlashData
ENDIF
RETURN
The chip should be Erased when it's programmed, so you only need to ERASECODE, if you need to overwrite the data.
HTH,
Bookmarks