Flash Memory Library

This library provides routines for accessing microcontroller Flash memory. Note that prototypes differ for MCU to MCU due to the amount of Flash memory.

  Important :

Library Routines

FLASH_Read_Byte

Prototype

// for MCUs with 64kb of Flash memory or less
function FLASH_Read_Byte(address : word) : byte;

// for MCUs with Flash memory larger than 64kb
function FLASH_Read_Byte(address : dword) : byte;

Returns

Returns data byte from Flash memory.

Description

Reads data from the specified address in Flash memory.

Requires

Nothing.

Example
// for MCUs with Flash memory larger than 64kb
var tmp : dword;
...

begin
  tmp := Flash_Read(0x0D00);
end

FLASH_Read_Bytes

Prototype

// for MCUs with 64kb of Flash memory or less
procedure FLASH_Read_Bytes(address : word; buffer : ^byte; NoBytes : word);

// for MCUs with Flash memory larger than 64kb
procedure FLASH_Read_Bytes(address : dword; buffer : ^byte; NoBytes : word);

Returns

Nothing.

Description

Reads number of data bytes defined by NoBytes parameter from the specified address in Flash memory to variable pointed by buffer.

Requires

Nothing.

Example
// for MCUs with Flash memory larger than 64kb
const F_ADDRESS : long = 0x200;
var dat_buff : array[32] of word;
...
begin
  FLASH_Read_Bytes(F_ADDRESS, dat_buff, 64);
end.

FLASH_Read_Word

Prototype

// for MCUs with 64kb of Flash memory or less
function FLASH_Read_Word(address : word) : word;

// for MCUs with Flash memory larger than 64kb
function FLASH_Read_Word(address : dword) : word;

Returns

Returns data word from Flash memory.

Description

Reads data from the specified address in Flash memory.

Requires

Nothing.

Example
// for MCUs with Flash memory larger than 64kb
var tmp : word;
...
begin
  tmp := Flash_Read(0x0D00);
begin

FLASH_Read_Words

Prototype

// for MCUs with 64kb of Flash memory or less
procedure FLASH_Read_Words(address : word; buffer : ^word; NoWords : word);

// for MCUs with Flash memory larger than 64kb
procedure FLASH_Read_Words(address : dword; buffer : ^word; NoWords : word);

Returns

Nothing.

Description

Reads number of data words defined by NoWords parameter from the specified address in Flash memory to variable pointed by buffer.

Requires

Nothing.

Example
// for MCUs with Flash memory larger than 64kb
const F_ADDRESS : dword = 0x200;
var dat_buff : array[32] of word;
...
begin
  FLASH_Read_Words(F_ADDRESS,dat_buff, 32);

FLASH_Write_Page

Prototype

procedure FLASH_Write_Page(address : word; dData : ^word);

// for XMEGA family of MCUs

procedure FLASH_Write_Application_Page(address : word; wData : ^word);

procedure FLASH_Write_Boot_Page(address : word; wData : ^word);

procedure FLASH_Erase_Write_Application_Page(address : word; wData : ^word);

procedure FLASH_Erase_Write_Boot_Page(address : word; wData : ^word);

Returns

Nothing.

Description

Writes a data array pointed to by dData parameter to the specified address in Flash memory.

For XMEGA family of MCU's you can write to the both boot and application data section. Also, you can erase the appropriate page prior to writing.

Parameters :

  • address: address of the page used for writting.
  • dData: pointer to a data array which is being written.
Requires

Nothing.

Example
var WRITE_ADDRESS : word;
var dat_buff : array[8] of word;
...
begin
  WRITE_ADDRESS := 0x200;
  FLASH_Read_Words(WRITE_ADDRESS,dat_buff);
end.

FLASH_Erase_Page

Prototype

procedure FLASH_Erase_Page(address : word);

// for XMEGA family of MCUs

procedure FLASH_Erase_Boot_Page(address : word);

procedure FLASH_Erase_Application_Page(address : word);

Returns

Nothing.

Description

Erases specified page in the Flash memory.

For XMEGA family of MCU's you can erase both boot and application data section.

Parameters :

  • address: address of the page being erased.
Requires

Nothing.

Example
var ERASE_ADDRESS : word;
...
begin
  WRITE_ADDRESS := 0x200;
  FLASH_Read_Words(ERASE_ADDRESS);
end.

Library Example

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

Copy Code To ClipboardCopy Code To Clipboard
program Flash_MCU_test;

const F_ADDRESS : longint = 0x200;

const data_ : array[32] of word = (                               // constant table
  0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
  0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F,
  0x0000,0x0100,0x0200,0x0300,0x0400,0x0500,0x0600,0x0700,
  0x0800,0x0900,0x0A00,0x0B00,0x0C00,0x0D00,0x0E00,0x0F00
 ); org 0x200;

var counter : byte;
    word_ : word;
    dat_buff : array[32] of word;

begin
  DDRD := 0xFF;                              // set direction to be output
  DDRB := 0xFF;                              // set direction to be output
  word_ := data_[0];                         //  link const table

 counter := 0;
  while ( counter < 64 ) do                             //  reading 64 bytes in loop
    begin
      PORTD := FLASH_Read_Byte(F_ADDRESS + counter);    //  demonstration of reading single byte
      Inc(counter);
      PORTB := FLASH_Read_Byte(F_ADDRESS + counter);    //  demonstration of reading single byte
      Inc(counter);
      Delay_ms(200);
    end;

 FLASH_Read_Bytes(F_ADDRESS, @dat_buff, 64);            //  demonstration of reading 64 bytes
  for counter := 0 to 31 do
    begin
      PORTD := dat_buff[counter];                       //  output low byte to PORTD
      PORTB := word((dat_buff[counter] shr 8));         //  output higher byte to PORTB
      Delay_ms(200);
    end;

  counter := 0;
  while (counter <= 63) do                              // reading 32 words in loop
    begin
      word_  := FLASH_Read_Word(F_ADDRESS + counter);   //  demonstration of reading single word
      PORTD  := word_;                                  //  output low byte to PORTD
      PORTB  := word(word_ shr 8);                      //  output higher byte to PORTB
      counter := counter + 2;
      Delay_ms(200);
    end;

 
  FLASH_Read_Words(F_ADDRESS, @dat_buff, 32);           //  demonstration of reading 64 bytes
  for counter := 0 to 31 do
    begin
      PORTD := dat_buff[counter];                       //  output low byte to PORTD
      PORTB := word((dat_buff[counter] shr 8));         //  output higher byte to PORTB
      Delay_ms(200);
    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