Flash Memory Library

This library provides routines for accessing microcontroller Flash memory. Note that prototypes differ for PIC16 and PIC18 families.

  Important : Due to the P16/P18 family flash specifics, flash library is MCU dependent. Since the P18 family differ significantlly in number of bytes that can be erased and/or written to specific MCUs, the appropirate suffix is added to the names of functions in order to make it easier to use them.

Flash memory operations are MCU dependent :
  1. Read operation supported. For this group of MCU's only read function is implemented.
  2. Read and Write operations supported (write is executed as erase-and-write). For this group of MCU's read and write functions are implemented. Note that write operation which is executed as erase-and-write, may write less bytes than it erases.
  3. Read, Write and Erase operations supported. For this group of MCU's read, write and erase functions are implemented. Further more, flash memory block has to be erased prior to writting (write operation is not executed as erase-and-write).
Please refer to MCU datasheet before using flash library.

Library Routines

FLASH_Read

Prototype

// for PIC16
function FLASH_Read(Address: word): word;

// for PIC18
function FLASH_Read(address : dword) : byte;

Returns

Returns data byte from Flash memory.

Description

Reads data from the specified address in Flash memory.

Requires

Nothing.

Example
// for PIC18
var tmp : byte;
...
begin
  ...
  tmp := FLASH_Read(0x0D00);
  ...
end.

FLASH_Read_N_Bytes

Prototype

// for PIC18
procedure FLASH_Read_N_Bytes(address : longint; var data : byte; N : word);

Returns

Nothing.

Description

Reads N data from the specified address in Flash memory to varibale pointed by data

Requires

Nothing.

Example
FLASH_Read_N(0x0D00, data_buffer, sizeof(data_buffer));

FLASH_Write

Prototype

// for PIC16
procedure FLASH_Write(Address : word; var Data : array[4] of word);

// for PIC18

procedure FLASH_Write_8(address : dword; var data: array[8] of byte);

procedure FLASH_Write_16(address : dword; var data: array[16] of byte);

procedure FLASH_Write_32(address : dword; var data: array[32] of byte);

procedure FLASH_Write_64(address : dword; var data: array[64] of byte);

Returns

Nothing.

Description

Writes block of data to Flash memory. Block size is MCU dependent.

P16: This function may erase memory segment before writing block of data to it (MCU dependent). Furthermore, memory segment which will be erased may be greater than the size of the data block that will be written (MCU dependent). Therefore it is recommended to write as many bytes as you erase. FLASH_Write writes 4 flash memory locations in a row, so it needs to be called as many times as it is necessary to meet the size of the data block that will be written.

P18: This function does not perform erase prior to write.

Requires

Flash memory that will be written may have to be erased before this function is called (MCU dependent). Refer to MCU datasheet for details.

Example

Write consecutive values in 64 consecutive locations, starting from 0x0D00:

var toWrite : array[64] of byte;
...
begin
...
// initialize array:
for i := 0 to 63 do  
    toWrite[i] := i;
...
// write contents of the array to the address 0x0D00:
FLASH_Write_64(0x0D00, toWrite);
...
end.

FLASH_Erase

Prototype // for PIC16

procedure FLASH_Erase(address : word);

// for PIC18

procedure FLASH_Erase_64(address : dword);

procedure FLASH_Erase_1024(address : dword);

Returns

Nothing.

Description

Erases memory block starting from a given address. For P16 familly is implemented only for those MCU's whose flash memory does not support erase-and-write operations (refer to datasheet for details).

Requires

Nothing.

Example

Erase 64 byte memory memory block, starting from address $0D00:

FLASH_Erase_64($0D00);

FLASH_Erase_Write

Prototype // for PIC18

procedure FLASH_Erase_Write_64(address : dword; var data : array[64] of byte);

procedure FLASH_Erase_Write_1024(address : dword; var data : array[1024] of byte);

Returns

None.

Description

Erase then write memory block starting from a given address.

Requires

Nothing.

Example
var toWrite : array[64] of byte;
...
begin
...
// initialize array:
for i := 0 to 63 do  
    toWrite[i] := i;
...
// erase block of memory at address 0x0D00 then write contents of the array to the address 0x0D00:
FLASH_Erase_Write_64(0x0D00, toWrite);
...
end.

Library Example

This is a simple demonstration how to use to PIC16 internal flash memory to store data. The data is being written starting from the given location; then, the same locations are read and the data is displayed on PORTB and PORTC.

Copy Code To ClipboardCopy Code To Clipboard
program Flash_Write;

var i : byte;
    addr, data_ : word;
    dataAR: array[4] of array[4] of word;

begin
  ANSEL  := 0;                         // configure AN pins as digital I/O
  ANSELH := 0;
  C1ON_bit := 0;                       // Disable comparators
  C2ON_bit := 0;
  
  PORTB := 0;                          // initial PORTB value
  TRISB := 0;                          // set PORTB as output
  PORTC := 0;                          // initial PORTC value
  TRISC := 0;                          // set PORTC as output
  Delay_ms(500);

  dataAR[0][0]:= $FFAA+0;
  dataAR[0][1]:= $FFAA+1;
  dataAR[0][2]:= $FFAA+2;
  dataAR[0][3]:= $FFAA+3;
  dataAR[1][0]:= $FFAA+4;
  dataAR[1][1]:= $FFAA+5;
  dataAR[1][2]:= $FFAA+6;
  dataAR[1][3]:= $FFAA+7;
  dataAR[2][0]:= $FFAA+8;
  dataAR[2][1]:= $FFAA+9;
  dataAR[2][2]:= $FFAA+10;
  dataAR[2][3]:= $FFAA+11;
  dataAR[3][0]:= $FFAA+12;
  dataAR[3][1]:= $FFAA+13;
  dataAR[3][2]:= $FFAA+14;
  dataAR[3][3]:= $FFAA+15;

  // All block writes
  // to program memory are done as 16-word erase by
  // eight-word write operations. The write operation is
  // edge-aligned and cannot occur across boundaries.
  // Therefore it is recommended to perform flash writes in 16-word chunks.
  // That is why lower 4 bits of start address [3:0] must be zero.
  // Since FLASH_Write routine performs writes in 4-word chunks,
  // we need to call it 4 times in a row.
  addr := 0x0430;                      // starting Flash address, valid for P16F887
  for i := 0 to 3 do
    begin                              // write some data to Flash
      Delay_ms(100);
      Flash_Write(addr+i*4, dataAR[i]);
    end;
  Delay_ms(500);

  addr := 0x0430;
  for i := 0 to 15 do
    begin
      data_ := Flash_Read(addr);        // P16's FLASH is 14-bit wide, so
      Inc(addr);
      Delay_us(10);                     //   two MSB's will always be '00'
      PORTB := data_;                   // display data on PORTB LS Byte
      PORTC := Hi(data_);               // and PORTC MS Byte
      Delay_ms(500);
    end;
end.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code