Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.59 MB, 687 trang )
588
Microprocessors
Prelab
1. Connect the audio jack connector in your parts kit to the output of the
DAC. Also connect an LED to pin RC2/CCP1 of the PICF242, and the potentiometer to the RA0/AN0 input. The audio jack allows an external
speaker to be driven by the DAC output. This capability is crucial for the
last experiment, so it is tested in this experiment.
2. Demo to the TA a spreadsheet that calculates the values required for Figure
E.8. Your assigned target frequencies are in Table E.5. The frequencies in
Table E.5 use the PWM mode for generating the square wave; this does not
use the postscaler for frequency calculation. The spreadsheet should calculate the PR2 values given a target frequency, and prescaling factors of 1, 4,
and 16. The spreadsheet should also truncate the PR2 value to an integer
value, and then compute the %diff between the actual frequency obtained
and desired frequency. Choose the prescale and PR2 value that gives the
lowest %diff value.
TABLE E.5 Assigned Frequencies
Last Digit of Student ID
Use These Frequencies
0 or 1
2500 Hz, 10 kHz, 121 kHz
2 or 3
3600 Hz, 15 kHz, 133 kHz
4 or 5
4200 Hz, 28 kHz, 144 kHz
6 or 7
5500 Hz, 37 kHz, 151 kHz
8 or 9
6100 Hz, 49 kHz, 165 kHz
3. Become familiar with the sqwave.c, ledpwm.c, and sinegen.c programs, as
they are used in this experiment.
Lab Activity
sqwave.c Program
The sqwave.c program uses the PWM module to generate a square wave on the
RC2/CCP1 output. The program prompts the user to enter Timer2 prescale and
PR2 values.
Appendix E: Suggested Laboratory Exercises
589
1. Use the sqwave.c program to check the values you computed for Figure E.8.
Use a scope to monitor the output waveform on pin RC2/CCP1.
FIGURE E.8 Prescale, PR2 values.
ledpwm.c Program
The ledpwm.c program outputs a square wave of a fixed frequency, but allows
dynamic update of the duty cycle by reading the AN0 analog input. This 10-bit
value is used to set the value of the duty cycle. Adjusting the potentiometer adjusts
the duty cycle of the square wave on the RC1/CCP1 pin. Connect an LED to the
RC1/CCP1 output so that the LED turns on when a high voltage is on the
RC1/CCP1 output.
1. Verify the operation of ledpwm.c on your PIC. What happens to the LED
brightness as you adjust the duty cycle via the potentiometer? Monitor the
waveform generated on pin RC2/CCP1 with the oscilloscope.
2. Use a multimeter to measure the current through the LED for various duty
cycles and complete Figure E.9, which requires current measurements for
two different duty cycles. The two duty cycles, based on your student ID,
are: a) 0/1 5%/25%; b) 2/3 10%/30%; c) 4/5/6 15%/35%; d) 7/8/9
20%/40%. Also, measure current at the 85% duty cycle, and at a duty cycle
midway between the two above. After recording your current measurements in lines (1) and (2) of Figure E.9, compute the expected current for
line 3 (halfway between 1st and 2nd duty cycles) and line 4 (85%) duty cycles. Ideally there is a linear relationship between the current and duty
cycle. Use the first two measurements to compute a straight line slope that
is used to predict the currents for the last two duty cycles.
sinegen.c Program
The sinegen.c program generates a sine wave using a table lookup approach via the
MAX517 DAC. The program prompts the user to choose between a 16-entry and a
64-entry table. Timer2 is used to trigger an interrupt that reads the next entry from
the table. The interrupt interval is set by a prescale value of 4, a postscale value of 3,
590
Microprocessors
FIGURE E.9 PWM current measurements.
and the PR2 value that is set by the ADC AN0 input. The sine wave period is
table_size * interrupt_interval; the 16-entry and 64-entry sine wave tables are
in sinegen.h. The PR2 value is limited by sinegen.c to be between 25 and 100.
1. Verify sinegen.c operation on your PIC. Hook the audio jack output to
some external powered speakers or headphones. Vary the period of the
sine wave via the potentiometer and make primitive music.
2. Use the scope to monitor the output of the DAC. Note what happens for
the 16-table case when the frequency is increased to near its maximum
value. The interrupt interval becomes too small for the DAC to be updated
with the new table value because of the I2C bus speed. This causes waveform values to be skipped, degrading waveform quality.
3. Fill in the computations and measurements required in Figure E.10. See the
comments after the table for hints on obtaining these values.
FIGURE E.10 sinegen.c measurements.
Appendix E: Suggested Laboratory Exercises
591
Values (a), (b) can be computed from the datasheet formula for Timer2 interrupt interval.
For (c) through (f), the period of the sinewave is the interrupt interval times the
number of table entries for the sinewave; the frequency is the inverse of the period.
For (h), compute the DAC update time by multiplying the number of I2C bit
times required for the DAC update by the measured I2C bus speed. The measured
value can be obtained by using the scope on the I2C bus.
For (i), the measured DAC update time determines the minimum time interval for each new sinewave value. The number of entries in the sinewave times this
value gives the minimum period of the sinewave that can be reliability generated
without skipping values. Sinewave values are skipped when the Timer2 interrupt
interval becomes less than the DAC update time.
Arbitrary Waveform Generation
1. Modify sinegen.c to generate an arbitrary waveform as described in Table
E.6. Following this table are detailed hints on how to implement the arbitrary waveform generator.
Table E.6 provides the details of the arbitrary waveform that you are required
to generate. The waveform is one or more sine wave cycles, followed by one or
more triangle wave cycles, followed by one or more square wave cycles that are then
repeated. A 1x period is 64 time units; waveforms can have periods of 1x, 2x, or
0.5x. The waveform generated by sinegen.c has a 1x period by this definition. The
interrupt interval for this program should be set in the same way as in sinegen.c. The
triangle wave and square wave can also be inverted; Figure E.11 shows examples of
inverted triangle waves and square waves. If one cycle of a waveform is inverted, all
cycles are inverted.
Your program must track the current cycle number and the corresponding
waveform to be generated. The tabmax variable in sinegen.c determines the period of
a waveform; this value can be changed from cycle to cycle depending on the waveform being generated (i.e., for 2x period tabmax = 128, for a 0.5x period tabmax =
32). Write separate subroutines for square wave and triangle wave generation. The
easiest way to implement this capability is by using lookup tables for all three waveforms. An alternate method is to compute the value of each point given the current
table index. This computation is easy for the square wave and more difficult for the
triangle wave.
592
Microprocessors
TABLE E.6 Waveform Assignments
Last Digit of Student ID
Waveform
0
1 cyc sine, 2 cyc triangle (0.5x per), 1 cyc square (0.5x
per)
1
2 cyc sine, 1 cyc triangle (2x per), 2 cyc square (0.5x
per)
2
1 cyc sine, 1 cyc triangle (0.5x per), 1 cyc square (2x
per)
3
2 cyc sine, 2 cyc triangle (2x per), 1 cyc square (2x per)
4
1 cyc sine, 2 cyc triangle (2x per, inverted), 1 cyc square
(2x per)
5
1 cyc sine, 2 cyc triangle (2x per, inverted), 1 cyc square
(0.5x per)
6
2 cyc sine, 1 cyc triangle (0.5x per), 2 cyc square (0.5 x
per, inverted)
7
1 cyc sine, 1 cyc triangle (0.5x per, inverted), 1 cyc
square (0.5 x per, inverted)
8
2 cyc sine, 2 cyc triangle (0.5x per, inverted), 1 cyc
square (2x per)
9
1 cyc sine, 2 cyc triangle (0.5x per, inverted), 2 cyc
square (2x per, inverted)
FIGURE E.11 Arbitrary waveform examples.
Appendix E: Suggested Laboratory Exercises
593
E.13 EXPERIMENT 12: TIME MEASUREMENT AND IR WAVEFORM
DECODING (CHAPTER 13)
This program covers the use of the capture/compare module for time measurement
(Chapter 13). This lab assumes that the student has access to a universal remote
control.
PRELAB
1. Connect a momentary switch to the RC2/CCP1 input.
2. The program swdetov.c uses Timer1 and the capture module to measure
the pulse width of a momentary switch. Read this program and understand its operation, as you will need to modify it to fulfill the lab requirements.
Lab Activity
Pulse Width Measurement Using swdetov.c
1. The swdetov.c program uses the PIC18F242 capture module to measure the
low pulse width of a momentary switch as discussed in Section 13.4. Verify the operation of swdetov.c on your PIC18F242. The “timer tics” that is
printed is the elapsed timer tics between the edges; the pulse width is the
computed time in microseconds.
2. Fill in Figure E.12 for three button pushes. Use a scope in single trigger
mode and capture the low pulse width.
FIGURE E.12 Momentary switch pulse width results (original swdetov.c).
594
Microprocessors
swdetov.c Modification
1. Modify swdetov.c to use CCP2 as the input pin, capture register CCPR2,
and Timer3 as the timebase.
2. Fill in Figure E.13 for three button pushes. Use a scope in single trigger
mode and capture the low pulse width.
FIGURE E.13 Momentary switch pulse width results (modified swdetov.c).
IR Waveform Decoding
1. Place the IR receiver module (Radio Shack PN #276-640) from your parts
kit on the protoboard, and connect the OUT pin to the RC2/CCP1 pin of
the PIC.
2. On the universal remote, locate a manufacturer setting that produces
space-width encoded output as discussed in Chapter 13 (use the oscilloscope to verify that the output waveform is space-width encoded). Write a
program similar to the biphase decoding program of Figures 13.22, 13.23,
and 13.24 to perform space-width decoding. Space-width decoding is easier than biphase decoding, as the only measurement required is the time
between every falling edge of the incoming waveform because “0” and “1”
bits have different periods. Only print the first 2 bytes of a received waveform.
E.14 EXPERIMENT 13: AUDIO RECORD/PLAYBACK
(CHAPTER 14)
This experiment implements the audio record/playback project of Chapter 14.