# E72 Lab #5

A/D & D/A (PWM) Convertors

Make sure you read the lab rules about keeping the lab clean.  In particular, put away all components, wires and connectors when you are done. If you need to leave a circuit set up on a breadboard, put the breadboard in one of the cabinets or drawers when you leave.

The following might be useful: D/A and A/D convertors

In this lab you will be working with Digital to Analog (D/A - read as "D to A") and Analog to Digital (A/D - "A to D") convertors.

D/A convertors are used to generate an analog voltage from a number stored in the microcontroller. For example a three bit convertor with a range of 0-3.3 volts would have the following outputs. Note that the highest output is actually 3.3V*(2N-1)/2N (where N is the number of bits, 3 in this case). A binary value of 110 (decimal=6) would ideally result in 2.475 Volts.

 Input D/A Output Decimal Binary 0 000 0.000 V 1 001 0.4125 V 2 010 0.8250 V 3 011 1.2375 V 4 100 1.6500 V 5 101 2.0625 V 6 110 2.4750 V 7 111 2.8875 V

An A/D convertor performs the opposite operation, it takes an analog voltage and converts it to a binary equivalent.  A voltage between 2.475 and 2.8875 volts will result in an output of 110 (decimal=6).

 A/D Input Output Decimal Binary <0.4125 V 0 000 0.4125-0.8250V 1 001 0.8250-1.2375V 2 010 1.2375-1.6500V 3 011 1.6500V-2.0625V 4 100 2.0625-2.4750V 5 101 2.4750-2.8875V 6 110 >2.8875V 7 111

For this part of the lab, use your daughterboard on a breadboard. With Vcc and ground applied.

### 1) Build a simple parallel D/A convertor The first task this week is to build a simple D/A convertor and evaluate its performance.  Connect the circuit shown with R=10kΩ.  Keep the resistors separate from the op-amp because we will be swapping in a different op-amp later.  Wire neatly (make the breadboard look like the schematic and things should go quickly).

Note: I have wires that will let you easily connect from LaunchPad to breadboard.

Use a TLV2772 opamp powered from 0 and 5 volts.  The pinout is given below.  Write a program that sets P2.1, P2.2 and P2.3 as outputs.  Create a variable "i" that increments in a loop at about a 100 Hz rate.  In the loop use the statement "P2OUT = i;".  This will increment the output of the D/A converter (note: only 3 bits of "i" are important to the circuit, so there is no need to limit the range of "i").

Predict and measure the output voltages as i varies.  Note: you can pause the program, go to View→Locals, and change the value of i directly (or go to View→Registers, and change P2OUT manually). You can't view the registers while the program is running.

Get a screen shot of the output as the program runs clearly showing the discrete analog voltage levels.

Replace the TLV2772 with an LF411 op amp, but keep power at +5V and ground (you'll have to rewire because they have different printouts), and redo the screen shot (note: it won't be nearly as good).  ### 2) Using an A/D convertor

Install the potentiometer (either 10k or 100k) on the E72 PCB, and install the PCB on the launchpad. Board Schematic (page 3).

Download the following code (E72Lab5Template.c).   The code is not thouroughly commented, but you should be able to figure it out. Compile and run the code.

