IAR for AVR Study Notes

Source: Internet
Author: User
IAR for AVR Study Notes (1) -- data type (compiler supports ISO/ansi c basic data types and some additional data types)

1.1. The bool data type of integer data is supported by default in C ++. If you include stdbool. h In the header file of the C code, the bool data type can also be used in the C language. You can also use boolean values false and true. 1. 2. Floating Point Data Type:

. Pointer type: pointers include data pointers and function pointers.
1. Data Pointer:
The Data Pointer size is 8 bits, 16 bits, and 24 bits. It is defined as "*" after the integer data type.
Example: char * P;
There are no 24-bit integer data. For specific definitions of pointers, see the extension keyword section below.
2. function pointer: the size of the function pointer is 16 bits and 24 bits.
Pointer definition: add the "*" symbol after the function type

 

IAR for AVR Study Notes (2) -- extended keywords

It can be used to store data and functions. With it, we can define that variables are stored in the EEPROM and flash space. Define interrupt functions, pointers, and so on. There are many IAR keywords. Here we only list commonly used ones.

2. 1. Extended keywords: used to control data and pointers.
_ EEPROM is used in the EEPROM storage space to control data storage, control pointer type and storage
_ Tinyflash, _ flash, _ farflash, and _ hugeflash are used in flash storage space to control data storage and control pointer types and storage:
_ Ext_io, _ IO is used for I/O storage space, controlling data storage, controlling pointer type and storage
_ Regvar: place a variable in the working register.

2. Function Extension keywords :.
_ Nearfunc _ farfunc is used to control data storage. This set of keywords must be specified in the function declaration and definition:
_ Interrupt. The keyword controls the function type. This set of keywords must be specified in the function declaration and definition.
_ Root. The keyword only controls the defined functions:

2. 3. Other Special keywords:
@ Is used to locate the absolute address of a variable. You can also use the # pragma location command
# Pragma vector provides the entry address of the interrupt function.
_ Root ensure that unused functions or variables can also be included in the target code
_ No_init disables variable initialization during system startup.
ASM, _ ASM insert assembly code

 

IAR for AVR Study Notes (3)-bit operations

