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
unsigned FLASH_Read(unsigned address);

// for PIC18
unsigned short FLASH_Read(long address);

Returns

Returns data byte from Flash memory.

Description

Reads data from the specified address in Flash memory.

Requires

Nothing.

Example
// for PIC18
unsigned short tmp;
...
tmp = FLASH_Read(0x0D00);
...

FLASH_Read_N_Bytes

Prototype

void FLASH_Read_N_Bytes(long address, char* data_, unsigned int N);

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
void FLASH_Write(unsigned address, unsigned int* data);

// for PIC18

void FLASH_Write_8(long address, char* data);

void FLASH_Write_16(long address, char* data);

void FLASH_Write_32(long address, char* data);

void FLASH_Write_64(long address, char* data);

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:

unsigned short toWrite[64];
...
// initialize array:
for (i = 0; i < 64; i++)
  toWrite[i] = i;

// write contents of the array to the address 0x0D00:
FLASH_Write_64(0x0D00, toWrite);

FLASH_Erase

Prototype // for PIC16

void FLASH_Erase(unsigned address);

// for PIC18

void FLASH_Erase_64(long address);

void FLASH_Erase_1024(long address);

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 0x0D00:

FLASH_Erase_64(0x0D00);

FLASH_Erase_Write

Prototype // for PIC18

void FLASH_Erase_Write_64(long address, char* data);

void FLASH_Erase_Write_1024(long address, char* data);

Returns

None.

Description

Erase then write memory block starting from a given address.

Requires

Nothing.

Example
char toWrite[64];
int i;
...
// initialize array:
for(i=0; i<64; i++) 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);

Library Example

The example demonstrates simple write to the flash memory for PIC16F887, then reads the data and displays it on PORTB and PORTC.

Copy Code To ClipboardCopy Code To Clipboard
char i = 0;
unsigned int addr, data_, dataAR[4][4] = {{0x3FAA+0,  0x3FAA+1,  0x3FAA+2,  0x3FAA+3},
                                          {0x3FAA+4,  0x3FAA+5,  0x3FAA+6,  0x3FAA+7},
                                          {0x3FAA+8,  0x3FAA+9,  0x3FAA+10, 0x3FAA+11},
                                          {0x3FAA+12, 0x3FAA+13, 0x3FAA+14, 0x3FAA+15}};

void main() {
  ANSEL  = 0;                         // Configure AN pins as digital
  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);

  // 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; i < 4; i++){            // Write some data to Flash
    Delay_ms(100);
    FLASH_Write(addr+i*4, dataAR[i]);
  }
  Delay_ms(500);

  addr = 0x0430;
  for (i = 0; i < 16; i++){
    data_ = FLASH_Read(addr++);       // P16's FLASH is 14-bit wide, so
    Delay_us(10);                     //   two MSB's will always be '00'
    PORTB = data_;                    // Display data on PORTB (LS Byte)
    PORTC = data_ >> 8;               // and PORTC (MS Byte)
    Delay_ms(500);
  }
}
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