首页 > > 详细

EECS 461代写、辅导C/C++设计编程

EECS 461 Fall 2023
Lab 3: Analog-To-Digital Conversion
1 Overview
In this lab you will learn how to use the analog to digital converters on the NXP S32K144 microcontroller.
You will then sample a sine wave produced by the signal generator and display the result on a virtual
oscilloscope to study the phenomenon of aliasing. See Chapter 42 in the S32K144 Reference Manual:
“Analog-to-Digital Converter (ADC).” A block diagram of the ADC is found in Figure 1.
Figure 1: Block diagram of the ADC module.
EECS 461: Embedded Control Systems 1 Fall 2023
Lab 3 Analog-To-Digital Conversion ANin
GND
GND
5V
5V
6 4 2 0
7 5 3 1
Potentiometer
(Turning Knob)
PotJump (Connector)
Figure 2: Pin connection matrix between external signals and S32K144 microcontroller.
1.1 Analog-To-Digital Converter Module
The S32K144 contains two successive approximation register (SAR) A-D converter modules, referred to
as ADC0 and ADC1, each of which has 16 input channels. In Lab 3, we shall only use channel 0 of ADC0.
The ADC may be operated using either software or hardware to trigger conversions; we shall only use
software triggered conversions in Lab 3. The ADC also has single and continuous conversion modes; we
shall only use single mode conversions.
The clock for the S32K144 ADC uses an 8 MHz clock instead of the 80 MHz system clock.
2 Design Specifications
2.1 Hardware
The interface board has connections to eight of the input pins from the ADC module. The connections
are made to pins ANin0 through ANin7. Each input is connected to a four-pin header consisting of the
input signal, ground, a +5 volt reference and one unused pin.
A potentiometer is mounted permanently on the interface board to provide a variable input signal for
A/D conversion. Note the jumper that must be connected to the ANin0 signal pin for this to work,
as shown in Figure 2. In this lab, you will use this potentiometer as well as a sine wave from a signal
generator as input to the board.
2.2 Software
adc.h and adc.c
The first file, adc.h, contains function prototypes for three functions. You will be responsible for writing the definitions for these functions in the other file, adc.c (rename
adc template.c). As with Lab 2, place the adc.h file in the include directory and your
completed adc.c file in the lib directory.
lab3o.c
When using an A/D converter to sample an analog signal for an embedded control application,
it is almost always important that the samples be taken at a specified fixed interval. Variations
EECS 461: Embedded Control Systems 2 Fall 2023
Lab 3 Analog-To-Digital Conversion
in sample time are referred to as timing jitter, and are usually undesirable. To achieve fixed
interval sampling, the program lab3o.c allows you to sample the output of a signal generator
at a 50kHz sampling rate. The sample values are then transferred over the serial port to the
lab station desktop so that they may be displayed on a Matlab based virtual oscilloscope.
To achieve the desired sampling frequency, the A/D conversions are performed in a periodic
interrupt routine that executes every 0.02 msec. We have not yet learned how to use the
interrupt timers on the S32K144, and thus this file is provided to you directly and you don’t
need to make any modifications. Place lab3o.c in the lab3o directory.
virtual oscilloscope.slx
This file is a virtual oscilloscope program built in Simulink. As described in the discussion
of lab3o.c, the data used by the oscilloscope is collected by the ADC at a 50kHz sampling
rate. When executed, the virtual oscilloscope sampled date plotted in the time domain. The
other plot is a frequency domain FFT of the data, which is useful for observing the frequency
content of the data sequence from the serial port. We shall use the virtual oscilloscope to plot
the data collected at a 50kHz sampling rate using lab3o.c for sine wave outputs of various
frequencies. You will observe the e↵ects of aliasing and imperfect reconstruction in both the
frequency and time domains. See example plots in Figure 3.
Figure 3: Output of Virtual Oscilloscope Simulink Model: time samples and FFT of a 1 kHz sine wave.
EECS 461: Embedded Control Systems 3 Fall 2023
Lab 3 Analog-To-Digital Conversion
3 Pre-Lab Assignment
Pre-Lab questions must be done individually and a .pdf copy must be uploaded before the due date and
time specified on Canvas. PLEASE INDICATE YOUR ANSWERS CLEARLY. Note that you will need
refer to your Pre-Lab solutions while you are working in the lab. You must also, with your partner,
design an initial version of your software before you come to your lab section. Your software does not
need to be completely debugged, but obviously more debugging before coming to lab means less time you
will spend in the lab.
Within the file adc.c, there are three functions to be completed: init ADC0 single, ADC0 complete,
and read ADC0 single. The prototypes of these functions can be found in the adc.h file and you should
modify the code provided in adc template.c.
1. void init ADC0 single(void): There are several registers that need to be set to initialize the
ADC0 module. These are the Peripheral Clock Controller (PCC) register, the Status and Control
Registers SC1[0], SC2, and SC3, the the configurations registers CFG1 and CFG2.
• SC1[0] (Section 42.4.2): According to Table 41-2, the ADCH bitfield should be set to 0x1F
to disable the ADC0 module for configuration (the disable setting in Section 42.4.2 is not
applicable to the S32K144 processor). To enable a conversion, set ADCH to the desired channel.
The AIEN bitfield enables an interrupt when a conversion is completed; we do not use this
feature.
• CFG1 (Section 42.4.3): The MODE bitfield is set to specify the resolution of the ADC – use the
full 12-bit resolution. The ADICLK bitfield selects the ADC clock – use Alternate clock 1.
• CFG2 (Section 42.4.4): The SMPLTS bitfield selects the sample time, or time taken to acquire
an analog voltage for conversion. Use the default value of 13 ADC clock cycles.
• SC2 (Section 42.4.7): Set the ADTRG bitfield to specify software triggered conversions, and the
REFSEL bitfield to select the default reference voltages, VREFH and VREFL.
• SC3 (Section 42.4.8): Set the ADCO bitfield for single conversion mode. Set the other bitfields
to disable calibration and hardware averaging.
2. uint8 t ADC0 complete(void): This function returns the value of the COCO bit from the SC1[0]
register. The COCO bit equals 0 until the conversion is complete, when it is set to 1. Reading the
result of the ADC conversion clears the COCO bit.
3. uint32 t read ADC0 single(uint16 t inputChannel): This function does four things:
• Initiate conversion by writing the ADC channel number to the ADCH bitfield of the SC1[0]
register.
• Wait for conversion to complete.
• Read Data Result Register R[0] (Section 42.4.5).
• Convert the result to millivolts.
4. Suppose that we sample a sine wave with frequency f0 Hz in a software loop with
approximate execution time Ts seconds (and thus with sample frequency fs = 1/Ts
Hz). Assume that the frequency of the sine wave f0 is approximately (but not exactly)
equal to the sample frequency. State an expression for the apparent frequency of the
sampled sine wave. What is the approximate value of this frequency?
5. During the In-Lab, you will be asked to sample sine waves output from the signal generator in
a timed 50kHz software loop and display the results on a software oscilloscope. As described in
the handout “Sampling, Beats, and the Software Oscilloscope”, you may see a variety of di↵erent
phenomena, depending on the frequency of the sine wave with respect to the sampling frequency.
EECS 461: Embedded Control Systems 4 Fall 2023
Lab 3 Analog-To-Digital Conversion
For each of the sine wave frequencies listed below, which of the four plots in the
handout (i.e., which of Figures 1-4) corresponds to the result you should see? Note:
there is a 1-1 correspondence between Figures 1-4 and sine waves (a)-(d).
(a) f0 = 12.4 kHz;
(b) f0 = 55 kHz;
(c) f0 = 10 kHz;
(d) f0 = 24 kHz.
4 In-Lab Assignment
4.1 Single Mode Conversion Testing
1. Write a simple C program called lab3.c that uses the init ADC0 single and read ADC0 single
functions from adc.c to retrieve the value of an analog input from the potentiometer and store it
to a variable called iAnalog.
2. Run the lab3.c. Adjust the potentiometer to change the analog signal sent to the pin between 0
and 5 volts. Verify that the value of iAnalog has changed and that you can correctly read the full
voltage range.
4.2 Timing
In the Post-Lab Assignment, you will be asked to consult the S32K144 reference manual and compute
Tconv, the amount of time required for the hardware to perform one A/D conversion. We now measure
two other times of interest. Modify your lab3.c file so that, before the call to the read ADC0 single
function, one of the LEDs is set to high and is set back to low after the function returns. Connect an
oscilloscope to the GPO output pin.
1) Measure TRead, the amount of time required for one execution of the function read ADC0 single.
2) Measure TLab3, the period with which read ADC0 single is called.
Record these times for later analysis.
4.3 From Sine Wave to Square Wave
In this section of the lab, we read a sine wave output from a signal generator and display the result on
the oscilloscope. Because we can only output a digital signal, we cannot display the sine wave directly
in this way; instead, we convert the sine wave into a square wave. To do so, change your lab3.c file so
that when the ‘ANin0’ input measured in read ADC0 single is greater than approximately half of the
maximum value for the A/D conversion a digital output is set to output high. Otherwise, the digital
output will output low. Connect a function generator to the ‘ANin0’ signal pin and generate a sine wave
with parameters 0-5 V, 2.5 V o↵set, 5 Vpp, and 3 kHz. Use an oscilloscope to observe both the input
signal and the output signal. Use the stop and run features on the oscilloscope to freeze the waveform if
necessary.
As in Section 4.2, measure the amount of time Ts between successive calls to read ADC0 single, and
thus determine the frequency fs at which the sine wave is being sampled. Record these values for later
analysis. First try an input sine wave with frequency f0 much smaller (say by a factor of ten) than fs,
and record the approximate frequency of the resulting square wave. Next, try an input sine wave with
frequency f0 that is about equal to the sample frequency (say 95%), and again record the frequency of
the resulting square wave. Repeat a few times with di↵erent values until you are comfortable with the
relation between sine wave and square wave frequencies.
EECS 461: Embedded Control Systems 5 Fall 2023
Lab 3 Analog-To-Digital Conversion
4.4 The Virtual Oscilloscope
In this section of the lab, we again read sine wave output from a signal generator, and we output the
measured data over the serial port to a virtual oscilloscope program written in Simulink. To do so, use the
file lab3o.c and the Simulink model virtual oscilloscope.slx provided on the course website to plot
the data. Attach the signal generator to your interface board as shown by your instructor. Demonstrate
the working virtual oscilloscope to your instructor using a 3 KHz sinusoid. (Remember to put the signal
generator in high-Z mode). Recall that the data plotted by the virtual oscilloscope is collected by the
program lab3o.c at a 50 kHz sampling rate.
Increase the input signal frequency slowly until you see the “beat” phenomenon illustrated in Figure 3
of [1]. Record the frequency of the input signal and the resulting beat period for later analysis. Further
increase the input signal frequency until you see aliasing, as illustrated in Figure 2 of [1]. Record the
signal frequency and the resulting alias frequency for further analysis. Demonstrate to your GSI that you
can produce all four cases discussed in [1].
Save a copy of your code to hand in with the Post-Lab Assignment.
5 Post-Lab Assignment
1. According to Section 42.5.4.5 of the S32K144 Reference Manual, the time Tconversion required to
perform one A-D conversion consists of (i) the time Tsample required to sample the analog signal;
(ii) the time Tcompare required to set all 12 of the converted bits; and (iii) some additional overhead
time. Given that the ADC clock TADC is set to 0.125 µsec (fADC = 8 MHz) and the bus clock is
set to TBUS = 0.025µsec (fBUS =40 MHz), what are the values of (i) Tsample, (ii) Tcompare, and
Tconversion? Express your answer in µseconds.
2. In Section 4.2, you measured TRead, the amount of time required for one execution of read ADC0 single,
and TLab3, the period with which read ADC0 single is called. What values did you get for
TRead and TLab3? Recall that Tconversion is the amount of time required to perform one
conversion. Why is TRead greater than Tconversion? Why is TLab3 greater than TRead?
Assume e↵ects due to hardware inaccuracies are negligible. Limit your complete response to less
than 5 sentences.
3. For In-Lab part 4.3, what values did you record for the loop sample time and frequency?
For the low frequency input case, what value of input sine wave frequency did you use,
and what was the frequency of the resulting square wave? For the case fs ⇡ f0, what
values of input sine wave frequency and output square wave frequency did you record?
Verify that these values are consistent with your answer to question 4 of the Pre-Lab.
4. When you used the virtual oscilloscope in Section 4.4, you recorded signal frequencies that resulted
in aliasing, and in beats, as depicted in Figures 2 and 3 of [1].
(a) What was the frequency f0 of the sine wave from the signal generator that resulted
in aliasing? What was the frequency fA of the resulting alias? Verify that these
signals di↵er by an integer multiple of the sampling frequency by finding that
value of k for which f0 kfs = ±fA.
(b) What was the frequency f0 of the sine wave for which you observed periodic beats
on the virtual oscilloscope? What were the period and frequency fbeat of the
resulting beats? Verify that fbeat = 2(fN f0).
Be sure to upload a copy of your code with your solutions to the Post-Lab.
If you have comments that you feel would help improve the clarity or direction of this assignment, please
include them following your postlab solutions.
EECS 461: Embedded Control Systems 6 Fall 2023
Lab 3 Analog-To-Digital Conversion
References
[1] J. S. Freudenberg, “Sampling, Beats, and the Software Oscilloscope” available from the EECS 461
Canvas website.
EECS 461: Embedded Control Systems 7 Fall 2023

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!