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:
- Figure 1 from page 4 of the DAC0808 datasheet is a good place to start your design.
- The pins are labeled A1 through A8, but note that A1 is the Most Significant Bit, and A8 is the Least Significant Bit (the opposite of the normal convention). Ground the two least significant bits.
- The D/A convertor has an output current, instead of an output voltage. The output pin should stay at about 0 volts. The op-amp on the "Typical Application" on the datasheet converts the current to a voltage. How does it do this?
- The output current from pin 4 ranges between 0 (when the inputs are all 0) to Imax*255/256 when all the inputs are 1. The current, Imax, is determined by the current into pin 14 (which is at 0 volts). Note: Since we are using only 6 bits, the maximum value is Imax*252/256.
- You'll need to modify the circuit given in the datasheet to get a full scale range of 0 to 5 volts. Again, our output will be just under 5 volts (since we are only using 6 bits).
- The output of the D/A convertor takes some time to settle. You may need to take this in consideration when planning the timing of the A/D conversion in later sections of this lab. Check the DAC0808 datasheet for specs.
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"
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
Don't forget the pull-up resistor on the output of the comparator, as well as the necessary ground connection on the emitter. (311 datasheet). LM311 Pinout:
- Connect the output of the comparator to one of the Port B inputs and read it as you do the pushbutton. Refer to your original schematic: Port B pin 0 already has a pull-up resistor.
- Don't forget to set "TRISC" so that PORTC is outputs.
- A simple way to get 0-5 volts to use as your analog input is shown below (there are potentiometers at the bottom of the breadboard). The output of the pot will vary between 0 and 5 volts as you turn it.
To Turn in:
To include in your report for a section entitled "Successive Approximation A/D"
- Describe your design, and how you chose component values.
- Turn in a schematic of your circuit, with component values clearly marked.
- Describe how your program is intended to operate (i.e., your algorithm).
- Clearly describe your plan for verifying and documenting the proper operation of your design, and include well-documented code if pertinent.
- 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"
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"
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).
- Briefly hit the pushbutton, the LED on the daughterboard will flash, and then you can adjust the brightness of one of the green LED's with the potentiometer (the other green LED will be on at this time).
- Briefly hit the pushbutton, the LED on the daughterboard will flash, and then you can adjust the brightness of one of the blue LED's with the potentiometer (the other blue LED will be on at this time).
- Briefly hit the pushbutton, the LED on the daughterboard will flash, and then you can adjust the brightness of one of the red LED's with the potentiometer (the other red LED will be on at this time). The first red LED will be flashing as you adjust its duty cycle.
- Briefly hit the pushbutton. One of the LED's will be off, the other will have the color set according to the first three steps. You may adjust the color by repeating the process.
Figure out how the code works and what it does.
To Turn in:
To include in your report for a section entitled "Light Show"
| ← |
Comments or Questions? |
![]() |