E72 Lab #5

D/A & A/D 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.

Note: I haven't had time to fully check all of the code supplied with this lab.  If you find (or suspect) errors, let me know.  Also, I have used some C syntax we did not cover in class; let me know if any of the code is not clear.


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-5 volts would have the following outputs. Note that the highest output is actually 5V*(2N-1)/2N (where N is the number of bits, 3 in this case). A binary value of 110 (decimal=6) would result in 3.75 Volts.

Input

D/A Output

Decimal 

Binary 

0

000

0 V

1

001

0.625 V

2

010

1.25 V

3

011

1.875 V

4

100

2.5 V

5

101

3.125 V

6

110

3.75 V

7

111

4.375 V

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

A/D Input

Output

Decimal 

Binary 

<0.625 V

0

000

0.625-1.25V

1

001

1.25-1.875V

2

010

1.875-2.5V

3

011

2.5V-3.125V

4

100

3.125-3.75

5

101

3.75-4.375

6

110

>4.375 V

7

111

1) Build a simple parallel D/A convertor

For this part of the lab, use your daughterboard on a breadboard.

The first task this week is to build a simple D/A convertor and evaluate its performance.  You will be using a DAC0808 (DAC0808 datasheet).  The chip may be labeled 1408 or DAC0806.  Your design should take 6 bits from the microcontroller (use the lower 6 bits on Port C of the PIC18F2620 as the input to the D/A convertor).  The D/A output should range from 0 to 5 volts.  The lower 6 bits from the PIC should go into the upper 6 bits from the DAC0808.  Note: I expect you to try to get the device to work by reading the datasheet and trying to understand it, if you have trouble you should come see me, but I expect you to have fairly specific questions that indicate you have put some thought into the problem.

Verify and document that your design works as you expect.

Measure how long it takes the D/A output to settle when it goes from about 1 to about 4 volts.

Things to consider with the design:

Things to consider with testing:

You will need to be able to write a static value to the microcontroller.  You can do this with a program, or you can manually change the value on PORTC my manipulating the appropriate SFRs (Special Function Registers; go to View→SFR) - PORTC and TRISC.   However, before you use MPLab to write to the registers you need to download a project to it - you can use the project from last week's lab, or make a new one.

To do dynamic testing it will be easiest to write a program to change the value of PORTC to a particular value (1V) wait a bit, change it to another value (4V) wait a bit, and then go back to the first value...

Don't disconnect your D/A, you will need it for the next section.

To Turn in:

To include in your report for a section entitled "Parallel D/A"

  1. Submit a copy of your schematic and any design calculations.
  2. Verify operation of your circuit by generating several numbers from the microcontroller, sending them to the DAC by connecting PortC to the DAC, and measuring the resulting voltage.  Compare these with your predictions.
  3. To measure the speed of your D/A, change the program so that the output of the D/A alternately changes between 1 volt and 4 volts.  Use the oscilloscope to measure the D/A output during the transition.  If it is linear, measure the slew rate.  If it is first order, measure the time constant. If second order, or higher, estimate the settling time (to within 5% of the final value).  Any overshoot?  How long until equilibrium is reached?  The delay between successive values should be long enough to allow the D/A output to equilibrate.  Also include your code.
  4. Set the DAC to output from 0 to 60 in 10 equal steps and then repeat.  Get a scope trace showing  the resulting stairstep pattern.

Don't disconnect your D/A, you will need it for the next section.


2) Build an A/D convertor using your D/A convertor.

For this part of the lab, use your daughterboard on a breadboard.

A typical A/D convertor is shown below.  Analog voltages are shown in red, digital voltages in blue.  In operation, the microcontroller generates a binary number (Digital (OUT)), that gets sent to the D/A convertor.  The D/A convertor generates an analog voltage, and this is compared to the unknown input voltage.  If the output of the D/A is higher than the unknown voltage, the microcontroller makes the D/A convertor's output lower.   If the output of the D/A is too low, the D/A output is increased.  This process goes through several iterations until the output of the D/A is very close to that of the unknown voltage. 

There are several ways to do this.  The simplest is to start the microcontroller with an output of all zeros, which gives a zero output from the D/A convertor.  The microcontroller then increases the digital output, and as soon as the comparator changes states, we know that the output of the D/A convertor is approximately equal to (but slightly above) the unknown voltage.  The microcontroller's output is now the binary (i.e. digital) representation of the analog input voltage.  However, this scheme is inefficient.  For an 6  bit convertor, the microcontroller might have to try 64 different values before finding the correct one. 

