[go: up one dir, main page]

0% found this document useful (0 votes)
150 views4 pages

Timer PWM Questions and Answers

The document discusses the timer/counter PWM subsystem on the ATmega328P microcontroller. It provides information about the different PWM modes and how to configure timer/counter 2 to operate in fast PWM mode with a prescale value of 32 to generate a PWM signal on pin OC2A with a frequency of approximately 2 kHz and a duty cycle that can be set by writing values to the OCR2A register. Example C code is given to configure the timer registers and set the pin as an output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views4 pages

Timer PWM Questions and Answers

The document discusses the timer/counter PWM subsystem on the ATmega328P microcontroller. It provides information about the different PWM modes and how to configure timer/counter 2 to operate in fast PWM mode with a prescale value of 32 to generate a PWM signal on pin OC2A with a frequency of approximately 2 kHz and a duty cycle that can be set by writing values to the OCR2A register. Example C code is given to configure the timer registers and set the pin as an output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Timer/Counter PWM Subsystem

Reference:

1. Timer with PWM


2. Adafruit Motor Shield - Part 2 Timer PWM
3. Section 17. "8-bit Timer/Counter2 with PWM and Asynchronous Operation" of the ATmega328P
datasheet

Figure 17-1. 8-bit Timer/Counter Block Diagram

1. For Timer/Counter 2, which waveform modes implement pulse width modulation?

ANSWER: Modes 1, 3, 5, 7

2. Which mode(s) instructs the timer to count up to a specified value and then restart from the
BOTTOM (0x00). What is this mode(s) named?

ANSWER: Modes 3 and 7 Fast PWM


For more on the Fast PWM Mode read Section 17.7.3 "Fast PWM Mode" in the datasheet.
3. Which mode(s) instructs the timer to count up to a specified value and then counts down to
BOTTOM (0x00)? What is this mode(s) named?

ANSWER: Modes 1 and 5 Phase-Correct PWM

4. What modes restrict the frequency of the PWM to one of five (5) values defined by (a) the clock
frequency (fclk) (b) a prescale value (1, 8, 64, 256, 1024), and (c) the value of TOP?

ANSWER: Modes 3 and 1

5. What values would needed to be saved in registers TCCR2A and TCCR2B so Timer/Counter 2
operates in Mode 3 Fast PWM, OC2A non-inverted output and OC2B off, with a clock prescaler of
clkI/O/32. Hint: Review Section 17.11 Register Description in the ATmega datasheet or Section 7.3
Pertinent Register Descriptions in your textbook. Hint: read the Adafruit Motor Shield - Part 2 pdf
document and Section 17.7.3 "Fast PWM Mode" in theATmega datasheet.

ANSWER:
In fast PWM mode, the compare unit allows generation of PWM waveforms on the OC2A pin.
Setting the COM2A1 and COM2A0 bits to two (102) will produce a non-inverted PWM TOP is
defined as 0xFF when WGM2:0 = 3 (See Table 17-3 on page 158). The actual OC2A value will only
be visible on the port pin if the data direction for the port pin is set as output.
TCCR2A = 0b10000011 = 0x83
TCCR2B = 0b00000011 = 0x03

6. Using the _BV(bit) macro, write the C code to configure TCCR2A without modifying any other bits.
Specifically do not modify the configuration of the output OC2B. Hint: read the Adafruit Motor
Shield - Part 2 pdf document

TCCR2A |= _BV(COM2A1) | _BV(WGM20) | _BV(WGM21); // fast PWM, turn on oc0

7. Write the C code to configure bit 3 of PORT B as an output so you can see the PWM output.
ANSWER: DDRB |= _BV(3);

8. Assuming that Timer/Counter 2 is configured in FAST PWM mode as described in the last question,
what frequency would generated on output OC2A given the 16 MHz clock of the Arduino.

ANSWER: fOC2APWM = 1.953 KHz

DISCUSSION:
For our 8-bit counter/timers the PWM frequency is given by the equation:

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

9. Given Adafruit method call motor.setSpeed(200); what hexadecimal number would be


loaded into Output Compare Register A (OCR2A).

ANSWER: OCR2A = 0xC8

10. Again assuming the call motor.setSpeed(200); what duty cycle would be generated on the
OC2A pin?

ANSWER: Duty Cycle = 200/255 = 78 %

You might also like