View previous topic :: View next topic |
Author |
Message |
mkuang
Joined: 14 Dec 2007 Posts: 257
|
How to declare persistent global variable? |
Posted: Tue Jun 16, 2009 2:35 pm |
|
|
Hello,
I am trying to declare a persistent global variable and the compiler is giving me errors.
Right before main() I write:
Code: |
persistent int cal_cancelled_flag;
|
And the compiler is giving me an error:
*** Error 48 "Line 38(12,22): Expecting a (
*** Error 48 " Line 38(16,34): Expecting a (
Now if I remove the persistent label the code is fine.
I am using PCH 4.064 compiler with 18f2525. |
|
|
ak6dn
Joined: 08 Jan 2006 Posts: 23 Location: Saratoga, CA USA
|
Re: How to declare persistent global variable? |
Posted: Tue Jun 16, 2009 2:48 pm |
|
|
mkuang wrote: | Now if I remove the persistent label the code is fine.
|
That is a clue. 'persistent' is not a language keyword that CCS C defines. Have you read their manual?
If you want persistent storage, you need to manage it manually in a device that has EEPROM data storage. _________________ Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll never see him on the weekend. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 16, 2009 2:59 pm |
|
|
In C, the traditional persistent variable, is declared with the 'static' keyword. Persistent, is not a normal C keyword.
The difference between 'persistent', and 'global', in the normal meaning of the term is that a persistent variable is local in scope.
Some 'third party' languages, use the 'global persistent' keyword in C, for a variable that is maintained after power is removed. this does not exist in CCS, mainly because the only memory that can be used for this, is the EEPROM, which suffers from severe timing overhead, when written, and limited write life, making it unsuitable for 'variables'.
If you want to make a variable behave as 'global persistent', then _you_ need to read it from EEPROM on boot, and write it (preferably _infrequently_), when changed.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jun 16, 2009 3:08 pm |
|
|
Thanks guys for all your help. |
|
|
|