Microchip Technology MA240029 データシート

ページ / 406
PIC24FJ128GA310 FAMILY
DS39996F-page 88
 2010-2011 Microchip Technology Inc.
6.6.2
PROGRAMMING A SINGLE WORD 
OF FLASH PROGRAM MEMORY
If a Flash location has been erased, it can be pro-
grammed using table write instructions to write an
instruction word (24-bit) into the write latch. The
TBLPAG register is loaded with the 8 Most Significant
Bytes (MSBs) of the Flash address. The TBLWTL and
TBLWTH
 instructions write the desired data into the
write latches and specify the lower 16 bits of the pro-
gram memory address to write to. To configure the
NVMCON register for a word write, set the NVMOP bits
(NVMCON<3:0>) to ‘0011’. The write is performed by
executing the unlock sequence and setting the WR bit
(see 
). An equivalent procedure in ‘C’
compiler, using the MPLAB C30 compiler and built-in
hardware functions, is shown in 
EXAMPLE 6-5:
PROGRAMMING A SINGLE WORD OF FLASH PROGRAM MEMORY
EXAMPLE 6-6:
PROGRAMMING A SINGLE WORD OF FLASH PROGRAM MEMORY 
(‘C’ LANGUAGE CODE)
; Setup a pointer to data Program Memory
MOV
#tblpage(PROG_ADDR), W0
;
MOV
W0, TBLPAG
;Initialize PM Page Boundary SFR
MOV
#tbloffset(PROG_ADDR), W0
;Initialize a register with program memory address
MOV
#LOW_WORD_N, W2
;
MOV
#HIGH_BYTE_N, W3
;
TBLWTL
W2, [W0]
; Write PM low word into program latch
TBLWTH
W3, [W0++] 
; Write PM high byte into program latch
; Setup NVMCON for programming one word to data Program Memory
MOV
#0x4003, W0
;
MOV
W0, NVMCON
; Set NVMOP bits to 0011
DISI
#5
; Disable interrupts while the KEY sequence is written
MOV.B
#0x55, W0
; Write the key sequence
MOV
W0, NVMKEY
MOV.B
#0xAA, W0
MOV
W0, NVMKEY
BSET
NVMCON, #WR
; Start the write cycle
NOP
; Required delays
NOP
// C example using MPLAB C30
unsigned int offset;
unsigned long progAddr = 0xXXXXXX;
// Address of word to program
unsigned int progDataL = 0xXXXX;
// Data to program lower word
unsigned char progDataH = 0xXX;
// Data to program upper byte
//Set up NVMCON for word programming
NVMCON = 0x4003;
// Initialize NVMCON
//Set up pointer to the first memory location to be written
TBLPAG = progAddr>>16;
// Initialize PM Page Boundary SFR
offset = progAddr & 0xFFFF;
// Initialize lower word of address
//Perform TBLWT instructions to write latches
__builtin_tblwtl(offset, progDataL);
// Write to address low word
__builtin_tblwth(offset, progDataH);
// Write to upper byte
asm(“DISI #5”);
// Block interrupts with priority <7
// for next 5 instructions
__builtin_write_NVM();
// C30 function to perform unlock
// sequence and set WR