Atmel CAVR-4 Manual De Usuario

Descargar
Página de 323
CAVR-4
Part1. Using the compiler
Efficient coding for embedded applications
133
Protecting the eeprom write mechanism
A typical example of when it can be necessary to use the 
_ _monitor
 keyword is when 
protecting the eeprom write mechanism, which can be used from two threads (for 
example, main code and interrupts). Servicing an interrupt during an EEPROM write 
sequence can in many cases corrupt the written data.
ACCESSING SPECIAL FUNCTION REGISTERS
Specific header files for a number of AVR derivatives are included in the AVR IAR 
C/C++ Compiler delivery. The header files are named 
ioderivative.h
 and define the 
processor-specific special function registers (SFRs). 
In IAR Embedded Workbench, enable the bit definitions by selecting the option 
General Options>System>Enable bit definitions in I/O include files
SFRs with bitfields are declared in the header file. The following example is from 
iom128.h
:
_ _io union
{
  unsigned char PORTE;    /* The sfrb as 1 byte */
  struct
  {  
    unsigned char PORTE_Bit0:1,
                  PORTE_Bit1:1,
                  PORTE_Bit2:1,
                  PORTE_Bit3:1,
                  PORTE_Bit4:1,
                  PORTE_Bit5:1,
                  PORTE_Bit6:1,
                  PORTE_Bit7:1;
  };
} @ 0x1F;
By including the appropriate include file to your code, it is possible to access either the 
whole register or any individual bit (or bitfields) from C code as follows:
/* whole register access */
PORTE = 0x12;
/* Bitfield accesses */
PORTE.PORTE_Bit0  = 1;
You can also use the header files as templates when you create new header files for other 
AVR derivatives. For details about the @ operator, see Located data, page 46.