Positron Compiler Documentation

Creating Flash Memory Tables using Dim as Code or Dim as Flash

Source: Positron8 Compiler User Manual, PDF page 412

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

These 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.

The data produced by the Code and Flash directive can 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 MyFLash as Code = as Word 1, 2, 3, 4, 5

It can also be formatted the same as Cdata with Byte, Word, Long, Dword, or Float types for the size of the constant values.

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, Flash32 or FlashF can use used, and these are the same as the directives: Code8, Code16, 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, even the integer values will all be converted into 32-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 because the TBLPTRU SFR is not required to be filled when data goes over 65535 bytes on an 18F device because it is stored under the 64K boundary, 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 that the mechanism of open and close curly braces also applies to Cdata table entries.

See also : Cdata, cPtr8, cPtr16, cPtr24, cPtr32, Cread, Cread8, Cread16, Cread24,

Cread32, Ldata, Lread.