4.2.10 Special Register Issues
Some of the timer-related 16-bit registers internally use an 8-bit wide temporary register (called TEMP in the device data sheets) to guarantee atomic access to the timer, since two separate byte transfers are required to move timer values. Typically, this register is used by the device when accessing the current timer/counter value register (TCNTn), the input capture register (ICRn), and when writing the output compare registers (OCRnM). Refer to your device data sheet to determine which peripherals make use of the TEMP register.
This temporary register is not accessible to your program, but it is shared by many peripherals, thus your program needs to ensure that the register is not corrupted by interrupt routines that also uses this register.
Within main-line code, interrupts can be disabled during the execution
of the code which utilizes this register. That can be done be encapsulating the code in
calls to the cli() and sei() macros, but if the status
of the global interrupt flag is not known, the following example code can be used.
unsigned int read_timer1(void)
{
unsigned char sreg;
unsigned int val;
sreg = SREG; // save state of interrupt
cli(); // disable interrupts
val = TCNT1; // read timer value register; TEMP used internally
SREG = sreg; // restore state of interrupts
return val;
}
