beautypg.com

3 fast pwm mode, Atmega128rfa1 – Rainbow Electronics ATmega128RFA1 User Manual

Page 258

background image

258



8266A-MCU Wireless-12/09

ATmega128RFA1

cleared to zero when the counter value (TCNTn) matches either the OCRnA (WGMn3:0
= 4) or the ICRn (WGMn3:0 = 12). The OCRnA or ICRn define the top value for the
counter, hence also its resolution. This mode allows greater control of the compare
match output frequency. It also simplifies the operation of counting external events.

The timing diagram for the CTC mode is shown in the following figure. The counter
value (TCNTn) increases until a compare match occurs with either OCRnA or ICRn,
and then counter (TCNTn) is cleared.

Figure 18-6. CTC Mode Timing Diagram

TCNTn

OCnA
(Toggle)

OCnA Interrupt Flag Set
or ICFn Interrupt Flag Set
(Interrupt on TOP)

1

4

Period

2

3

(COMnA1:0 = 1)

Each time the counter reaches the TOP value an interrupt can be generated by either
the OCFnA or ICFn Flag according to the register used to define the TOP value. If the
interrupt is enabled, the interrupt handler routine can be used for updating the TOP
value. However, changing TOP to a value close to BOTTOM when the counter is
running with no or a low prescaler value must be done with care since the CTC mode
does not have the double buffering feature. The counter will miss the compare match if
the new value written to OCRnA or ICRn is lower than the current value of TCNTn. The
counter will then have to count to its maximum value (0xFFFF) and wrap around
starting at 0x0000 before the compare match can occur. In many cases this feature is
not desirable. The fast PWM mode is available as an alternative using OCRnA for
defining TOP (WGMn3:0 = 15). The OCRnA then will be double buffered.

For generating a waveform output in CTC mode, the OCnA output can be set to toggle
its logical level on each compare match by setting the Compare Output mode bits to
toggle mode (COMnA1:0 = 1). The OCnA value will not be visible on the port pin unless
the data direction for the pin is set to output (DDR_OCnA = 1). The waveform
generated will have a maximum frequency of f

OCnA

= f

clk_I/O

/2 when OCRnA is set to zero

(0x0000). The waveform frequency is given by the following equation:

)

1

(

2

/

_

OCRnA

N

f

f

O

I

CLK

OCnA

+

=

The N variable represents the prescaler factor (1, 8, 64, 256, or 1024).

As for the Normal mode of operation, the TOVn Flag is set in the same clock cycle of
the timer when the counter changes from MAX to 0x0000.

18.9.3 Fast PWM Mode

The fast Pulse Width Modulation (PWM) mode (WGMn3:0 = 5, 6, 7, 14 or 15) provides
a high frequency PWM waveform generation option. The fast PWM differs from the
other PWM options by its single-slope operation. The counter counts from BOTTOM to
TOP then restarts from BOTTOM. In non-inverting Compare Output mode, the Output
Compare (OCnx) is cleared on the compare match between TCNTn and OCRnx, and