Microchip Technology PICkit 3 Debug Express Debugger/Programmer (DV164131) PICkit 3 Debug Express DV164131 Fiche De Données

Codes de produits
DV164131
Page de 78
PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc.
 
DS41370C-page 65
FIGURE 3-53:
EXAMPLE PROGRAM MEMORY WRITE FUNCTION
3.11.2
Protecting Program Memory in the Configuration Bits.
The program is divided into sections that can individually be protected by setting the 
appropriate Configuration bits. The protections available are:
Code Protect – The CPx bits prevent microcontroller programmers such as the 
PICkit 3 from reading the contents of program memory in the address range asso-
ciated with the particular CPx Configuration bit. If a programmer attempts to read 
a code-protected section of memory, all locations will read as value 0x00. This 
prevents other parties from stealing proprietary program code.
Write Protect – When a WRTx Configuration bit is ON, then program memory 
erase or write operations are prohibited from working on the associated range of 
memory. This could be used to protect a bootloader from accidental corruption by 
inadvertent application program memory writes or erases.
Table Read Protect – The EBTRx bits, when asserted, prevent program memory 
locations being read from instructions executing in another program memory 
block. For example, if EBTR3 was asserted, then program memory locations from 
0x6000 to 0x7FFF by any code executing from program memory locations 0x0000 
to 0x5FFF. Locations in the block 0x6000 to 0x7FFF could still be read by code 
executing in that block. This could be used, for example, to prevent using a boot-
loader to read out sensitive code-protected data.
Once these protective Configuration bits have been asserted (set to ON), they cannot 
be turned off or changed without a programmer executing a Bulk Erase on the 
microcontroller, which erases all program memory and data EEPROM memory. It is 
possible to prevent other Configuration bits from being changed after the device is 
initially programmed using the WRTC Configuration bit.
unsigned  char  ProgMemWr32(unsigned  int  address,  unsigned  char  *buffer_ptr)
{  //  NOTE:  program  memory  must  also  be  erased  first.
   near  rom  unsigned  char  *ptr;
   char  i;
   ptr  =  (rom  unsigned  char  *)(address  &  0xFFE0);//  ensure  write  starts  on  32-byte  boundary
   for  (i  =  0;  i  <  32;  i++)
   {
   *(ptr  +  i)  =  buffer_ptr[i];
//
write  the  data  into  the  holding  registers
   }
   EECON1bits.EEPGD  =  1;
//
write  to  flash  program  memory
   EECON1bits.CFGS  =  0;
//
not  configuration  registers
   EECON1bits.FREE  =  0;
//
we're  not  erasing  now.
   EECON1bits.WREN  =  1;
//
enable  write/erase  operations
   //  execute  code  sequence,  which  cannot
be  interrupted,  then  execute  write32
   INTCONbits.GIE  =  0;
//  Disable  interrupts
   EECON2  =  0x55;
//  Begin  Write
sequence
   EECON2  =  0xAA;
   EECON1bits.WR  =  1;
//  Set  WR  bit  to  begin  32-byte  write
   INTCONbits.GIE  =  1;
//  re-enable  interrupts
   EECON1bits.WREN  =  0;
//
disable  write/erase  operations
}