This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NVRAM Howto...

I have never used external RAM before (beleive it or not :-o ) .
Now I am using an NVRAM from Dallas so that in case of
power failure the value of some variables is retained.
My question is how do I declare the 'Non Volatile External' variable
and how do I retreive the previous (pre-power fail) value ?

  • It sounds like your entire XDATA is going to be non-volatile, so anything you define as xdata will be non-volatile!

    "how do I retreive the previous (pre-power fail) value?"

    You just read it and, because it's non-volatile, it will still have its previous value!

  • C51 does not have any "native" support for nonvolatile RAM that I'm aware of. Using a contrived example, let's say you have a single 32K nonvolatile RAM device, 8K of which you need for "volatile" data and 24K of which you need for nonvolatile data. You would configure STARTUP.A51 for only the 8K that would hold conventional volatile C/assembly xdata objects. This way the 8K gets cleared (and optionally initialized) as part of the C runtime startup. The remaining 24K is totally up to your software to manage. That is, you must explicitly clear it when you need to and explicitly place data objects there. This is the technique I have used.

    I suppose you could also consider using assembly language modules to allocate (and externalize) data to named nonvolatile segments and have the linker locate these segments. At least this way you could use C identifier names and the linker would resolve the addresses for you.

  • Hi!

    - Declare your "nonvolatile"-variables to be placed into the xdata-space

    - do not use any initial-values

    - use a function to initialize these variables on demand (first time the system is started, but not on power-up)

    - the rest will
    Andy


  • Suppose I declare an XDATA variable in my program , would it be
    automatically intialised as zero , for example :


    
       int XDATA non_vol    _at_0x0010;    //variable at 0010h
       .
       .
       .
       if(some_event_occured)
       {
          non_vol++;                    //increment var on some event
       }
    

    Would this work ?

  • Review this thread:
    http://www.keil.com/forum/msgpage.asp?MsgID=4719

    BTW: note that the Keil C51 xdata keyword is lowercase, and you need a space between the _at_ and the address value.
    (just typos in your post?)

    BTW2: note that applying bold seems to defeat the &ltpre&gt and &lt/pre&gt tags - the font is no longer monospaced :-(

  • Memory is not initialised by the compiler. You must explicitly initialise it. If you use the startup code, you can set it up to clear any memory area you want.

    Also, unless you MUST specify addresses for RAM, why not let the linker decide where in memory to put variables? This is much easier on the developer as well as avoiding conflicts between modules.

  • If I just declare a variable it would not be initialised ba default ?

  • "If I just declare a variable it would not be initialised ba default?"

    The compiler doesn't know how much XRAM you have, nor its base address.
    Therefore, if you want your XRAM to be initialised, you must specify its size and base address - you do this by means of the startup code

    Of course, this gives you the opportunity to specify that some areas of XRAM will be initialised, while others (eg, NVRAM) will not.

    For details, refer to the thread cited earlier:
    http://www.keil.com/forum/msgpage.asp?MsgID=4719

  • Hello,
    you must declare a static variable (of course without any initialisation). Automatic variables (all variables you "just declare" are automatic) don't survive even leaving the block they was declared in, so how should they survive power off?
    Have success - Peter