The code takes the input from the potentiometer on your microcontroller board which is connected to pin 1.3 and converts it with a 10 bit A/D converter.  You can vary the voltage by turning the pot and use a voltmeter to measure the voltage on the appropriate pin.  You can check the value of the conversion using by pausing the program and examining the value of the variables (you can't view the registers while the program is running).  Take several measurements (at least 10) as the voltage is varied and fill in a table like the one below (but with more rows):

 Measured Voltage Expected A/D Value Measured A/D Value

*IMPORTANT* Make sure you don't apply more than 3.3 (or Vcc, which should be 3.3V but may be slightly higher or lower) volts to any input on the microcontroller, this can destroy the chip.

### 3) PWM

We will be using SMCLK to control the PWM.  The code sets P1.4 to SMCLK.  Verify that it oscillates at 250 kHz when the code is running.

Another way to make a D/A convertor is more complicated conceptually, but only takes one pin on the output.  It is based on a technique called pulse width modulation or PWM.  Three pulse width modulated signals are shown below.  It also has some significant advantages in terms of efficieny in high power circuits and is commonly used in those applications. The top signal shows an output of 0.5, because the input is high 50% of the time.  The middle one shows an output of 0.1, and the bottom one is 0.9.  An output of 0.0 would be always low, 1.0 would be always high.  Note that the frequency doesn't change, and that the amplitude is unimportant; only the duty cycle matters.

The MSP430, fortunately, is set up to do this kind of modulation.  Run the code.  You can pause it and change the values of pwm1val and pwm2val (from 0 to 16).   Measure the PWM outputs on pins P2.2 and P2.4 and verify that it behaves as expected. Connect pin P2.2 to the circuit shown below with R=3.3kΩ and C=0.1μF.  Measure the duty cycle (the oscilloscopes in 310 will measure duty cycle for you) and the output voltage across the capacitor at enough points to verify that the output is approximately equal to the average value of the input.  Since the input is either 0 or 3.3 volts (or whatever Vcc is), a 50% duty cycle should give 1.65 volts, a 10% duty cycle should give 0.33 volts.  Make sure you understand how this works.  Get a screen shot of the input and output with a 50% duty cycle.  Measure, as accurately as you can, the magnitude of the ripple. The oscilloscope's peak-to-peak measurement will usually overestimate the error because it will pick the distance between the top point and the bottom point. It is more accurate to use the cursors (or magnify the signal and the screen and measure the number of divisions by eye) instead.

### PWM Dynamics:

Modify the code to alternately vary the duty cycle to produce a signal that goes between a duty cycle of 25% and a duty cycle of 75% roughly 100 times per second and measure the input and the output voltage.  Get a screen shot, and make sure you can explain it.

### PWM Accuracy:

Calculate a resistor value that gives an error of only 1 part in 128 when the duty cycle is 50% (in other words, the variation of the voltage around the average value is only 1/128th of that average).  Get a screen shot of the input and the output with a 50% duty cycle.  Note that the "peak-to-peak" measurement on the scope will tend to overestimate the value because it takes the highest and lowest values from the screen, so any noise peaks are included. Instead, blow the image up on the screen and use the cursors (or just measure the number of divisions and volts-per-division).

Set the scope channel on Vout to AC coupling and increase the scale until you can see the variations in Vout.  Record the measurement and get a screen shot.  This is the main reason that AC coupling is useful - observing small fluctuations on a large DC signal.

### PWM Dynamics:

Switch back to DC coupling and repeat the PWM dynamics with the frequency at which the duty cycle is changed is reduced enough that the circuit comes to (approximated) equilibrium between changes. You should observe a different time constant than you had before.

### PWM Fun:

Write a program that increases the duty cycle on PWM1 from 0 to 100% as you rotate the potentiometer, and decreases the duty cycle on PWM2 from 100 to 0%.  Now put the E72 board on the LaunchPad and observe the three color LED.

### To turn in:

Part 1) Simple D/A converter

• A table showing expected and measured value of the output voltage for each of the 8 possible inputs.
• Your well commented code for the program that you used to generate the screen shot.
• Your screen shot with the TLV2772, and an explanation. Consider the "High-level output voltage" and "Low-level output voltage" characteristics on the data sheet.
• Your screen shot with an LF411, and an explanation. Consider the "Output Voltage Swing" characteristic on the data sheet.
• Why did we use the TLV2772 in this situation?

Part 2) A/D

• a table showing your results (expected and measured)
• a graph showing the same

Part 3) PWM

• A screen shot of Vout with R=3.3kΩ and C=0.1μF and an explanation.
• Calculate the expected ripple and compare to measured.
• You well commented code.
• Your screen shot.
• Your explanation, including a quantitative measure of the speed of transition of the output when the duty cycle changes.
• PWM Accuracy:
• Your calculation of a resistor value (along with your reasoning).
• Screenshots of output, including a "detail" shot showing ripple in the output.
• Comparison of experimental and measured ripple.
• A screen shot showing dynamics, with an explanation of the difference between the dynamics of this circuit vs. those of the original circuit.
• PWM Fun:
• Your well commented code.
• Explain your observations.

Part 4) Code

• Add comments to the sample code (E72Lab5Template.c) to make operation of each part clearer (you don't need to comment the oscillator code in the block comment).  The commenting should be good enough that anybody in the class could follow it.   You may also want to include a document that describes the mode of operation of the various peripherals (so you can use block diagrams and images from the documentation (documentation is here (look in MSP430G2553 Family User's Guide and Device Specs)).

This will count as roughly 1/4 of grade.

Please ask questions if you have them - we will be building on this code throughout the semester.