Creating and using Flash Memory Strings
All new, flash based, microcontroller devices have the ability to read and write to their own flash memory, and although writing to this memory too many times is unhealthy for the microcontroller, reading this memory is both fast and harmless. Which offers a unique form of data storage and retrieval, the Cdata command, or better still, the new Dim as Flashx directives prove this, as they uses the mechanism of reading and storing in the microcontroller's flash memory.
Combining the features of reading the Flash memory with a string format, the compiler is capable of reducing the overhead of printing, or transmitting large amounts of text data. The Cstr modifier may be used in commands that deal with text processing on standard 14-bit core devices, i.e. Print, Serout, HRsout, and Rsout . However, with newer devices, the Cstr directive is not required.
On older standard 14-bit core devises, the Cstr modifier can be used in conjunction with flash memory tables. The Dim as Flashx directives are used for initially creating the string of characters in flash memory: -
Dim FlashString1 as Flash8 = "Hello World", 0
The above line of code will create, in flash memory, the 8-bit values that make up the ASCII text "Hello World", at address FlashString1. Note the null terminator after the ASCII text.
Null terminated means that a zero (null) is placed at the end of the string of ASCII characters to signal that the string has finished.
To display, or transmit this string of characters, the following command structure could be used:
Print FlashString1
The label that declared the address where the list of flash memory values resided, now becomes the string's name. In a large program with lots of text formatting, this type of structure can save quite literally hundreds of bytes of valuable code space.
The term 'virtual string' relates to the fact that a string formed in flash memory cannot (rather should not) be written too, but only read from.
Not only label names can be used with the Cstr modifier, constants, variables and expressions can also be used that will hold the address of flash memory table’s label (a pointer). For example, the program below uses a Word size variable to hold 2 pointers (address of a label, variable or array) to 2 individual null terminated text strings formed by Dim As Flash8.
Example 1
Device = 18F26K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
' Set the Baud Rate for HRsoutLn
' Create the text to display in flash memory
Dim String1 as Flash8 = "Hello ", 0
Dim String2 as Flash8 = "World", 0
Dim wAddress as Word
' Pointer variable
wAddress = String1
' Point address to string 1
HRsoutLn Cstr wAddress
' Display string 1
wAddress = String2
' Point Address to string 2
HRsoutLn Cstr wAddress
' Display string 2
It is also possible to eliminate the Cstr modifier altogether and place the label’s name directly with enhanced 14-bit core devices, 18F devices, PIC24 devices and dsPIC33 devices. The compiler will see this as an implied Cstr and act accordingly. For example:
Example 2
Device = 18F26K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
' Set the Baud Rate for HRsoutLn
' Create the text to display in flash memory
Dim FlashString1 as Flash8 = "Hello ", 0
Dim FlashString2 as Flash8 = "World", 0
HRsoutLn FlashString1
' Display the text in FlashString1 on a serial terminal
HRsoutLn FlashString2
' Display the text in FlashString2 on a serial terminal