Timer PWM Questions and Answers
Timer PWM Questions and Answers
Timer PWM Questions and Answers
Reference:
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?
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?
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
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.
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).
10. Again assuming the call motor.setSpeed(200); what duty cycle would be generated on the
OC2A pin?