. The following operations are usually performed in the C language:
Portb | = (1 <2); // set the 2nd bits of portb to 1
Portb & = ~ (1 <2); // set the 2nd bits of portb to 0
Portb ^ | = (1 <2); // returns the 2nd-bit anti-portb.
While (portb & (1 <2); // Judge 1
While (! (Portb & (1 <2); // 0

3.2.iar compiler supports more powerful ing. In addition to the above method, there are also the following simpler operations:
Portb _ bit2 = 1; // set the 2nd bits of portb to 1
Portb _ bit2 = 0; // set the 2nd bits of portb to 0
Portb _ bit2 = ~ Portb _ bit2; // returns the 2nd-bit reverse portb.
While (portb _ bit2); or while (portb_bit2 = 1); // Judge 1
While (portb _ bit2 = 0); // judge 0
Portc_bit4 = portb_bit2; // transfers the 2nd bits of portb to the 4th bits of portc.

3. Bit variable definition:
Since IAR uses an extended language, its support for ing fields is changed to the char type at least, so we can easily define bit variables.
Use struct to define bitwise variables:
Struct
{
Unsigned char bit0: 1;
Unsigned char bit1: 1;
Unsigned char bit2: 1;
Unsigned char bit3: 1;
Unsigned char bit4: 1;
Unsigned char bit5: 1;
Unsigned char bit6: 1;
Unsigned char bit7: 1;
} T;
Then we can use the lower-bit variable.
T. bit0 = 1;
T. bit0 = ~ T. bit0;
However, the bitwise variables made using the above struct can only access the T bit and cannot directly access the variable t, which is different from the standard IAR bit operations. Better results can be defined using consortium.
# I nclude <iom8.h>
Union
{
Unsigned char T;
Struct
{Unsigned char t_bit0: 1,
T_bit1: 1,
T_bit2: 1,
T_bit3: 1,
T_bit4: 1,
T_bit5: 1,
T_bit6: 1,
T_bit7: 1;
};
};
Void main (void)
{
T_bit0 = 1; // the bit of the access variable t
T_bit0 = ~ T_bit0;
Portb = T; // directly access the variable t.
}
Bit variables can also be directly defined in the work register.

3.4 bool data types are supported by default in C ++.
If you include stdbool. h In the header file of the C code, the bool data type can also be used in the C language. You can also use boolean values false and true. However, it takes 8 bytes and 1 byte.
# I nclude <iom8.h>
# I nclude <stdbool. h>
Bool y = 0; // defines the bit variable
Void main (void)
{
Y =! Y; // obtains the inverse variable.
Portb_bit3 = y; // pass the bit variable
}

IAR for AVR Study Notes (4) -- Flash operations

Operation Methods of common flash types

4.1.flash region data storage.
Use the keyword _ flash Control for storage, and the _ flash keyword for the same effect before and after the data type.
_ Flash unsigned char a; // defines a variable stored in the Flash space.
Unsigned char _ flash a; // The effect is the same as above
_ Flash unsigned char P []; // defines an array stored in the Flash Space
The read operations on the variables in the Flash space are the same as those in the SRAM data space. The Compiler automatically uses
LPM and elpm commands.
Example:
# I nclude <iom8.h>
_ Flash unsigned char P [];
_ Flash unsigned char;
Void main (void)
{Portb = P [1]; // read The Flash array variable operation
Portb = A; // read The Flash variable operation
}
In a normal program, the flash space is read-only, so variables without assignment are meaningless. To define constants in the Flash space, you only need to assign the initial values to the variables. Since the constant is randomly allocated in the Flash space address, the constant value can be read only when the read variable is used.
10
IAR-AVR-C compiler Overview
_ Flash unsigned char a = 9; // defines a constant stored in the eeprom space.
_ Flash unsigned char P [] = {1, 2, 3, 4, 5, 6, 7, 8 };
// Define a group of constants stored in the Flash space.
Example:
# I nclude <iom8.h>
_ Flash unsigned char P [] = {1, 2, 3, 4, 5, 6, 7, 8 };
_ Flash unsigned char a = 9;
Void main (void)
{
Portb = A; // read The Flash Space Value 9
Portc = P [0]; // read The Flash Space Value
}

4.1.2absolute address location of the flash space:
_ Flash unsigned char a @ 0x8; // define the variable to be stored in the Flash space 0x08 Unit _ flash unsigned char P [] @ 0x22 // define the array to be stored in the Flash space. The starting address is 0x22 unit.
_ Flash unsigned char a @ 0x08 = 9; // defines the unit 0x08 of a constant stored in the Flash space.
_ Flash unsigned char P [] @ 0x22 = {1, 2, 3, 4, 5, 6, 7, 8 };
// Define the starting address of a group constant stored in the eeprom space as 0x22
Since constants have been allocated in the Flash space address, you can use variables and addresses to read the flash space value.

4. 2. pointer operations related to _ flash. The _ flash keyword controls the storage and type of pointers.
4.2.1 pointer to flash space flash pointer (control type attribute)
Unsigned char _ flash * P; // defines the pointer to the Flash space address, which is 8 bits.
Unsigned int _ flash * P; // defines a pointer to the Flash space address, 16 bits.
Unsigned int _ farflash * P; // defines the pointer to the Flash space address, 24 bits.
Unsigned int _ hugeflash * P; // defines the pointer to the Flash space address, 24 bits.
Unsigned char _ flash * P; // defines a pointer to the Flash space address, which is stored in SARM. The value of P indicates an address of the flash space. * P indicates the content stored in the address unit of the flash space. For example, if p = 10, the address of the flash space is 10 units, and the content of the flash m space is read by * P.
Example:
# I nclude <iom8.h>
Char _ flash t @ 0x10;
Char _ flash * P;
Void main (void)
{
Portb = * P; // read the value of 10 units in the Flash space.
Portb = * (p + 3); // read the value of the flash space 0x13 unit.
}

4.2.2. pointer Data Pointer stored in Flash Space
Control the storage attributes just like the data stored in the Flash Space
_ Flash unsigned char * P; // defines the pointer to the sarmm space address, which is stored in flash.

. The _ flash definition used to control data and pointer storage must be a global variable, and the control type attribute (as if only a pointer) can be a local variable.
# I nclude <iom8.h>
_ Flash unsigned char P; // controls Storage
Void main (void)
{
Unsigned char _ flash * t; // control attributes
Portb = P;
Portb = * t;
}

4.4. The _ root keyword ensures that unused functions or variables can also be included in the target code.
Define the data stored in the _ flash space. During program compilation, the generated code is automatically embedded into the flash code, if the program is not used, the compiled data (such as your version number and time can be embedded in the Code) must be subject to the keyword _ root.
Example:
# I nclude <iom8.h>
_ Root _ flash unsigned char P @ 0x10 = 0x56;
Void main (void)
{}
If the program does not use the P variable, the Code is also generated during compilation.
: 020000020000fc
: 00016c0189518951895189518951895189518955f
: 100010005695189518951895189518951895189518953a
: 10002000189518951895089500008895fecf0fe94a
: 100030000dbf00e00ebfc0e8d0e003d0f4dff4df76
: 06004000f3cf01e008957a
: 0400000300000000f9
: 00000001ff

4.5.flash macro functions: Detailed descriptions are provided in the header file comp_a90.h intrinsics. h. The Flash space has the read-only performance normally. For the read flash data compiler, the corresponding LPM and elpm commands are automatically compiled, however, there is no corresponding C command for the Self-programming write command SPM of the flash space. Here we will not explain the detailed self-programming method, but will explain the Read and Write Functions of the flash space.
Read the address data of the flash space directly in the program: the file must contain the intrinsics. h header file.
_ Load_program_memory (const unsigned char _ flash *); // 64 K space
// Read data from the specified flash space address. This function is described in the intrinsics. h header file.
The comp_a90.h file has its simplified write _ LPM (ADDR ). Note that the Assembly command LPM Rd, Z in Z is a pointer. Therefore, (const unsigned char _ flash *) is used to forcibly convert the pointer to the address of the flash space. Therefore, the correct syntax of this macro function should be as follows:
_ Load_program_memory (const unsigned char _ flash *) ADDR );
Example:
# I nclude <iom8.h>
# I nclude <intrinsics. h>
Void main (void)
{Portb =__ load_program_memory (const unsigned char _ flash *) 0x12 );
}
This function is not easy to write. It is simplified in the comp_a90.h file:
# DEFINE _ LPM (ADDR) _ load_program_memory (ADDR) is a little more convenient. Change
# DEFINE _ LPM (ADDR) _ load_program_memory (const unsigned char
_ Flash *) ADDR) makes it easier to use data directly.
Example:
# I nclude <iom8.h>
# I nclude <comp_a90.h>
# I nclude <intrinsics. h>
Void main (void)
{
Portb =__ LPM (0x12); // read data from the specified flash space address unit 0x12
}
_ Extended_load_program_memory (const unsigned char _ farflash *);
// 128k Space _ elpm (ADDR); // 128k Space
It is easier to write with reference to the above understanding and modification.

