ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 2206
Скачиваний: 0
CodeVisionAVR
There are the following predefined macros:
__CODEVISIONAVR__ the version and revision of the compiler represented as an integer, example for V1.24.0 this will be 1240
__LINE__ the current line number of the compiled file __FILE__ the current compiled file
__TIME__ the current time in hh:mm:ss format __DATE__ the current date in mmm dd yyyy format __BUILD__ the build number
_CHIP_ATXXXXX_ where ATXXXXX is the chip type, in uppercase letters, specified in the
Project|Configure|C Compiler|Code Generation|Chip option
_MCU_CLOCK_FREQUENCY_ the AVR clock frequency specified in the Project|Configure|C Compiler|Code Generation|Clock option, expressed as an integer in Hz
_MODEL_TINY_ if the program is compiled using the TINY memory model _MODEL_SMALL_ if the program is compiled using the SMALL memory model _MODEL_MEDIUM_ if the program is compiled using the MEDIUM memory model _MODEL_LARGE_ if the program is compiled using the LARGE memory model _OPTIMIZE_SIZE_ if the program is compiled with optimization for size _OPTIMIZE_SPEED_ if the program is compiled with optimization for speed
_WARNINGS_ON_ if the warnings are enabled by the Project|Configure|C
Compiler|Messages|Enable Warnings option
_WARNINGS_OFF_ if the warnings are disabled by the Project|Configure|C
Compiler|Messages|Enable Warnings option
_ENHANCED_CORE_ if the program is compiled using the enhanced core instructions available in the new ATmega chips
_HEAP_START_ the heap starting address
_HEAP_SIZE_ the heap size specified in the Project|Configure|C Compiler|Code Generation|Heap size option
_UNSIGNED_CHAR_ if the Project|Configure|C Compiler|Code Generation|char is unsigned compiler option is enabled or #pragma uchar+ is used
_8BIT_ENUMS_ if the Project|Configure|C Compiler|Code Generation|8 bit enums compiler option is enabled or #pragma 8bit_enums+ is used.
The #line directive can be used to modify the predefined __LINE__ and __FILE__ macros. The syntax is:
#line integer_constant ["file_name"]
Example:
/* This will set __LINE__ to 50 and __FILE__ to "file2.c" */
#line 50 "file2.c"
/* This will set __LINE__ to 100 */ #line 100
The #error directive can be used to stop compilation and display an error message. The syntax is:
#error error_message
Example:
#error This is an error!
© 1998-2007 HP InfoTech S.R.L. |
Page 67 |
CodeVisionAVR
The #warning directive can be used to display a warning message.
The syntax is:
#warning warning_message
Example:
#warning This is a warning!
The #pragma directive allows compiler specific directives.
You can use the #pragma warn directive to enable or disable compiler warnings. Example:
/* Warnings are disabled */ #pragma warn-
/* Write some code here */
/* Warnings are enabled */ #pragma warn+
The compiler’s code optimizer can be turned on or off using the #pragma opt directive. This directive must be placed at the start of the source file.
The default is optimization turned on. Example:
/* Turn optimization off, for testing purposes */ #pragma opt-
or
/* Turn optimization on */ #pragma opt+
If the code optimization is enabled, you can optimize some portions or all the program for size or speed using the #pragma optsize directive.
The default state is determined by the Project|Configure|C Compiler|Code Generation|Optimization menu setting. Example:
/* The program will be optimized for minimum size */ #pragma optsize+
/* Place your program functions here */
/* Now the program will be optimized for maximum execution speed */ #pragma optsize-
/* Place your program functions here */
© 1998-2007 HP InfoTech S.R.L. |
Page 68 |
CodeVisionAVR
The automatic saving and restoring of registers affected by the interrupt handler, can be turned on or off using the #pragma savereg directive.
Example:
/* Turn registers saving off */ #pragma savereg-
/* interrupt handler */ interrupt [1] void my_irq(void) {
/* now save only the registers that are affected by the routines in the interrupt handler, for example R30, R31 and SREG */
#asm
push r30 push r31
in r30,SREG push r30
#endasm
/* place the C code here */ /* .... */
/* now restore SREG, R31 and R30 */ #asm
pop r30
out SREG,r30 pop r31
pop r30 #endasm
}
/* re-enable register saving for the other interrupts */ #pragma savereg+
The default state is automatic saving of registers during interrupts.
The #pragma savereg directive is maintained only for compatibility with versions of the compiler prior to V1.24.1. This directive is not recommended for new projects.
The automatic allocation of global variables to registers can be turned on or off using the #pragma regalloc directive.
The default state is determined by the Project|Configure|C Compiler|Code Generation|Automatic Register Allocation check box.
Example:
/* the following global variable will be automatically allocated to a register */
#pragma regalloc+ unsigned char alfa;
/* the following global variable will not be automatically allocated to a register and will be placed in normal SRAM */ #pragma regalloc-
unsigned char beta;
© 1998-2007 HP InfoTech S.R.L. |
Page 69 |
CodeVisionAVR
The ANSI char to int operands promotion can be turned on or off using the #pragma promotechar directive.
Example:
/* turn on the ANSI char to int promotion */ #pragma promotechar+
/* turn off the ANSI char to int promotion */ #pragma promotechar-
This option can also be specified in the Project|Configure|C Compiler|Code Generation|Promote char to int menu.
Treating char by default as an unsigned 8 bit can be turned on or off using the #pragma uchar directive.
Example:
/* char will be unsigned by default */ #pragma uchar+
/* char will be signed by default */ #pragma uchar-
This option can also be specified in the Project|Configure|C Compiler|Code Generation|char is unsigned menu.
The #pragma library directive is used for specifying the necessity to compile/link a specific library file. Example:
#pragma library mylib.lib
The #pragma glbdef+ directive is used for compatibility with projects, created with versions of CodeVisionAVR prior to V1.0.2.2, where the Project|Configure|C Compiler|Global #define option was enabled.
It signals the compiler that macros are globally visible in all the program modules of a project. This directive must be placed in beginning of the first source file of the project.
By default this directive is not active, so macros are visible only in the program module where they are defined.
© 1998-2007 HP InfoTech S.R.L. |
Page 70 |
CodeVisionAVR
3.2 Comments
The character string "/*" marks the beginning of a comment. The end of the comment is marked with "*/".
Example:
/* This is a comment */ /* This is a
multiple line comment */
One-line comments may be also defined by using the string "//". Example:
// This is also a comment
Nested comments are not allowed.
© 1998-2007 HP InfoTech S.R.L. |
Page 71 |
CodeVisionAVR
3.3 Reserved Keywords
Following is a list of keywords reserved by the compiler. These can not be used as identifier names.
break bit case char const continue default defined do double eeprom else enum extern flash float for funcused goto
if inline int interrupt long register return short signed sizeof sfrb sfrw static struct switch typedef union unsigned void volatile while
© 1998-2007 HP InfoTech S.R.L. |
Page 72 |
CodeVisionAVR
3.4 Identifiers
An identifier is the name you give to a variable, function, label or other object.
An identifier can contain letters (A...Z, a...z) and digits (0...9), as well as the underscore character (_). However an identifier can only start with a letter or an underscore.
Case is significant; i.e. variable1 is not the same as Variable1. Identifiers can have up to 32 characters.
3.5 Data Types
The following table lists all the data types supported by the CodeVisionAVR C compiler, their range of possible values and their size:
Type |
Size (Bits) |
Range |
bit |
1 |
0 , 1 |
char |
8 |
-128 to 127 |
unsigned char |
8 |
0 to 255 |
signed char |
8 |
-128 to 127 |
int |
16 |
-32768 to 32767 |
short int |
16 |
-32768 to 32767 |
unsigned int |
16 |
0 to 65535 |
signed int |
16 |
-32768 to 32767 |
long int |
32 |
-2147483648 to 2147483647 |
unsigned long int |
32 |
0 to 4294967295 |
signed long int |
32 |
-2147483648 to 2147483647 |
float |
32 |
±1.175e-38 to ±3.402e38 |
double |
32 |
±1.175e-38 to ±3.402e38 |
The bit data type is supported only for global variables.
If the Project|Configure|C Compiler|Code Generation|char is unsigned option is checked or
#pragma uchar+ is used, then char has by default the range 0..255.
© 1998-2007 HP InfoTech S.R.L. |
Page 73 |
CodeVisionAVR
3.6 Constants
Integer or long integer constants may be written in decimal form (e.g. 1234), in binary form with 0b prefix (e.g. 0b101001), in hexadecimal form with 0x prefix (e.g. 0xff) or in octal form with 0-prefix (e.g. 0777).
Unsigned integer constants may have the suffix U (e.g. 10000U). Long integer constants may have the suffix L (e.g. 99L).
Unsigned long integer constants may have the suffix UL (e.g. 99UL). Floating point constants may have the suffix F (e.g. 1.234F).
Character constants must be enclosed in single quotation marks. E.g. 'a'.
String constants must be enclosed in double quotation marks. E.g. "Hello world".
If you place a string between quotes as a function parameter, this string will automatically be considered as constant and will be placed in FLASH memory.
Example:
/ * this function displays a string located in SRAM */ void display_ram(char *s) {
/* ....... */
}
/ * this function displays a string located in FLASH */ void display_flash(char flash *s) {
/* ....... */
}
void main(void) {
/* this will not work !!! */
/* because the function addresses the string as */
/* it is located in SRAM, but the string "Hello world" */ /* is constant and is placed in FLASH */ display_ram("Hello world");
/* this will work !!! */
/* the function addresses the string as it is located in FLASH */ display_flash("Hello world");
}
Constant can be grouped in arrays, which can have up to 8 dimensions.
Constants are stored in FLASH memory, to specify this you must use the flash or const keywords. Constant expressions are automatically evaluated during compilation.
Example:
flash int |
integer_constant=1234+5; |
flash char |
char_constant=’a’; |
flash long |
long_int_constant1=99L; |
flash long |
long_int_constant2=0x10000000; |
flash int |
integer_array1[]={1,2,3}; |
/* The first two elements will be 1 and 2, the rest will be 0 */
flash int integer_array2[10]={1,2};
flash int multidim_array[2][3]={{1,2,3},{4,5,6}};
flash char string_constant1[]=”This is a string constant”; const char string_constant2[]=”This is also a string constant”;
Constants can’t be declared inside functions.
© 1998-2007 HP InfoTech S.R.L. Page 74