Creating Flash Memory Tables using Dim
There are two special cases of the Dim directive. These are for creating data tables in standard Flash memory and in the PSV section of Flash memory:
Dim MyFlashTable as Code
or
Dim MyFlashTable as Flash
and
Dim MyCode as PSV
All the above will create a data table in the device's flash memory, however, the PSV directive will ensure that the AddressOf function returns the PSV address of the table, instead of its actual flash memory address. This is used mainly for DSP operations.
The data produced by the Code, Flash or PSV directives follow the same casting rules as the Cdata directive, in that the table's data can be given a size that each element will occupy.
Dim MyCode as Code = as Word 1, 2, 3, 4, 5
or
Dim MyCode as PSV = As Dword 100, 200, 300, 400
It can also be formatted the same as Cdata with Byte, Word, Dword, or Float types for the size of the constant values.
However, there is a special case of the Dim directive. This is:
Dim MyFlashTable as Code
or
Dim MyFlashTable as Code8
or
Dim MyFlashTable as Code16
or
Dim MyFlashTable as Code24
or
Dim MyFlashTable as Code32
or
Dim MyFlashTable as CodeF
or
Dim MyFlashTable as Flash
or
Dim MyFlashTable as Flash8
or
Dim MyFlashTable as Flash16
or
Dim MyFlashTable as Flash24
or
Dim MyFlashTable as Flash32
or
Dim MyFlashTable as FlashF
This will create a data table in the device's code (Flash) memory, which is what is also known as non-volatile flash memory. Both the Code and Flash directives operate exactly the same, they just use a different name for Flash memory, that used to be named as Code memory.
To ensure that the entire flash memory table’s data is all of a common size, the Code8, Code16, Code24, Code32 or CodeF directives can be used instead of the standard Code directive. These will make the table’s constant values either, all, 8-bit, 16-bit, 24-bit, 32-bit or floating point, regardless of their value.
For example, to make a table of 16-bit values only:
Dim MyFLash as Code16 = 1, 2, 3, 4, 5
Even though the table’s constant values are less that 16-bit in size, they will be internally cast to ensure they require the flash memory for 16-bit values, thus making the table usable for the Cread16 or cPtr16 commands. If the values are larger that 16-bit, they will be shortened to fit a 16-bit value.
The directives Flash8, Flash16, Flash24, Flash24, Flash32 or FlashF can use used, and these are the same as the directives: Code8, Code16, Code24, Code24, Code32 and CodeF, but use a different name because the internal program memory used to be called ‘Code’ memory, but is now called ‘Flash’ memory within datasheets.
1.) Create a flash memory ASCII character table. Note the Null termination at the end of it:
Dim FlashString as Flash8 = "Hello Word, How are you?", 0
2.) Create a flash memory table consisting of 8-bit constant values only:
Dim FlashTable as Flash8 = 0, 1, 1024, 123456, 1234567890
In the above table, the longer values will all be truncated to fit 8-bits.
3.) Create a flash memory table consisting of 16-bit constant values only:
Dim FlashTable as Flash16 = 0, 1, 1024, 123456, 1234567890
In the above table, the longer values will all be truncated to fit 16-bits and the shorter values will be padded to also fit 16-bits.
4). Create a flash memory table consisting of 24-bit constant values only:
Dim FlashTable as Flash24 = 0, 1, 1024, 123456, 1234567890
In the above table, the longer values will all be truncated to fit 24-bits and the shorter values will be padded to also fit 24-bits.
5). Create a flash memory table consisting of 32-bit constant values only:
Dim FlashTable as Flash32 = 0, 1, 1024, 123456, 1234567890
In the above table, the longer values will all be truncated to fit 32-bits and the shorter values will be padded to also fit 32-bits.
6). Create a flash memory table consisting of 32-bit floating point constant values only:
Dim FlashTable as FlashF = 0, 3.14, 1024.9, 123456, 1234567
In the above table, the integer values will all be converted into 32-bit floating point constants.
7). Create a flash memory table consisting of 64-bit floating point constant values only:
Dim FlashTable as FlashD = 0, 3.14, 1024.9, 123456, 1234567
In the above table, the integer values will all be converted into 64-bit floating point constants.
Dim as CodeX or Dim As FlashX are a better choice for data tables because the compiler will store the tables in low flash memory and not inline as with Cdata, so a Dim as CodeX or Dim As FlashX directive can be placed anywhere in the program and it will not interfere with the program’s flow. It also helps to optimise programs, unless the flash memory data itself is larger than 65536 bytes.
When a Flash memory table runs to more than a single line, it can be continued to the following lines, either with the line continuation characters ‘,_’. For example:
Dim FlashTable as Flash16 = 1000, 2000, 3000, 4000, 5000, 6000,_
7000, 8000, 9000, 10000, 11000, 12000,_
13000, 14000, 15000, 16000, 17000
Or the table data can start with an open curly brace character ‘{‘, then only commas after each line are required until it finds a closing curly brace, ‘}’. For example, the above can be written as:
Dim FlashTable as Flash16 = {1000, 2000, 3000, 4000, 5000, 6000,
7000, 8000, 9000, 10000, 11000, 12000,
13000, 14000, 15000, 16000, 1700}
The maximum amount of flash memory data items depends on the amount of Flash memory the device contains.
Note.
The mechanism of open and close curly braces also applies to Cdata table entries.
A Flash memory or PSV memory data items will not be included into a program if it is not used somewhere within the program as a parameter, in a command, or assignment, or operand, so it will not occupy flash memory if not used.