A better way to accomplish the goal of performing an A/D conversion is to use the same block diagram, but to implement a binary search, or divide-and-conquer, algorithm.  The first value that the microcontroller would try is halfway between the minimum and maximum value.  In the case of an 8 bit convertor, the first guess would be 128.  If that yielded a voltage from the D/A that was higher than the input voltage, the next guess would be 64 (=128-64) .  If it was too low, the next guess would be 192 (=128+64).  After the next iteration, 32  would be added or subtracted, then 16... This method requires at most 8 iterations to get to the correct value.  This type of A/D convertor is called a successive approximation A/D convertor.

Your task is to build a 6 bit A/D convertor using successive approximation.  Your A/D convertor should have a full scale range of 0 to (very nearly) 5 volts.  Since you can't display numbers, you'll have to put a breakpoint in the program and examine the value of the A/D conversion stored in a variable.   Verify and document quantitatively that your design works as you expect.  

Things to consider

To Turn in:

To include in your report for a section entitled "Successive Approximation A/D"

  1. Describe your design, and how you chose component values.
  2. Turn in a schematic of your circuit, with component values clearly marked.
  3. Describe how your program is intended to operate (i.e., your algorithm).
  4. Clearly describe your plan for verifying and documenting the proper operation of your design, and include well-documented code if pertinent.
  5. Neatly and clearly present quantitative evidence that verifies and documents your circuit's operation.

3) Using the PIC's A/D convertor

For this part of the lab, use your daughterboard on a breadboard.

Fortunately, it is not always so difficult to build and use an A/D convertor because the PIC has a built-in 10 bit A/D convertor.  Enter and run the following code (pic_a_d.c).

Verify and document that the convertor works as expected.  It takes the input from pin RA0/AN0 which is connected to the potentiometer on your microcontroller board,.  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 breakpoints, or simply by stopping the program and examining the value of the variables.

Make sure you don't apply more than 5 volts to any input on the PIC, this can destroy the chip.

To Turn in:

To include in your report for a section entitled "PIC A/D"

  1. Neatly and clearly present quantitative evidence that verifies and documents your circuit's operation, showing both expected and measured results.  A graph (along with a table showing data) is one good method for doing this.

4) A one pin D/A convertor

For this part of the lab, use your daughterboard on a breadboard.

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 PIC, fortunately, is set up to do this kind of modulation.  Run the following code (in the file pic_pwm.c; you'll need to add appropriate header files...).

Connect pin C2 to the circuit shown below.  Measure the duty cycle (the oscilloscopes in 310 will measure duty cycle automatically) and the output voltage across the capacitor at enough points to verify that the output is equal to the average value of the input.  Since the input is either 0 or 5 volts, a 50% duty cycle should give 2.5 volts, a 10% duty cycle should give 0.1 volts.  Make sure you understand how this works.

Modify the code to alternately vary the duty cycle to produce a signal that goes between 1 and 4 volts approximately 5 times per second and measure the output voltage.  Measure the speed at which the output (Vout) changes.  If the change is linear measure its slew rate.  If it is first order, measure its time constant. Get a screen shot of the large time scale changes in Vout (as it goes from 1 to 4 volts) and the short time scale variations (just as the voltage starts to increase).  It may be difficult to trigger the scope to observe the latter unless you generate a large output that occurs at the same time that you can use as a trigger.  See me if you are having trouble with this.

Change the resistor to 330Ω and observe and record the output at both small and large time scales. 

To Turn in:

To include in your report for a section entitled "PWM"

  1. Show why the output of the RC circuit should be the average of the input signal.  Use the fact that the time constant is much longer than the period of oscillation of the PWM signal.  You could also use a Fourier Analysis (note: you only need the zero frequency component of the signal -- why?).
  2. Neatly and clearly present quantitative evidence that verifies and documents your circuit's operation, showing both expected and measured results.  A graph (along with a table showing data) of input values and expected and observed output values is one good method for doing this.
  3. How fast does your output change when you change the duty cycle?  Include a quantitative measure.  Is this what you expected?  Include screen shots of small time scale and large time scale behavior.
  4. Explain, qualitatively, the changes seen in the signal (at small and large time scales) when the resistance was changed to 330Ω.

 

5) Software PWM

For this part of the lab, use your daughterboard on the motherboard with your LED board also attached.

Run the following code (in the file LEDpwm.c).

Figure out how the code works and what it does.

To Turn in:

To include in your report for a section entitled "Light Show"

  1. Submit your code, describe how it works (i.e., comment the code), and include your observations of the PIC's output (i.e., the LED's).
  2. Why does the red LED flash as you set its brightness?
  3. The Resistor and Capacitor acted as a low pass filter to determine the average value of the input signal in your first PWM experiments.  When the PWM was connected to the LED it made it appear dimmer or brighter though the LED was either fully lit or fully dark. What was performing the low pass filtering in this case?

Comments or Questions?

Erik Cheever
Engineering Department
Swarthmore College