4. Self-programmed functions:
_ Spm_get_lockbits (); // read and shrink location
_ Spm_get_fusebits (); // read the fuse bit
_ Spm_erase (ADDR); // 16-bit page Erasure
_ Spm_filltemp (ADDR, word); // 16-bit page Buffer
_ Spm_pagewrite (ADDR;) // write a 16-bit page
_ Spm_24_erase (ADDR); // 24-bit page Erasure
_ Spm_24_filltemp (ADDR, data); // 24-bit page Buffer
_ Spm_24_pagewrite (ADDR) // 24-bit page write

IAR for AVR Study Notes (5) -- SRAM operations

Specific operations on the SARM Data Type

The SARM space is the most important part of the AVR Microcontroller. All operations must be completed by this Part. There are three storage modes for variables in the SARM space: tiny and small large, which correspond to the three storage properties of _ tiny and _ near _ Far. Once the storage mode is selected, the default attribute of the corresponding data is determined, but you can use the _ tiny, _ near _ Far keyword to change it.
For local variables in the program, the compiler will automatically process them, and we cannot add any storage attributes, but IAR provides powerful external variable definitions.

5. 1. Define variables in the working register
The IAR compiler uses part of the working registers internally, leaving the user with only 12 R4-R15 registers for use by the user. To use the Working registers, the locking option must be enabled in the project options.
Example:
Define two variables using the working register R14, R15.
# I nclude <iom8.h>
_ Regvar _ no_init char G @ 15;
_ Regvar _ no_init char P @ 14;
Void main (void)
{
G ++;
P ++;
}
Open the register R14-R15 to be used in the C/C ++ complier> code in the project option.

The compilation result is as follows to see if the register is directly used as the data application.
// 4 void main (void)
Main:
CFI block cfiblock0 using cficommon0
CFI function main
// 5 {g ++;
Require? Register_r14_is_global_regvar
Require? Register_r15_is_global_regvar
INC R15
// 6 p ++ ;}
INC R14
RET
Note: variables defined in registers cannot have initial values. It is best not to use more than 9 register variables, otherwise it may cause potential danger because no registers are locked during database creation. 5. 2. defines the absolute address of a variable. variables without features are randomly allocated. To assign an address to a variable, you must modify the features. Note that the address must not overlap with the on-chip Register address when defining the address.
5.2.1 to define absolute address variables without storage features, _ no_init or const object features must be added.
_ No_init char t @ 0x65; // defined outside the I/O address
Const char t @ 0x65; // defines the address of the read-only variable
Example:
# I nclude <iom8.h>
_ No_init char U @ 0x65;
Void main (void)
{U ++ ;}
Corresponding assembly:
Void main (void)
\ Main:
{U ++ ;}
\ 00000000 e6e5 LDI R30, 101
\ 00000002 e0f0 LDI R31, 0
\ 00000004 8100 LD R16, Z
\ 00000006 9503 Inc R16
\ 00000008 8300 st Z, R16
\ 0000000a 9508 RET
5.2.2 define the absolute address _ IO ,__ ext_io of variables with keywords with storage features in I/O space
# I nclude <iom8.h>
_ IO char U @ 0x65;
Void main (void)
{U ++ ;}
Corresponding assembly:
Void main (void)
\ Main:
{U ++ ;}
\ 00000000 91000065 lDs R16, 101
\ 00000004 9503 Inc R16
\ 00000006 93000065 STS 101, R16
\ 0000000a 9508 RET
Compared with 5.2.1 and 5.2.2, it is found that the Code defined by 5.2.2 is much smaller.

