Материал: DOC1228

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

ATmega161(L)

Table 14. Compare 1 Mode Select(1)

COM1X1

 

COM1X0

Description

 

 

 

 

0

 

0

Timer/Counter1 disconnected from output pin OC1X

 

 

 

 

0

 

1

Toggle the OC1X output line.

 

 

 

 

1

 

0

Clear the OC1X output line (to zero).

 

 

 

 

1

 

1

Set the OC1X output line (to one).

 

 

 

 

Note: 1.

X = A or B

 

In PWM mode, these bits have a different function. Refer to Table 18 for a detailed description.

• Bit 3 FOC1A: Force Output Compare1A

Writing a logical “1” to this bit forces a change in the compare match output pin PD5 according to the values already set in COM1A1 and COM1A0. If the COM1A1 and COM1A0 bits are written in the same cycle as FOC1A, the new settings will not take effect until the next compare match or forced compare match occurs. The Force Output Compare bit can be used to change the output pin without waiting for a compare match in the timer. The automatic action programmed in COM1A1 and COM1A0 happens as if a Compare Match had occurred, but no interrupt is generated and it will not clear the timer even if CTC1 in TCCR1B is set. The FOC1A bit will always be read as zero. The setting of the FOC1A bit has no effect in PWM mode.

• Bit 2 FOC1B: Force Output Compare1B

Writing a logical “1” to this bit forces a change in the compare match output pin PE2 according to the values already set in COM1B1 and COM1B0. If the COM1B1 and COM1B0 bits are written in the same cycle as FOC1B, the new settings will not take effect until the next compare match or forced compare match occurs. The Force Output Compare bit can be used to change the output pin without waiting for a compare match in the timer. The automatic action programmed in COM1B1 and COM1B0 happens as if a Compare Match had occurred, but no interrupt is generated. The FOC1B bit will always be read as zero. The setting of the FOC1B bit has no effect in PWM mode.

• Bits 1..0 PWM11, PWM10: Pulse Width Modulator Select Bits

These bits select PWM operation of Timer/Counter1 as specified in Table 15. This mode is described on page 55.

Table 15. PWM Mode Select

PWM11

PWM10

Description

 

 

 

0

0

PWM operation of Timer/Counter1 is disabled.

 

 

 

0

1

Timer/Counter1 is an 8-bit PWM.

 

 

 

1

0

Timer/Counter1 is a 9-bit PWM.

 

 

 

1

1

Timer/Counter1 is a 10-bit PWM.

 

 

 

51

1228B–09/01

Timer/Counter1 Control

Register B – TCCR1B

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Bit

7

6

 

5

 

4

3

2

1

0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

$2E ($4E)

ICNC1

ICES1

 

 

 

CTC1

CS12

CS11

CS10

TCCR1B

 

 

 

 

 

 

 

 

 

 

 

 

 

Read/Write

R/W

R/W

 

R

R

R/W

R/W

R/W

R/W

 

Initial Value

0

0

 

0

 

0

0

0

0

0

 

• Bit 7 ICNC1: Input Capture1 Noise Canceler (4 CKs)

When the ICNC1 bit is cleared (zero), the input capture trigger noise canceler function is disabled. The input capture is triggered at the first rising/falling edge sampled on the ICP (input capture pin) as specified. When the ICNC1 bit is set (one), four successive samples are measured on the ICP (input capture pin), and all samples must be high/low according to the input capture trigger specification in the ICES1 bit. The actual sampling frequency is XTAL clock frequency.

• Bit 6 ICES1: Input Capture1 Edge Select

While the ICES1 bit is cleared (zero), the Timer/Counter1 contents are transferred to the Input Capture Register (ICR1) on the falling edge of the input capture pin (ICP). While the ICES1 bit is set (one), the Timer/Counter1 contents are transferred to the Input Capture Register (ICR1) on the rising edge of the input capture pin (ICP).

