1 reusing the temporary high byte register, Atmega4hvd/8hvd – Rainbow Electronics ATmega8HVD User Manual
Page 85

85
8052B–AVR–09/08
ATmega4HVD/8HVD
The following code examples show how to do an atomic write of the TCNTnH/L register con-
tents. Writing any of the OCRnA/B registers can be done by using the same principle.
Note:
See
”About Code Examples” on page 5
.
The assembly code example requires that the r17:r16 register pair contains the value to be
written to TCNTnH/L.
16.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 writ-
ten, 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;
}