Skip to main content
Contents
Dark Mode Prev Up Next
\(\newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 9.9 Practice Problems
Unless otherwise specified, assume a clock frequency of 16ย MHz to answer all of the following questions.
1.
How many timer/counters are available on the ATmega328P?
2.
True or false: Timer/counterย 0 on the ATmega328P is a 16-bit counter.
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 ____.
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.
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 . 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.
9.
In CTC mode, calculate the delay that is obtained when
OCR1A = 1499 and the prescaler is 64.
Answer . Solution .
\(T = \frac{64 \times (1499 + 1)}{16 \times 10^6~\textrm{Hz}} = 6~\textrm{ms}\)
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 . 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.