Header File Meanings
avr_macros.h includes simplified writing for reading and writing 16-bit registers, along with several bit manipulation functions.
comp_a90.h provides brief writing for a large number of intrinsic functions.
ina90.h includes the “inavr.h” and “comp_A90.h” files.
intrinsics.h provides intrinsic functions that offer the simplest operations for handling the processor’s low-level features: sleep, watchdog, FLASH functions.
iomacro.H is a sample file for I/O register definitions.
iom8.h includes definitions for I/O and other registers.
Bit Manipulation
In C language, bit manipulation is generally done as follows:
PORTB |= (1 << 2); // Set the 2nd bit of PORTB to 1
PORTB &= ~(1 << 2); // Set the 2nd bit of PORTB to 0
PORTB ^= (1 << 2); // Toggle the 2nd bit of PORTB
while(PORTB & (1 << 2)); // Check if it is 1
while(!(PORTB & (1 << 2))); // Check if it is 0
The IAR compiler has stronger support for bit operations. In addition to the above methods, there are simpler operations:
PORTB_Bit2 = 1; // Set the 2nd bit of PORTB to 1
PORTB_Bit2 = 0; // Set the 2nd bit of PORTB to 0
PORTB_Bit2 = ~PORTB_Bit2; // Toggle the 2nd bit of PORTB
while(PORTB_Bit2); or while(PORTB_Bit2 == 1); // Check if it is 1
while(PORTB_Bit2 == 0); // Check if it is 0
PORTC_Bit4 = PORTB_Bit2; // Transfer the 2nd bit of PORTB to the 4th bit of PORTC
Bit Variable Definition:
Since IAR uses an extended language, its support for bit fields is minimized to char type, which allows us to easily define bit variables.
Using a structure to define bit 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 you can use the following bit variables:
t.bit0 = 1;
t.bit0 = ~t.bit0;
However, the bit variables created using the above structure can only access the bits of t and cannot directly access variable t. This is different from standard IAR bit operations, and a union can be used to define it.
#include
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; // Access the bit of variable t
t_bit0 = ~t_bit0;
PORTB = t; // Directly access variable t
}
Delay Function
__delay_cycles(unsigned long);
If the processor frequency is 1M, to delay 100us, use:
__delay_cycles(100);
To delay for any time:
#define CPU_F 8000000
#define delay_us(x) __delay_cycles(CPU_F * x / 1000000)
#define delay_ms(x) __delay_cycles(CPU_F * x / 1000)
Interrupt Function:
In the IAR compiler, use the keyword __interrupt to define an interrupt function and 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 can also be written as #pragma vector=TIMER0_OVF_vect for better clarity. Each interrupt’s entry address is described in the header file.
Interrupt Instructions
__disable_interrupt(); // Can also use _CLI(); or SREG_Bit7 = 0;
__enable_interrupt(); // Can also use _SEI(); or SREG_Bit7 = 1;
MCU Control Instructions
__no_operation(); // No operation instruction
_NOP();
__sleep(); // Sleep instruction
_SLEEP();
__watchdog_reset(); // Watchdog reset
_WDR();