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

problem in initilise static variable

Hi,

I am using c51 keil compiler with cygnal chip c8051C020.
I find problem in initilise static variable.
exp.
main()
{
static unsigned char i =0 ;
i++;
}
when i write this type of code & simulate it ,i find that i is not initilise to 0.

Note:this is just example code & not the actual program i work.
If any specify me the reasion it will help me lot.

Regards,
Pandurang S.

Parents Reply Children
  • hi,

    Thanks to all for immediate reply.
    "Including those files in your project will not make any difference unless you modify them"

    What modification i have to make?

    Regards,
    Pandurang S.

  • those required for your chip

    here is an example for a f12x

    STARTUP1:
    
    ;----------------------------------------------------------------
    ; Watchdog Timer Configuration
               mov   SG_WDTCN,#0DEh        ; Disable WDT
               mov   SG_WDTCN,#0ADh        ; takes 2 writes
    
    ; CROSSBAR REGISTER CONFIGURATION
               mov   SG_SFRPAGE,#00Fh
               mov   SF_XBR0, #004h                   ; XBAR0: UART0 enabled
               mov   SF_XBR1, #00ch                   ; XBAR1: INT0, T1 to port pin
               mov   SF_XBR2, #040h                   ; XBAR2: crossbar enbl & non-mpx memory
    ; Select Pin I/0
               mov SG_SFRPAGE, #00Fh;
               mov SF_P0MDOUT, #011h ; Output configuration for SG_P0 TxD PP, remaining open drain
               mov SF_P1MDOUT, #0ffh ; Output configuration for SG_P1 all are push-pull
               mov SF_P2MDOUT, #0ffh ; Output configuration for SG_P2 all are push-pull
               mov SF_P3MDOUT, #0ffh ; Output configuration for SG_P3 all are push-pull
               mov SF_P4MDOUT, #0c3h ; Output configuration for SF_P4 mixed
               mov SF_P5MDOUT, #0ffh ; Output configuration for SF_P5 all are push-pull
               mov SF_P6MDOUT, #0ffh ; Output configuration for SF_P6 all are push-pull
               mov SF_P7MDOUT, #0ffh ; Output configuration for SF_P7 all are open drain
    
               mov SF_P1MDIN, #0FFh  ; Input configuration for P1
    
               mov   SG_SFRPAGE,#000h
               mov   S0_EMI0CF,#038h    ; External Memory Configuration Register
    
    
    ; Oscillator Configuration
    ; internaql oswcillator, system clock = 48MHz
        mov    SG_SFRPAGE,#000h
        mov    S0_FLSCL,#090h
        mov    SG_SFRPAGE,#00fh
        orl    SF_PLL0CN,#001h
        mov    SF_PLL0DIV,#001h
        mov    SF_PLL0FLT,#021h
        mov    SF_PLL0MUL,#002h
        clr    A                     ; Wait 5us for initialization
        djnz   SG_ACC, $
        orl    SF_PLL0CN,    #002h
    Pll_Wait:
        mov    A,SF_PLL0CN
        jnb    SG_ACC.4,Pll_Wait
        mov    SF_OSCICN,#083h
        mov    SF_CLKSEL,#002h
    
               mov   SG_SFRPAGE,#000h
               mov   S0_RSTSRC,#002h       ; Reset Source Register
               mov   S0_EMI0TC,#000h       ; External Memory Timing
    
    ; clear (i)data

    FYI the first and last line are from unmodified startup to show where to insert.

    OF course you will have to use config2 to make the equivalent for your processor and port etc use.

    Erik

  • Hi Erik,

    Thanks once again.
    The example you given quite same as the config file generated by cofig2.
    But still i am not clear how to init static variable (may be to 0 or other than 0 value) that are used in a function,which is call by my main function.
    What is the relation betn the example u given & library files(init.a51 & startup.a51).

    Regards,
    Pandurang S.

  • Hi Erik,... the example u given

    I can not comment on an example given by micro.

    Erik

  • Hi,

    Leave the part of example.
    "But still i am not clear how to init static variable (may be to 0 or other than 0 value) that are used in a function,which is call by my main function."


    Regards,
    Pandurang S.

  • Hi,

    In my code when i have define the static variable in the internal memory with keyword idata, then the varable is initilise to proper value but when it come to xdata during 2nd trial(for testing i again define it to extermnal memory) it is not initilise to the value other than 0.

    Regards,
    Pandurang S.

  • then the varable is initilise to proper value but when it come to xdata during 2nd trial(for testing i again define it to extermnal memory) it is not initilise to the value other than 0.

    This is getting ridiculous

    HOW OFTENM DO I HAVE TO REPEAT?

    If the external memory is not configured at the beginning of (a private copy of)startup.a51 variables in external memory will NOT be initialized.

    Erik

  • "This is getting ridiculous

    HOW OFTENM DO I HAVE TO REPEAT?

    If the external memory is not configured at the beginning of (a private copy of)startup.a51 variables in external memory will NOT be initialized."

    But we have already established that the derivative in question defaults to using internal XDATA, so that does not appear to be the problem.

  • If the external memory is configured before startup clear and ljmp ?C_START it will not be initialized.

    My experience is that if it is configured before startup clear and ljmp ?C_START it will be initialized. Also that would be the way the Keil software should work.

    Thus I see no other place that would affect the issue.

    Who knows (I refer to f12x for my knowledge) what SFR if the f020 could have a hidden effect re memory access.

    Erik

  • "If the external memory is configured before startup clear and ljmp ?C_START it will not be initialized."

    "My experience is that if it is configured before startup clear and ljmp ?C_START it will be initialized."

    Those statements appear to be in complete contradiction. What are you trying to say?