beautypg.com

Atmega128rfa1 – Rainbow Electronics ATmega128RFA1 User Manual

Page 247

background image

247


8266A-MCU Wireless-12/09

ATmega128RFA1

Assembly Code Examples

(1)

...

; Set TCNTn to 0x01FF

ldi r17,0x01

ldi r16,0xFF

out TCNTnH,r17

out TCNTnL,r16

; Read TCNTn into r17:r16

in r16,TCNTnL

in r17,TCNTnH

...

C Code Examples

(1)

unsigned int i;

...

/* Set TCNTn to 0x01FF */

TCNTn = 0x1FF;

/* Read TCNTn into i */

i = TCNTn;

...

Notes:

1. See

"About Code Examples" on page 7

.

The assembly code example returns the TCNTn value in the r17:r16 register pair.

It is important to notice that accessing 16-bit registers are atomic operations. If an
interrupt occurs between the two instructions accessing the 16-bit register and the
interrupt code updates the temporary register by accessing the same or any other of the
16-bit Timer Registers, then the result of the access outside the interrupt will be
corrupted. Therefore the main code must disable the interrupts during the 16-bit access
when both the main code and the interrupt code update the temporary register.

The following code examples show how to do an atomic read of the TCNTn Register
contents. Reading any of the OCRnA/B/C or ICRn Registers can be done by using the
same principle.

The assembly code example returns the TCNTn value in the r17:r16 register pair.

Assembly Code Examples

(1)

TIM16_ReadTCNTn:

; Save global interrupt flag

in r18,SREG

; Disable interrupts

cli

; Read TCNTn into r17:r16

in r16,TCNTnL

in r17,TCNTnH

; Restore global interrupt flag

out SREG,r18

ret