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

CodeVisionAVR

3.7.7 Global Variables Memory Map File

During compilation the C compiler generates a Global Variables Memory Map File, in which are specified the SRAM address location, register allocation and size of the global variables used by the program.

This file has the .map extension and can be viewed using the menu File|Open command or by pressing the Open button on the toolbar.

Structure and union members are listed individually along with their corresponding address and size. This file is useful during program debugging using the AVR Studio debugger.

3.8 Defining Data Types

User defined data types are declared using the typedef reserved keyword.

The syntax is:

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

The symbol name <identifier> is assigned to <type definition>.

Examples:

/* type definitions */ typedef unsigned char byte; typedef eeprom struct {

int a; char b[5];

} eeprom_struct_type;

/* variable declaration */ byte alfa;

eeprom eeprom_struct_type struct1;

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

Page 86

CodeVisionAVR

3.9 Type Conversions

In an expression, if the two operands of a binary operator are of different types, then the compiler will convert one of the operands into the type of the other.

The compiler uses the following rules:

If either of the operands is of type float then the other operand is converted to the same type.

If either of the operands is of type long int or unsigned long int then the other operand is converted to the same type.

Otherwise, if either of the operands is of type int or unsigned int then the other operand is converted to the same type.

Thus char type or unsigned char type gets the lowest priority.

Using casting you can change these rules.

Example:

void main(void) { int a, c;

long b;

/* The long integer variable b will be treated here as an integer */ c=a+(int) b;

}

It is important to note that if the Project|Configure|C Compiler|Code Generation|Promote char to int option isn't checked or the #pragma promotechar+ isn't used, the char, respectively unsigned char, type operands are not automatically promoted to int , respectively unsigned int, as in compilers targeted for 16 or 32 bit CPUs.

This helps writing more size and speed efficient code for an 8 bit CPU like the AVR. To prevent overflow on 8 bit addition or multiplication, casting may be required. The compiler issues warnings in these situations.

Example:

void main(void) { unsigned char a=30; unsigned char b=128; unsigned int c;

/* This will generate an incorrect result, because the multiplication is done on 8 bits producing an 8 bit result, which overflows. Only after the multiplication, the 8 bit result is promoted to unsigned int */

c=a*b;

/* Here casting forces the multiplication to be done on 16 bits, producing an 16 bit result, without overflow */

c=(unsigned int) a*b;

}

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

Page 87

CodeVisionAVR

The compiler behaves differently for the following operators:

+= -= *= /= %= &= |= ^= <<= >>=

For these operators, the result is to be written back onto the left-hand side operand (which must be a variable). So the compiler will always convert the right hand side operand into the type of left-hand side operand.

3.10 Operators

The compiler supports the following operators:

+-

*/

%++

--=

==~

!!=

<>

<= >=

&&&

| ||

^?

<<>>

-=

+=

/=

%=

&=

*=

^=

|=

>>=

<<=

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

Page 88

CodeVisionAVR

3.11 Functions

You may use function prototypes to declare a function.

These declarations include information about the function parameters. Example:

int alfa(char par1, int par2, long par3);

The actual function definition may be written somewhere else as:

int alfa(char par1, int par2, long par3) { /* Write some statements here */

}

The old Kernighan & Ritchie style of writing function definitions is not supported. Function parameters are passed through the Data Stack.

Function values are returned in registers R30, R31, R22 and R23 (from LSB to MSB).

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

Page 89