Skip to main content

Microcontrollers

Exercises 9.9 Practice Problems

Unless otherwise specified, assume a clock frequency of 16ย MHz to answer all of the following questions.

3.

On timer/counterย 0 and timer/counterย 2, in normal mode, when the counter rolls over, the value stored in TCNTn goes from a value of ____ to a value of ____.
Answer.
0xFF to 0x00

4.

Calculate the period and duty cycle of the waveform depicted in Figureย 9.9.1, given an I/O clock frequency of 1ย MHz and prescaler of 1.
An input signal is shown oscillating between 0 and 5 V at regular intervals. The value in TCNT1 at the first rising edge is 4. The value in TCNT1 at the first falling edge is 10. The value in TCNT1 at the second rising edge is 29. The value in TCNT1 at the second falling edge is 35. The value in TCNT1 at the third rising edge is 54. The value in TCNT1 at the third falling edge is 60.
Figure 9.9.1. Example waveform captured with timer/counterย 1.
Answer.
\(T = 25~\mu\textrm{s}\text{,}\) \(D = 24\%\)
Solution.
\(T = (29 - 4) \times \frac{1}{1 \times 10^6~\textrm{Hz}} = 25~\mu\textrm{s}\)
\(D = \frac{35 - 29}{29 - 4} = \frac{6}{25} = 24\%\)

5.

In normal mode, what is the longest possible delay that can be obtained using timer/counterย 0 or timer/counterย 2? With what prescaler value is this achieved?
Answer.
\(T_{max} = 16.38~\textrm{ms}\text{,}\) \(N = 1024\)
Solution.
\(T_{max} = \frac{1024 \times 2^8}{16 \times 10^6~\textrm{Hz}} = 16.38~\textrm{ms}\)

6.

In normal mode, what is the longest possible delay that can be obtained using timer/counterย 1? With what prescaler value is this achieved?
Answer.
\(T_{max} = 4.19~\textrm{s}\text{,}\) \(N = 1024\)
Solution.
\(T_{max} = \frac{1024 \times 2^{16}}{16 \times 10^6~\textrm{Hz}} = 4.19~\textrm{s}\)

7.

In normal mode, how much time elapses between timer/counter overflows when using timer/counterย 1 and a prescaler of 8?
Answer.
32.768ย ms
Solution.
\(T = \frac{8 \times 2^{16}}{16 \times 10^6~\textrm{Hz}} = 32.768~\textrm{ms}\)

8.

In normal mode, what prescaler must be used with timer/counterย 2 to achieve a delay of 0.512ย ms? Is this possible with timer/counterย 0? Why or why not?
Answer.
N = 32 using timer/counterย 2. This is not possible with timer/counterย 0 because it does not have a prescaler option of 32.

10.

In CTC mode using timer/counterย 2 and a prescaler of 32, calculate the value that must be stored in OCR2A to obtain a delay of 0.38ย ms.
Answer.
OCR2A = 189
Solution.
\(TOP = \frac{T \times f_{CLK,I/O}}{N} - 1 = \frac{(0.38 \times 10^{-3}~\textrm{s})(16 \times 10^6~\textrm{Hz})}{32} - 1 = 190 - 1 = 189\)

11.

In CTC mode, calculate the prescaler to must be used if OCR0A = 140 and the desired time interval should be approximately 9ย ms.
Answer.
Solution.
\(N = \frac{T \times f_{CLK,I/O}}{TOP + 1} = \frac{(9\times 10^{-3}~\textrm{s})\times(16 \times 10^6~\textrm{Hz})}{141} = 1021\)
The closest possible prescaler value to this is 1024.