16-bit I/O Access (Timer1 and ADC Special Considerations)
Reading or writing 16-bit values directly from AVR Studio can cause some problems. To read, for example the counter value from timer 1, a 16-bit value, one of the bytes must be stored in a temporary register. This temporary register will be corrupted if the 16-bit value is read when the program execution is stopped.

Tip!
Using the following macros (for AVR Assembler only) will solve the 16-bit access problem when using symbolic debugging. If using the macros both inside and outside an interrupt code, the cli and sei instruction pair must be included (atomic operation).

MACROS:

.macro outw
  (cli)
   out    @2, @0
   out    @2-1, @1
  (sei)
.endmacro

.macro inw
  (cli)
   in     @2, @0-1
   in     @1, @0
  (sei)
.endmacro
USAGE:
   inw    r17, r16, TCNT1H      ; Reads the counter value
   outw   TCNT1H, r17, r16      ; Writes the counter value
When using symbolic debugging in C, the entire C line is executed for each set. Therefore the 16-bit read or write problem will not occur in this situation in the real chip.

See Also