• Bits 5, 4 Res: Reserved Bits

These bits are reserved bits in the ATmega161 and always read as zero.

• Bit 3 CTC1: Clear Timer/Counter1 on Compare Match

When the CTC1 control bit is set (one), the Timer/Counter1 is reset to $0000 in the clock cycle after a compareA match. If the CTC1 control bit is cleared, Timer/Counter1 continues counting and is unaffected by a compare match. When a prescaling of 1 is used and the compareA register is set to C, the timer will count as follows if CTC1 is set:

... | C-1 | C | 0 | 1 | ...

When the prescaler is set to divide by 8, the timer will count like this:

... | C-1, C-1, C-1, C-1, C-1, C-1, C-1, C-1 | C, C, C, C, C, C, C, C | 0, 0, 0, 0, 0, 0, 0, 0 | ...

In PWM mode, this bit has a different function. If the CTC1 bit is cleared in PWM mode, the Timer/Counter1 acts as an up/down counter. If the CTC1 bit is set (one), the Timer/Counter wraps when it reaches the TOP value. Refer to page 55 for a detailed description.

• Bits 2, 1, 0 CS12, CS11, CS10: Clock Select1, Bits 2, 1 and 0

The Clock Select1 bits 2, 1 and 0 define the prescaling source of Timer/Counter1.

Table 16. Clock 1 Prescale Select

CS12

CS11

CS10

Description

 

 

 

 

0

0

0

Stop, the Timer/Counter1 is stopped.

 

 

 

 

0

0

1

CK

 

 

 

 

0

1

0

CK/8

 

 

 

 

0

1

1

CK/64

 

 

 

 

1

0

0

CK/256

 

 

 

 

1

0

1

CK/1024

 

 

 

 

1

1

0

External Pin T1, falling edge

 

 

 

 

1

1

1

External Pin T1, rising edge

 

 

 

 

52 ATmega161(L)

1228B–09/01

Timer/Counter1 Register –

TCNT1H AND TCNT1L

ATmega161(L)

The Stop condition provides a Timer Enable/Disable function. The CK down divided modes are scaled directly from the CK oscillator clock. If the external pin modes are used for Timer/Counter1, transitions on PB1/(T1) will clock the counter even if the pin is configured as an output. This feature can give the user software control of the counting.

Bit

15

14

13

12

11

10

9

8

 

$2D ($4D)

MSB

 

 

 

 

 

 

 

TCNT1H

 

 

 

 

 

 

 

 

 

 

$2C ($4C)

 

 

 

 

 

 

 

LSB

TCNT1L

 

 

 

 

 

 

 

 

 

 

 

7

6

5

4

3

2

1

0

 

Read/Write

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

 

 

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

 

Initial Value

0

0

0

0

0

0

0

0

 

 

0

0

0

0

0

0

0

0

 

This 16-bit register contains the prescaled value of the 16-bit Timer/Counter1. To ensure that both the high and low bytes are read and written simultaneously when the CPU accesses these registers, the access is performed using an 8-bit temporary register (TEMP). This temporary register is also used when accessing OCR1A, OCR1B, and ICR1. If the main program and interrupt routines perform access to registers using TEMP, interrupts must be disabled during access from the main program and interrupt routines.

TCNT1 Timer/Counter1 Write

When the CPU writes to the high byte TCNT1H, the written data is placed in the TEMP register. Next, when the CPU writes the low byte TCNT1L, this byte of data is combined with the byte data in the TEMP register and all 16 bits are written to the TCNT1 Timer/Counter1 register simultaneously. Consequently, the high byte TCNT1H must be accessed first for a full 16-bit register write operation.

TCNT1 Timer/Counter1 Read

Timer/Counter1 Output

Compare Register – OCR1AH

AND OCR1AL

1228B–09/01

