Homework4 Solution
Homework4 Solution
Homework4 Solution
Chapter Problems
• Fundamental
1. An analog signal has a spectrum (frequency content) which varies from 400 Hz to 5.9 KHz. It is to be
sampled at a rate of 10,000 samples per second. Is the sampling rate adequate to allow reconstruction
of the signal?
Answer: The Nyquist criterion indicates that a signal should be sampled at a minimum of twice the
highest frequency content in the signal. Applying the Nyquist criterion, the analog signal should be
sampled at a least 11.8 KHz. Therefore, the sampling rate of 10,000 Hz is not adequate.
2. The HC12 analog-to-digital converter system converts each analog sample to an eight bit unsigned bi-
nary value. What is the resolution of the converter? Assume that the reference voltages for the HC12
designated as VRH and VRL are 4 volts and 1 volt, respectively.
Answer:
3. The S12 microcontroller allows the selection of either 8-bit or 10-bit ATD resolution, what is the reso-
lution of the S12 ATD converter when the converter is set for 8-bit resolution? What if the converter
is set for 10-bit resolution? Assume that the reference voltages VRH and VRL are 5 volts and 0 volt,
respectively.
Answer: The two reference levels chosen for our ATD converter is 5.0 volts for VRH and 0 volts for
VRL . If we divide the voltage span between these two references into 256 identically spaced levels, the
voltage difference (quantum) represented by each step is:
85
86 CHAPTER 9. ANALOG-TO-DIGITAL (ATD) CONVERTER
Intuitively, if we increase the number of steps between the two voltage references, the individual voltage
steps become smaller or the converter resolution improves. In general, resolution may be described by:
Assume that the reference voltages for the S12 designated as VRH and VRL are 5 volts and 0 volts,
respectively. The resolution is given by:
4. What is the advantage of using ATD 10-bit resolution over 8-bit resolution? Is there a disadvantage?
Why not always set the microcontroller for 10-bit resolution?
Answer: With 10 bits unknown voltages can be resolved to 4.88 mV; whereas, with 8 bits resolution is
19.56 mV. The results of a 10-bit ATD conversion requires an unsigned integer variable for representa-
tion; whereas, an 8-bit result may be represented with an unsigned character variable. The resolution
must be chosen for the application at hand.
5. A 2 KHz square wave is to be sampled using an analog-to-digital conversion system. What should the
sampling frequency be?
Answer: The Nyquist criterion indicates that a signal should be sampled at a minimum of twice the
highest frequency content in the signal. A square wave has significant frequency content up to 10 times
the fundamental frequency or in this example 20 KHz. Applying the Nyquist criterion, the analog signal
should be sampled at a least 40 KHz.
6. In the question above, how many bits of binary data is generated per second assuming an eight bit
analog-to-digital converter?
Answer: With the signal sampled at 40,000 times per second, 320 Kbytes per second are generated
(40,000 samples x 8 bits/sample).
7. What is the dynamic range of the ATD system aboard the HC12? Explain.
Answer: If unwanted frequency components cause the Nyquist criterion to be violated, frequency
folding or aliasing will occur. In the process of recovering the signal, the folded part of the spectrum
causes distortion in the recovered signal which can not be removed by filtering from the recovered signal.
Frequency folding is eliminated by using a LPF which has a cutoff frequency corresponding to fh , the
expected frequency content of the signal.
9. If VRH = 5.0 V and VRL = 0 V, what digital value is returned when the ATD system converts the
following voltage assuming an 8 bit conversion?
– 5.0 volts
– 0.0 volts
– 2.7 volts
– 3.2 volts
– 1.2 volts
89
0 volts = 500 mV ∗ K + B
13. What analog value corresponds to the HC12 converter’s maximum digital output (all 1’s) when VRH =
5.0 V and VRL = 0 V ?
Answer:
Answer: The Select 8 Channel Mode (S8CM) bit (bit 6) must be set to logic “0” to set a conversion
sequence of four conversions. The Enable Continuous Channel Scan (SCAN) bit (bit 5) must be set
to logic “1” for a continuous conversion sequence. The Enable Multichannel Conversion (MULT) bit
(bit 4) must be set to logic “1” to configure the ATD sequencer to perform sequential conversions in a
specific channel group. The “CD” and “CC” bit must be set to “01” to scan channels AN4 through AN7.
Putting all this information together provides the control word “0011 01XX”. Where “XX” indicates
don’t care bit values. Therefore, a $34 should be loaded to the ATD Control Register 5 (ATDCTL5).
The results of the conversions will be placed in ATD Converter Result Registers 0-3.
4. Repeat the question above for the S12 processor.
Answer: The S12 ATD registers should be set in the following manner:
– ATDCTL2: $80 (ADPU = 1, AFFC = 0)
94 CHAPTER 9. ANALOG-TO-DIGITAL (ATD) CONVERTER
based ATD system similar to the HC12. However, the S12 ATD has additional enhanced features. These
powerful enhanced features which include:
– Selectable 8 or 10 bit resolution,
– Left/right justified and signed/unsigned result data,
– External trigger control,
– Flexible 1 to 8 conversion sequence length configuration, and
– Multiple ATD Channels.
• Challenging
1. Write a subroutine that reads channel 1 of the ATD, calculates the average value of the last 8 data
points, and outputs the value to PORT B of the HC12.
Answer: The structure chart for this program is shown in Figure 9.5. We will use the “INIT” routine
from the text. We will modify the “CONVERT” subroutine and modify the control word sent to
“ATDCTL5” with the following values:
– S8CM = 1, 8 channel conversions
– SCAN = 0, single conversion
– MULT = 0, single channels
– CD,CC,CB,CA = “0001”, channel signal 1
– ATDCTL5 control word = $41
INCLUDE ’HC12REG.ASM’
;—————————————-
;MAIN PROGRAM ;
;—————————————-
ORG $7000 ;User RAM at $7000
FIFTEEN EQU $0F ;loop control constant
;MAIN
BSR INIT ;Branch to INIT for initialization
BSR CONVERT ;Branch to CONVERT for conversion
DONE BSA DONE ;Branch to self
;—————————————-
;Subroutine INIT: Initialize ATD ;
;—————————————-
convert
main initialize
accumulator D
initialize
initialize
convert ATD
conversions
initiate
conversion sequence
conversion
complete?
yes
sum eight
conversions
divide sum
by eight
output result
to PORT B
RTS
;—————————————-
;Subroutine CONVERT ;
;—————————————-
;Set-up ATD, make 8 conversions on channel 1, average conversions
;send results to Port B
2. Repeat the question above for the S12 microcontroller. Choose an appropriate output port for data
output.
Answer: The code solution will be very similar. Change the register values to the following values:
– ATDCTL2: $80 (ADPU = 1, AFFC = 0)
– ATDCTL3: $44 (FIFO = 1, S8C:4C:2C:1C = 1000)
– ATDCTL4: $80 (SRES8 = 1)
– ATDCTL5: $21 (DIM = 0, DSGN = 0, SCAN = 1, MULT = 0, CC:CB:CA = 001)
3. Write a subroutine that continuously senses the voltage at PAD3. If the voltage is between 0 and 1 volt,
bit 0 on PORT B is set high. If the voltage is between 1 and 2 volts, bit 1 on PORT B is set high. If
the voltage is between 2 and 3 volts, bit 2 on PORT B is set high. If the voltage is between 3 and 4
volt, bit 3 on PORT B is set high. If the voltage is between 4 and 5 volts, bit 4 on PORT B is set high.
Answer: The structure chart for this problem is provided in Figure 9.6. The flow chart for subroutine
“CONVERT” is also provided. The subroutine will convert the voltage on PAD3 and then compare the
converted value in turn with the hexadecimal equivalent for 1V, 2V, 3V, and 4 Volts. The proper mask
is sent to Port B when the voltage level is determined. The voltage comparison values and the mask
values are also provided in the figure.
INCLUDE ’HC12REG.ASM’
;—————————————-
;MAIN PROGRAM ;
;—————————————-
ORG $7000 ;User RAM at $7000
VOLT1 EQU $33 ;1 volt comparison value
VOLT2 EQU $66 ;2 volt comparison value
VOLT3 EQU $99 ;3 volt comparison value