Example from manual
code
Description The code memory type may be used for allocating constants in program memory.
Example ' puts txt in program memory
const txt = "Enter parameter" code
does not compile.
const txt = "Enter Parameter"
does compile, but no evidence of generated code in asm listing. Maybe it's hiding somewhere.
----------------
Is there a type for pointer to flash byte? So that one can read strings from flash, rather than having to resort to FLASH_Read_Byte?
Use of "code" keyword and type isn't clear.
constants in flash
-
- Posts: 158
- Joined: 24 May 2008 15:55
- Location: Germany
Help says compiler will automatically asume string in your example. But it asumes char. So does mb for 8051 - i mentioned it there. That is why you get a type mismatch.
you have to declare const txt as string[10] = "vjhdxhgfs" and it works.
You will find the constant at the end of assembled program.
The code specifier is not accepted by the compiler - i really do not know what it does - constants are always in rom???
Thopas
you have to declare const txt as string[10] = "vjhdxhgfs" and it works.
You will find the constant at the end of assembled program.
The code specifier is not accepted by the compiler - i really do not know what it does - constants are always in rom???
Thopas
Please help with my brain warping.Thopas wrote:Help says compiler will automatically asume string in your example. But it asumes char. So does mb for 8051 - i mentioned it there. That is why you get a type mismatch.
you have to declare const txt as string[10] = "vjhdxhgfs" and it works.
You will find the constant at the end of assembled program.
The code specifier is not accepted by the compiler - i really do not know what it does - constants are always in rom???
Thopas
The Help file does not discuss how pointer assignments are done. <<<
' Does this go into Flash or RAM? Does not appear in asm listing.
const mytext as string[10] = "vjhdxhgfs"
' How does one declare a pointer to an arbitrary string of any length in RAM? And in Flash? Help file mentions dim p as ^word where "word" is a typename, so I tried:
dim p as ^string[1]
' Compiler rejects all of these
p = @mytext
p = @mytext[1]
p = mytext
'and it rejects this due to mismatched types
UART1_Write_Text(@mytext)
' How does one pass a string constant in Flash (or RAM) to a library routine?
-
- Posts: 158
- Joined: 24 May 2008 15:55
- Location: Germany
1. Program has to work with the constant - only declaration part is no program. Try this:
const txt1 as string[10] = "123456789"
dim txt2 as string[10] rx '(rx if you do not want to waste ram)
dim p as ^char 'pointer to char
main:
txt2 = txt1 ' txt2 you can print
p = @txt2 +2 'points at "3456789"
p^ = "x" 'exchanges "3" to "x"
end.
compile it - look at assembly - place before symbols you find txt1 +0, txt1 +2 ... and so on.
pointer may point at text2 - not at text1 - error typemismatch
Thopas
const txt1 as string[10] = "123456789"
dim txt2 as string[10] rx '(rx if you do not want to waste ram)
dim p as ^char 'pointer to char
main:
txt2 = txt1 ' txt2 you can print
p = @txt2 +2 'points at "3456789"
p^ = "x" 'exchanges "3" to "x"
end.
compile it - look at assembly - place before symbols you find txt1 +0, txt1 +2 ... and so on.
pointer may point at text2 - not at text1 - error typemismatch
Thopas
thanks. I now understand most of the example, above.
If this puts a string in the flash (code memory) of the AVR (to save RAM):
const mytext as string[5] = "1234"
how do I declare a pointer to the flash address known as mytext?
Or brute force a longword to be the address-of mytext such as
dim pf as longword
pf = @mytext
then UART1_Write(FLASH_Read_Byte(pf)) ' and increment pf, etc.
or some such
?
If this puts a string in the flash (code memory) of the AVR (to save RAM):
const mytext as string[5] = "1234"
how do I declare a pointer to the flash address known as mytext?
Or brute force a longword to be the address-of mytext such as
dim pf as longword
pf = @mytext
then UART1_Write(FLASH_Read_Byte(pf)) ' and increment pf, etc.
or some such
?
Pointer to a constant in flash, and its use:
Code: Select all
program show
const sometext = "abcde" ' text in flash
dim ConstPtr as ^byte ' pointer to flash memory
main:
...
ConstPtr = @sometext ' point to some text
while ConstPtr <> 0 ' while not the end of text
...
PORTB = constPtr^ ' read one character
inc(ConstPtr) ' next character
...
wend
end.
So anything declared as type const is by definition always in flash?zristic wrote:Pointer to a constant in flash, and its use:
Code: Select all
program show const sometext = "abcde" ' text in flash dim ConstPtr as ^byte ' pointer to flash memory main: ... ConstPtr = @sometext ' point to some text while ConstPtr <> 0 ' while not the end of text ... PORTB = constPtr^ ' read one character inc(ConstPtr) ' next character ... wend end.
And this
const sometext = "abcde" ' text in flash
doesn't explicitly declare the type. Could be a null terminated string, could be an array of bytes. Please clarify.
Example has a byte array in flash. Can a string constant be in flash? Can an array of strings be in flash (as one often needs to do)? Or an array of pointers to strings in flash?
If you declare a simple type const, it will not be stored in flash.stevech wrote: So anything declared as type const is by definition always in flash?
If you declare a const array or a const structure then it will be stored in flash.
It is always a null terminated string which is also an array of bytes padded with a zero.And this
const sometext = "abcde" ' text in flash
doesn't explicitly declare the type. Could be a null terminated string, could be an array of bytes. Please clarify.
Yes, if declared as typed constant. See above.Example has a byte array in flash. Can a string constant be in flash?
Yes it can. If declared as const array of strings it will be stored in flash.Can an array of strings be in flash (as one often needs to do)?
Array of pointers is an array of variables and as such it cannot be stored to flash.Or an array of pointers to strings in flash?