Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CodeVision AVR 1.25.7, user manual.pdf
Скачиваний:
214
Добавлен:
12.08.2013
Размер:
1.22 Mб
Скачать

CodeVisionAVR

3.7 Variables

Program variables can be global (accessible to all the functions in the program) or local (accessible only inside the function they are declared).

If not specifically initialized, the global variables are automatically set to 0 at program startup. The local variables are not automatically initialized on function call.

The syntax is:

[<storage modifier>] <type definition> <identifier>;

Example:

/* Global variables declaration */ char a;

int b;

/* and initialization */ long c=1111111;

void main(void) {

/* Local variables declaration */ char d;

int e;

/* and initialization */ long f=22222222;

}

Variables can be grouped in arrays, which can have up to 8 dimensions. The first element of the array has always the index 0.

If not specifically initialized, the elements of global variable arrays are automatically set to 0 at program startup.

Example:

/* All the elements of the array will be 0 */ int global_array1[32];

/* Array is automatically initialized */ int global_array2[]={1,2,3};

int global_array3[4]={1,2,3,4};

char global_array4[]=”This is a string”;

/* Only the first 3 elements of the array are initialized, the rest 29 will be 0 */

int global_array5[32]={1,2,3};

/* Multidimensional array */

int multidim_array[2][3]={{1,2,3},{4,5,6}};

void main(void) {

/* local array declaration */ int local_array1[10];

/* local array declaration and initialization */ int local_array2[3]={11,22,33};

char local_array3[7]="Hello";

}

© 1998-2007 HP InfoTech S.R.L.

Page 75

CodeVisionAVR

Local variables that must conserve their values during different calls to a function must be declared as static. Example:

int alfa(void) {

/* declare and initialize the static variable */ static int n=1;

return n++;

}

void main(void) { int i;

/* the function will return the value 1 */ i=alfa();

/* the function will return the value 2 */ i=alfa();

}

If not specifically initialized, static variables are automatically set to 0 at program startup.

Variables that are declared in other files must be preceded by the extern keyword. Example:

extern int xyz;

/* now include the file which contains the variable xyz definition */

#include <file_xyz.h>

To instruct the compiler to allocate a variable to registers, the register modifier must be used. Example:

register int abc;

The compiler may automatically allocate a variable to registers, even if this modifier is not used.

The volatile modifier must be used in order to prevent a variable to be allocated to registers and to warn the compiler that it may be subject to outside change during evaluation.

Example:

volatile int abc;

All the global variables, not allocated to registers, are stored in the Global Variables area of SRAM. All the local variables, not allocated to registers, are stored in dynamically allocated space in the Data Stack area of SRAM.

© 1998-2007 HP InfoTech S.R.L.

Page 76

CodeVisionAVR

3.7.1 Specifying the SRAM Storage Address for Global Variables

Global variables can be stored at specific SRAM locations at design-time using the @ operator. Example:

/* the integer variable "a" is stored in SRAM at address 80h */

int a @0x80;

/* the structure "alfa" is stored in SRAM at address 90h */

struct x {

int a; char c;

}alfa @0x90;

3.7.2Bit Variables

The global bit variables located in the GPIOR register(s) and R2 to R14 memory space. These variables are declared using the bit keyword.

The syntax is:

bit <identifier>;

Example:

/* declaration and initialization for an ATtiny2313 chip which has GPIOR0, GPIOR1 and GPIOR2 registers */

bit alfa=1; /* bit0 of GPIOR0 */ bit beta; /* bit1 of GPIOR0 */

void main(void)

{

if (alfa) beta=!beta;

/* ........

*/

}

 

Memory allocation for the global bit variables is done, in the order of declaration, starting with bit 0 of GPIOR0, then bit 1 of GPIOR0 and so on, in ascending order.

After all the GPIOR registers are allocated, further bit variables are allocated in R2 up to R14. If the chip does not have GPIOR registers, the allocation begins directly from register R2.

The size of the global bit variables allocated to the program can be specified in the

Project|Configure|C Compiler|Code Generation|Bit Variables Size list box.

This size should be as low as possible, in order to free registers for allocation to other global variables. If not specifically initialized, the global bit variables are automatically set to 0 at program startup.

© 1998-2007 HP InfoTech S.R.L.

Page 77

CodeVisionAVR

The compiler allows also to declare up to 8 local bit variables which will be allocated in register R15. Example:

void main(void)

{

bit alfa; /* bit 0 of R15 */ bit beta; /* bit 1 of R15 */ /* ........ */

}

In expression evaluation bit variables are automatically promoted to unsigned char.

© 1998-2007 HP InfoTech S.R.L.

Page 78