When the CPU reads the low byte TCNT1L, the data of the low byte TCNT1L is sent to the CPU and the data of the high byte TCNT1H is placed in the TEMP register. When the CPU reads the data in the high byte TCNT1H, the CPU receives the data in the TEMP register. Consequently, the low byte TCNT1L must be accessed first for a full 16bit register read operation.

The Timer/Counter1 is realized as an up or up/down (in PWM mode) counter with read and write access. If Timer/Counter1 is written to and a clock source is selected, the Timer/Counter1 continues counting in the timer clock cycle after it is preset with the written value.

Bit

15

14

13

12

11

10

9

8

 

$2B ($4B)

MSB

 

 

 

 

 

 

 

OCR1AH

 

 

 

 

 

 

 

 

 

 

$2A ($4A)

 

 

 

 

 

 

 

LSB

OCR1AL

 

 

 

 

 

 

 

 

 

 

 

7

6

5

4

3

2

1

0

 

Read/Write

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

 

 

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

 

Initial Value

0

0

0

0

0

0

0

0

 

 

0

0

0

0

0

0

0

0

 

53

Timer/Counter1 Output

Compare Register – OCR1BH

AND OCR1BL

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Bit

15

14

13

 

 

12

11

10

9

8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

$29 ($49)

MSB

 

 

 

 

 

 

 

 

 

 

 

OCR1BH

 

 

 

 

 

 

 

 

 

 

 

 

 

 

$28 ($48)

 

 

 

 

 

 

 

 

 

 

 

LSB

OCR1BL

 

 

 

 

 

 

 

 

 

 

 

 

 

7

6

5

 

 

4

3

2

1

0

 

Read/Write

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

 

 

R/W

R/W

R/W

R/W

R/W

R/W

R/W

R/W

 

Initial Value

0

0

0

 

 

0

0

0

0

0

 

 

0

0

0

 

 

0

0

0

0

0

 

The output compare registers are 16-bit read/write registers.

The Timer/Counter1 Output Compare Registers contain the data to be continuously compared with Timer/Counter1. Actions on compare matches are specified in the Timer/Counter1 Control and Status registers. A software write to the Timer/Counter Register blocks compare matches in the next Timer/Counter clock cycle. This prevents immediate interrupts when initializing the Timer/Counter.

A compare match will set the compare interrupt flag in the CPU clock cycle following the compare event.

Since the Output Compare Registers (OCR1A and OCR1B) are 16-bit registers, a temporary register (TEMP) is used when OCR1A/B are written to ensure that both bytes are updated simultaneously. When the CPU writes the high byte, OCR1AH or OCR1BH, the data is temporarily stored in the TEMP register. When the CPU writes the low byte, OCR1AL or OCR1BL, the TEMP register is simultaneously written to OCR1AH or OCR1BH. Consequently, the high byte OCR1AH or OCR1BH must be written first for a full 16-bit register write operation.

The TEMP register is also used when accessing TCNT1 and ICR1. If the main program and interrupt routines perform access to registers using TEMP, interrupts must be disabled during access from the main program and interrupt routines.

Timer/Counter1 Input Capture

Register – ICR1H AND ICR1L

Bit

15

14

13

12

11

10

9

8

 

$25 ($45)

MSB

 

 

 

 

 

 

 

ICR1H

 

 

 

 

 

 

 

 

 

 

$24 ($44)

 

 

 

 

 

 

 

LSB

ICR1L

 

 

 

 

 

 

 

 

 

 

 

7

6

5

4

3

2

1

0

 

Read/Write

R

R

R

R

R

R

R

R

 

 

R

R

R

R

R

R

R

R

 

Initial Value

0

0

0

0

0

0

0

0

 

 

0

0

0

0

0

0

0

0

 

The input capture register is a 16-bit read-only register.

When the rising or falling edge (according to the input capture edge setting, ICES1) of the signal at the input capture pin (ICP) is detected, the current value of the Timer/Counter1 Register (TCNT1) is transferred to the Input Capture Register (ICR1). In the same cycle, the input capture flag (ICF1) is set (one).

