Datasheet

110
8197C–AVR–05/11
ATtiny261A/461A/861A
The following code examples show how to do an atomic write of the TCNT1 register contents.
Writing 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 requires that the r17:r16 register pair contains the value to be writ-
ten to TCNT1.
Assembly Code Example
TIM1_WriteTCNT1:
; Save global interrupt flag
in r18,SREG
; Disable interrupts
cli
; Set TCNT1 to r17:r16
out TC1H,r17
out TCNT1,r16
; Restore global interrupt flag
out SREG,r18
ret
C Code Example
void TIM1_WriteTCNT1( unsigned int i )
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Set TCNT1 to i */
TC1H = (i >> 8);
TCNT1 = (unsigned char)i;
/* Restore global interrupt flag */
SREG = sreg;
}