Datasheet

Table Of Contents
C Code Example
(1)
unsigned int TIM16_ReadTCNTn( void )
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Read TCNTn into i */
i = TCNTn;
/* Restore global interrupt flag */
SREG = sreg;
return i;
}
Note:  1. See About Code Examples.
The assembly code example returns the TCNTn value in the r17:r16 Register pair.
The following code examples show how to do an atomic write of the TCNTn Register contents. Writing
any of the OCRnA/B or ICRn Registers can be done by using the same principle.
Assembly Code Example
(1)
TIM16_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
(1)
void TIM16_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;
}
Note:  1. See About Code Examples.
The assembly code example requires that the r17:r16 Register pair contains the value to be written to
TCNTn.
Related Links
About Code Examples on page 19
19.3.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.
Atmel ATmega32A [DATASHEET]
Atmel-8155I-ATmega32A_Datasheet_Complete-08/2016
109