Since the Input Capture Register (ICR1) is a 16-bit register, a temporary register (TEMP) is used when ICR1 is read to ensure that both bytes are read simultaneously. When the CPU reads the low byte ICR1L, the data is sent to the CPU and the data of the high byte ICR1H is placed in the TEMP register. When the CPU reads the data in the high byte ICR1H, the CPU receives the data in the TEMP register. Consequently, the low byte ICR1L must be accessed first for a full 16-bit register read operation.

54 ATmega161(L)

1228B–09/01

ATmega161(L)

The TEMP register is also used when accessing TCNT1, OCR1A and OCR1B. If the main program and interrupt routines perform access to registers using TEMP, interrupts must be disabled during access from the main program and interrupt routine.

Timer/Counter1 in PWM Mode When the PWM mode is selected, Timer/Counter1 and the Output Compare Register1A (OCR1A) and the Output Compare Register1B (OCR1B) form a dual 8-, 9-, or 10-bit, free-running, glitch-free, and phase-correct PWM with outputs on the PD5(OC1A) and PE2(OC1B) pins. In this mode the Timer/Counter1 acts as an up/down counter, counting up from $0000 to TOP (see Table 17), where it turns and counts down again to zero before the cycle is repeated. When the counter value matches the contents of the 8, 9, or 10 least significant bits (depends of the resolution) of OCR1A or OCR1B, the PD5(OC1A)/PE2(OC1B) pins are set or cleared according to the settings of the COM1A1/COM1A0 or COM1B1/COM1B0 bits in the Timer/Counter1 Control Register (TCCR1A). Refer to Table 18 for details.

Alternatively, the Timer/Counter1 can be configured to a PWM that operates at twice the speed as in the mode described above. Then the Timer/Counter1 and the Output Compare Register1A (OCR1A) and the Output Compare Register1B (OCR1B) form a dual 8-, 9- or 10-bit, free-running and glitch-free PWM with outputs on the PD5(OC1A) and PE2(OC1B) pins.

Table 17. Timer TOP Values and PWM Frequency

CTC1

PWM11

PWM10

PWM Resolution

Timer TOP Value

Frequency

 

 

 

 

 

 

0

0

1

8-bit

$00FF (255)

fTCK1/510

0

1

0

9-bit

$01FF (511)

fTCK1/1022

0

1

1

10-bit

$03FF (1023)

fTCK1/2046

1

0

1

8-bit

$00FF (255)

fTCK1/256

1

1

0

9-bit

$01FF (511)

fTCK1/512

1

1

1

10-bit

$03FF (1023)

fTCK1/1024

As shown in Table 17, the PWM operates at either 8-, 9-, or 10-bit resolution. Note the unused bits in OCR1A, OCR1B and TCNT1 will automatically be written to zero by hardware, i.e., bits 9 to 15 will be set to zero in OCR1A, OCR1B and TCNT1 if the 9-bit PWM resolution is selected. This makes it possible for the user to perform read-modify-write operations in any of the three resolution modes and the unused bits will be treated as don’t care.

Table 18. Compare1 Mode Select in PWM Mode(1)

CTC1

COM1X1

COM1X0

Effect on OCX1

 

 

 

 

0

0

0

Not connected

 

 

 

 

0

0

1

Not connected

 

 

 

 

0

1

0

Cleared on compare match, up-counting. Set on compare

match, down-counting (non-inverted PWM).

 

 

 

 

 

 

 

0

1

1

Cleared on compare match, down-counting. Set on compare

match, up-counting (inverted PWM).

 

 

 

 

 

 

 

1

0

0

Not connected

 

 

 

 

1

0

1

Not connected

 

 

 

 

1

1

0

Cleared on compare match, set on overflow.

 

 

 

 

1

1

1

Set on compare match, cleared on overflow.

 

 

 

 

55

1228B–09/01