
97
ATmega8515(L)
2512A–AVR–04/02
Accessing 16-bit
Registers
TheTCNT1,OCR1A/B, andICR1 are16-bit registers that can beaccessedbythe AVR
CPU via the8-bit data bus.The16-bit register must be byte accessed using two read or
write operations. Each 16-bit timerhas a single8-bit registerfor temporary storing of the
highbyte of the16-bit access.The same temporary register issharedbetween all 16-bit
registers within each 16-bit timer.Accessing the lowbyte triggers the16-bit read orwrite
operation. When the lowbyte of a16-bit register iswritten by the CPU, the highbyte
stored in thetemporary register, and the lowbyte written are bothcopied into the16-bit
register in the same clock cycle. When the lowbyte of a16-bit register isreadbythe
CPU, the highbyte of the16-bit register iscopied into thetemporary register in the
same clock cycleas the lowbyte isread.
Not all 16-bit accesses uses thetemporary registerfor the highbyte. Reading the
OCR1A/B 16-bit registers does not involve using thetemporary register.
To doa16-bit write,
thehighbytemustbewrittenbeforethelowbyte
. For a16-bit read,
the low byte must be read before the high byte
.
The following codeexamplesshowhow to access the16-bit timerregisters assuming
that no interrupts updates thetemporary register.The same principle can beused
directly for accessing the OCR1A/B andICR1 Registers. Note that when using “C”, the
compilerhandles the16-bit access.
Note: 1. Theexample codeassumes that the part specificheaderfileis included.
Theassembly codeexample returns theTCNT1 value in the r17:r16registerpair.
Itis important to noticethat accessing 16-bit registers areatomic operations. If an inter-
ruptoccurs between thetwoinstructions accessing the16-bit register, and theinterrupt
codeupdates thetemporary registerbyaccessing the same or any other of the16-bit
timerregisters, then the resultof theaccess outsidetheinterrupt will be corrupted.
Therefore,when both themaincodeand theinterrupt codeupdate thetemporary regis-
ter, themaincodemust disabletheinterruptsduring the16-bit access.
Assembly Code Examples
(1)
...
;
Set TCNT
1
to 0x01FF
ldi r17,0x01
ldi r16,0xFF
out TCNT
1H,r17
out TCNT
1L,r16
; Read TCNT
1 into r17:r16
in r16,TCNT
1L
in r17,TCNT
1H
...
CCode Examples
(1)
unsigned int i;
...
/*
Set TCNT
1
to 0x01FF
*/
TCNT
1 = 0x1FF;
/*
Read TCNT
1
into i
*/
i = TCNT
1;
...
Comentários a estes Manuais