constants in flash

Beta Testing discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
stevech
Posts: 38
Joined: 20 Aug 2007 16:47

constants in flash

#1 Post by stevech » 10 Dec 2008 07:45

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.

stevech
Posts: 38
Joined: 20 Aug 2007 16:47

#2 Post by stevech » 11 Dec 2008 05:33

anyone?

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

#3 Post by Thomas.Pahl@t-online.de » 11 Dec 2008 17:55

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

stevech
Posts: 38
Joined: 20 Aug 2007 16:47

#4 Post by stevech » 11 Dec 2008 21:51

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
Please help with my brain warping.
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?

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

#5 Post by Thomas.Pahl@t-online.de » 11 Dec 2008 22:46

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

stevech
Posts: 38
Joined: 20 Aug 2007 16:47

#6 Post by stevech » 12 Dec 2008 08:39

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

?

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#7 Post by zristic » 16 Dec 2008 14:13

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.

stevech
Posts: 38
Joined: 20 Aug 2007 16:47

#8 Post by stevech » 24 Dec 2008 06:40

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.
So anything declared as type const is by definition always in flash?
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?

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#9 Post by zristic » 25 Dec 2008 13:21

stevech wrote: So anything declared as type const is by definition always in flash?
If you declare a simple type const, it will not be stored in flash.
If you declare a const array or a const structure then it will be stored in flash.
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.
It is always a null terminated string which is also an array of bytes padded with a zero.

Example has a byte array in flash. Can a string constant be in flash?
Yes, if declared as typed constant. See above.

Can an array of strings be in flash (as one often needs to do)?
Yes it can. If declared as const array of strings it will be stored in flash.

Or an array of pointers to strings in flash?
Array of pointers is an array of variables and as such it cannot be stored to flash.

Post Reply

Return to “mikroBasic PRO for AVR Beta Testing”