English
Language : 

ATTINY261A_1 Datasheet, PDF (81/292 Pages) ATMEL Corporation – 8-bit Microcontroller with 2/4/8K Bytes In-System Programmable Flash
ATtiny261A/461A/861A
The following code examples show how to do an atomic write of the TCNT0H/L register con-
tents. Writing any of the OCR0A/B registers can be done by using the same principle.
Assembly Code Example
TIM0_WriteTCNT0:
; Save global interrupt flag
in r18,SREG
; Disable interrupts
cli
; Set TCNT0 to r17:r16
out TCNT0H,r17
out TCNT0L,r16
; Restore global interrupt flag
out SREG,r18
ret
C Code Example
void TIM0_WriteTCNT0( unsigned int i )
{
unsigned char sreg;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Set TCNT0 to i */
TCNT0H = (i >> 8);
TCNT0L = (unsigned char)i;
/* Restore global interrupt flag */
SREG = sreg;
}
Note: See “Code Examples” on page 6.
11.9.1
The assembly code example requires that the r17:r16 register pair contains the value to be writ-
ten to TCNT0H/L.
Reusing the temporary high byte register
If writing to more than one 16-bit register where the high byte is the same for all registers written,
then the high byte only needs to be written once. However, note that the same rule of atomic
operation described previously also applies in this case.
81
8197A–AVR–10/09