Datasheet
109
8197C–AVR–05/11
ATtiny261A/461A/861A
The following code examples show how to do an atomic read of the TCNT1 register contents.
Reading any of the OCR1A/B/C/D registers can be done by using the same principle.
Note: See “Code Examples” on page 6.
The assembly code example returns the TCNT1 value in the r17:r16 register pair.
Assembly Code Example
TIM1_ReadTCNT1:
; Save global interrupt flag
in r18,SREG
; Disable interrupts
cli
; Read TCNT1 into r17:r16
in r16,TCNT1
in r17,TC1H
; Restore global interrupt flag
out SREG,r18
ret
C Code Example
unsigned int TIM1_ReadTCNT1( void )
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Read TCNT1 into i */
i = TCNT1;
i |= ((unsigned int)TC1H << 8);
/* Restore global interrupt flag
SREG = sreg;
return i;
}