This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Creating local variables in CLA

; Under  xxx.c

---------------------------------------------------------------

#pragma DATA_SECTION(var_name, "Cla1Ram1"); // Cla1Ram1 is mapped to RAML2 of datamemory

float var_name;

 

; Under clashared.h

---------------------------

extern float var_name;

; Under CLA.asm

-----------------------

.cdecls   C,LIST,"CLAShared.h"

_Cla1Task1:

...

....

MMOV32  @_var_name, MR1

...

...

 

--------------------------------------------------------------------------------------------------

In the above pieces of code, if i don't use extern while declaring var_name, it does'nt compile...Why is this so? Can't these variables be local to some file in CLA.. Why should it be global? I went through example codes and all of them r using extern while declaring the variables.....

  • There are two things happening here:

    1) The float needs to be extern as it is defined in another file, not this one - making it global (external to this file).

    2) Without extern, you end up defining a C variable in an ASM file, hence you get an error. 

    If you want a variable only local to the ASM file then the way around this is to define it in the assembly file itself.  The assembly equivalent of #pragma DATA_SECTION is .usect.  Thus the code you add to the assembly file is as follows:

    _var_name .usect "Cla1Ram1", 2

    The 2 is due to the fact that a float is 2 words long.  Also note you only need the underscore at the start if you want to reference the variable in C somewhere else (with the use of extern). Hopefully this works.  I have not tried it out on CLA files but it is how you do it in standard assembly so I see no reason for it not to work.

    Tim