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 :
- Read operation supported. For this group of MCU's only read function is implemented.
- 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.
- 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).
Library Routines
- FLASH_Read
- FLASH_Read_N_Bytes
- FLASH_Write
- FLASH_Write_8
- FLASH_Write_16
- FLASH_Write_32
- FLASH_Write_64
- FLASH_Erase
- FLASH_Erase_64
- FLASH_Erase_1024
- FLASH_Erase_Write
- FLASH_Erase_Write_64
- FLASH_Erase_Write_1024
FLASH_Read
| Prototype |
' for PIC16 ' for PIC18 |
|---|---|
| Returns |
Returns data byte from Flash memory. |
| Description |
Reads data from the specified |
| Requires |
Nothing. |
| Example |
' for PIC18 dim tmp as byte ... main: ... tmp = FLASH_Read(0x0D00) ... end. |
FLASH_Read_N_Bytes
| Prototype |
' for PIC18 |
|---|---|
| Returns |
Nothing. |
| Description |
Reads N data from the specified |
| Requires |
Nothing. |
| Example |
FLASH_Read_N(0x0D00,data_buffer,sizeof(data_buffer)) |
FLASH_Write
| Prototype |
' for PIC16 sub procedure FLASH_Write(dim Address as word, dim byref Data as word[4])
' for PIC18 sub procedure FLASH_Write_8(dim address as longword, dim byref data as byte[8]) sub procedure FLASH_Write_16(dim address as longword, dim byref data as byte[16]) sub procedure FLASH_Write_32(dim address as longword, dim byref data as byte[32]) sub procedure FLASH_Write_64(dim address as longword, dim byref data as byte[64]) |
|---|---|
| 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:
dim toWrite as byte[64]
...
main:
...
' initialize array:
for i = 0 to 63
toWrite[i] = i
next i
...
' write contents of the array to the address 0x0D00:
FLASH_Write_64(0x0D00, toWrite)
...
end.
|
FLASH_Erase
| Prototype |
' for PIC16
' for PIC18 sub procedure FLASH_Erase_64(dim address as longword) sub procedure FLASH_Erase_1024(dim address as longword) |
|---|---|
| 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 sub procedure FLASH_Erase_Write_64(dim address as longword, dim byref data as byte[64]) sub procedure FLASH_Erase_Write_1024(dim address as longword, dim byref data as byte[1024]) |
|---|---|
| Returns |
None. |
| Description |
Erase then write memory block starting from a given address. |
| Requires |
Nothing. |
| Example |
dim toWrite as byte[64]
...
main:
...
' initialize array:
for i = 0 to 63
toWrite[i] = i
next 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.
program Flash_Write
dim counter as byte
addr, data_ as word
dataAR as word[4][4]
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.
dataAR[0][0]= 0x3FAA+0
dataAR[0][1]= 0x3FAA+1
dataAR[0][2]= 0x3FAA+2
dataAR[0][3]= 0x3FAA+3
dataAR[1][0]= 0x3FAA+4
dataAR[1][1]= 0x3FAA+5
dataAR[1][2]= 0x3FAA+6
dataAR[1][3]= 0x3FAA+7
dataAR[2][0]= 0x3FAA+8
dataAR[2][1]= 0x3FAA+9
dataAR[2][2]= 0x3FAA+10
dataAR[2][3]= 0x3FAA+11
dataAR[3][0]= 0x3FAA+12
dataAR[3][1]= 0x3FAA+13
dataAR[3][2]= 0x3FAA+14
dataAR[3][3]= 0x3FAA+15
addr = 0x0430 ' starting Flash address, valid for P16F887
for counter = 0 to 3 ' write some data to Flash
Delay_ms(100)
FLASH_Write(addr+counter*4, dataAR[counter])
next counter
Delay_ms(500)
addr = 0x0430
for counter = 0 to 15
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 = word(data_ >> 8) ' and PORTC MS Byte
Delay_ms(500)
next counter
end.
What do you think about this topic ? Send us feedback!




