Datasheet

89
8024A–AVR–04/08
ATmega8HVA/16HVA
The following code examples show how to do an atomic write of the TCNTnH/L register con-
tents. Writing any of the OCRnB/A registers can be done by using the same principle.
Note: See “About Code Examples” on page 7.
The assembly code example requires that the r17:r16 register pair contains the value to be writ-
ten to TCNTnH/L.
17.9.1 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.
Assembly Code Example
TIMn_WriteTCNTn:
; Save global interrupt flag
in r18,SREG
; Disable interrupts
cli
; Set TCNTn to r17:r16
out TCNTnH,r17
out TCNTnL,r16
; Restore global interrupt flag
out SREG,r18
ret
C Code Example
void TIMn_WriteTCNTn( unsigned int i )
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Set TCNTn to i */
TCNTn = i;
/* Restore global interrupt flag */
SREG = sreg;
}