. The keyword volatile ensures that the variable is read from the original location. In the IAR compiler, except variables defined by _ no_init and _ root, other types of variables include volatile and _ no_init.

IAR for AVR Study Notes (6)-interrupt and related function operations

6. 1. Interrupt function:
In the IAR compiler, use the keyword _ interrupt to define an interrupt function. Use # pragma vector to provide the entry address of the interrupt function
# Pragma vector = 0x12 // timer 0 overflow interrupt entry address
_ Interrupt void time0 (void)
{
;
}
The above entry address is written as # pragma vector = timer0_ovf_vect, which is more intuitive. The entry address for each interruption is described in the header file. The function name time0 can be any name. The interrupt function automatically protects local variables, but does not protect global variables.

. Internal functions can also be called intrinsic functions
A function compiled by the compiler that can directly access the underlying features of the processor. The complete types described in intrinsics. h are further simplified in comp_a90.h.

6.2.1 latency functions, based on Cycles
_ Delay_cycles (unsigned long );
If the processor frequency is 1 m, the latency is us, as follows:
_ Delay_cycles (100 );
You can also modify the function:
# Define cpu_f 1000000
# Define delay_us (unsigned long) _ delay_cycles (unsigned long) * cpu_f)
# Define delay_ms (unsigned long) _ delay_cycles (unsigned long) * cpu_f/1000)

6.2.2 interrupt command
_ Disable_interrupt (); // you can use _ CLI () or sreg_bit7 = 0 to insert CLI commands;
_ Enable_interrupt (); // Insert the SEI command. You can also use _ sei () or sreg_bit7 = 1;
In fact, only bset S and BCLR s commands are set and cleared for the status word. SEI is just another name of bset 7. There are many similar phenomena in the AVR command. For example, the Ori commands are exactly the same as those in the SBR command. The AVR commands that claim more than 130 commands actually do not have that many commands.

6.2.3 read data from a specified address in the Flash Space
_ Extended_load_program_memory (unsigned char _ farflash *);
_ Load_program_memory (unsigned char _ flash *);
The instruction and correct usage are described in detail in 4.5.flash macro functions.

6.2.4 multiplication function
_ Fracdtional_multiply_signed (signed Char, signed Char );
_ Fractional_multiply_signed_with_unsigned (signed Char, unsigned char );
_ Fractional_multiply_unsigned (unsigned char, unsigned char );
// The above is the fixed-point decimal multiplication.
_ Multiply_signed (signed Char, signed Char); // multiplication of signed numbers
_ Multiply_signed_with_unsigned (signed Char, unsigned char );
// Multiplication of signed numbers and unsigned numbers
_ Multiply_unsigned (unsigned char, unsigned char); // unsigned Multiplication

6.2.4 half-byte switch command
_ Swap_nibbles (unsigned char );

6.2.5 MCU control commands
_ No_operation (); // null Operation Command
_ Nop ();
_ Sleep (); // sleep command
_ Sleep ();
_ Watchdog_reset (); // reset the Watchdog
_ WDR ();

 

IAR for AVR Study Notes (7) -- header file meaning avr_macros.h contains the 16-bit read/write register for simplified writing, and several bit operation functions comp_a90.h on a large number of internal functions are briefly written ina90.h contains "inavr. H "" comp_a90.h "file intrinsics. h internal functions provide the simplest underlying features of the operating processor. Sleep, watchdog, flash function. Iomacro. h I/O register definition file sample.

Iom8.h includes I/O and other register Definitions

 

IAR for AVR study notes (8) -- Assembly embedding method embedded assembly language
Online Assembly: Use ASM or _ ASM, and _ ASM is recommended.
# I nclude <iom8.h>
Void main ()
{
ASM ("NOP \ n"
"Clh \ n"
"Or R16, R17 \ n ");
}
However, IAR provides fully accessible underlying functions. We recommend that you do not frequently use the assembly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.