PDA

View Full Version : Writing to Program code space using Writecode command.A



readitaloud
- 26th January 2012, 08:08
Using a 18F26J11 device and having problems loading a byte or word into the program memory space. Program details below:

#CONFIG
CONFIG WDTEN = ON
CONFIG STVREN = ON
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = OFF
CONFIG OSC = INTOSC ; For 8MHz clock set CONFIG OSC = INTOSC. For 32MHz clock set to INTOSCPLL.
CONFIG T1DIG = ON
CONFIG LPT1OSC = ON
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG WDTPS = 512 ; 2.1 sec.
CONFIG DSWDTOSC = INTOSCREF
CONFIG RTCOSC = INTOSCREF
CONFIG DSBOREN = OFF
CONFIG DSWDTEN = OFF
CONFIG DSWDTPS = 2048 ; 2.1 sec.
CONFIG IOL1WAY = OFF
CONFIG MSSP7B_EN = MSK5
CONFIG WPEND = PAGE_WPFP
CONFIG WPCFG = OFF
CONFIG WPDIS = OFF

DEFINE OSC 8
DEFINE USE_LFSR 1
DEFINE HSER_CLROERR 1
DEFINE HSER2_CLROERR 1
DEFINE WRITE_INT 1

a VAR BYTE

ERASECODE $F400
WRITECODE $F400, $55
READCODE $F400, a

The value of "a" always comes back as $FF. I have tested the READCODE at other locations in memory and it works. I first erase block 62. One block equals 1024 bytes then follow with the WRITECODE command. Program code protection is turned off as shown in the configuration setting. Does anything stand out here ?

- Martin

Jumper
- 26th January 2012, 11:01
So You have a few problems in your code but you need to read the datasheet carfully and understand what options you have.

You must erase before you can write a new value to the chip.

PBP supports block erase and block write but not the ONE WORD write support this device has.


If you do block block write you must write the variables you want to change + dummy writes to complete an entire block and THEN the flash is written.

Read all of chapter 7 in the datasheet especially the Notes in the right side of page 109.

It is not that hard as it seems but it is way more complicated than an EE-prom and remember you can only get 10 000 writes to the flash.. compared to 1 million if you use a standard external Microchip EEprom



Chapter 7.0 in the datasheet


The program memory space is 16 bits wide, while thedata RAM space is 8 bits wide



A read from program memory is executed on 1 byte ata time. A write to program memory is executed onblocks of 64 bytes at a time or 2 bytes at a time.Program memory is erased in blocks of 1024 bytes ata time.

7.4 Erasing Flash Program Memory
The minimum erase block is 512 words or 1024 bytes.
Only through the use of an external programmer, or
through ICSP control, can larger blocks of program
memory be bulk erased. Word erase in the Flash array
is not supported.


7.5.2 FLASH PROGRAM MEMORY WRITE
SEQUENCE (WORD PRORAMMING).
The PIC18F46J11 family of devices has a feature that
allows programming a single word (two bytes). This
feature is enabled when the WPROG bit is set.

Darrel Taylor
- 26th January 2012, 16:34
I think the 26J11 has the same kind of Flash memory as the 26J50.

Try this code, which let's you write 1-word at a time.
http://www.picbasic.co.uk/forum/showthread.php?t=15050&p=104438#post104438

readitaloud
- 2nd February 2012, 08:50
Darrel,

Thanks for the tip. The routine works very well !

- Martin