Initialize variables once

Hi,

I am new to the Rabbit so I am not too familiar with initializing persistent variables. My objective is to initialize a set of variables, defined as static, only the first time that the board is powered up. Rest of the times, they can be modified during runtime and should retain their last values after power if cutoff. So my questions are:

  1. How do I achieve this?
  2. Do I need to have a battery to achieve retention?

I am using a RCM3200 module running at 44.2MHz.

Thanks

Zulfi

Hi,

Yes you need to use a battery to maintain the ram during power off.
For the variables you need to be static, there are several options.

  1. If the values will always be the same, and never change, consider using #DEFINE rather than variables. For example…
#define BACKLIGHT_TIMER 60
void main()
{
    printf("Backlight is : %d
",BACKLIGHT_TIMER);
}

or 2. Declare the variables in the global scope, ie before any functions. Like this…

int nBacklightTimer;

void main()
{
    printf("%d 
", nBackLightTimer);
}

So then the question is how do you know if your variables have been initialised? One solution is to use a ‘magic number’ like this…

int nBacklightTimer;
int nMagicNumber;

void main()
{
    if(nMagicNumber != 0x1234)
        Init();
   
    ...
}

void Init()
{
    nMagicNumber = 0x1234;
    nBacklightTimer = 60;
    ...
}

If the power fails, or the unit is switched off and the battery is dead, the magic number will be lost, and you know you have to re-initialise all your variables.

I hope this helps.

-Kenny

Thanks for tips Kenny but I had already tested the ‘static’ part as well as the ‘magic no.’ approach. However, it didnt work until I used the ‘bbram’ keyword before the ‘static’ and changed my compilation options to ‘compile in flash and run in RAM’. I found this hint at the following link.

http://www.embeddedrelated.com/groups/rabbit-semi/show/27734.php

Other people seem to have had the same problems as myself so benefitted from their experience.

Thank you for your help, though.

Zulfi