Lab 10: ADC, DAC Experiments
This lab has you explore the on-chip ADC, a SPI DAC, and a temperature sensor.
Prelab (20)
Gather parts: multimeter, potentiometer, MAX548A DAC (8 pin IC), LM60CIZ temperature sensor (3 pins; looks like a transistor), 1 uF cap, wires and/or jumpers.
Connect the DAC and potentiometer as shown below. You may use jumper wires if you wish: This is the last lab, and you have a test this week, so do a sloppy job if you want, as long as everything works. The datasheet for the MAX548A has the pin assignments for that part.
Use a long wire or a jumper (if you have one) to connect the potentiometer wiper to AN0, since you'll have to remove and replace this connection several times during the lab.
3. The pins of the LM60 are shown below. Connect VDD to 3.3V and GND to ground. If you reverse VDD and GND, the LM60 will get very warm because it is drawing too much current and you may damage it. Don't connect the middle pin, VOUT, to anything yet. Just insert it into a free row of your breadboard.
4. Open the chap11/adc_spidac_test project and save adc_spidac_test.c as my_adc_spidac_test.c. Exclude the original C file from the project and add yours. Build the project and upload to your PIC. When you run it, you should see something like this in BullyCPP:
"ADC in" voltage will depend on the potentiometer setting.
5. Turn your potentiometer as far as it will go in both directions. At one extreme you should see 0 V for "ADC In". At the other you should see about 3.3 V.
6. Unhook the wire connecting to the potentiometer wiper. (Leave the other end connected to AN0.) Connect this to VOUT of the LM60. BullyCPP should still be showing an image similar to the above screenshot, with an ADC In of something like 0.578 V.
Prelab Checkoff
When the LM60 is connected to AN0, BullyCPP has output that looks like the above screenshot. When you squeeze the LM60, ADC in changes values by more than a few thousandths of a volt. (10)
When AN0 is connected to the middle pin of the potentiometer, BullyCPP has output that looks like the above screenshot. When you adjust the potentiometer, ADC In changes values by more than a few thousandths of a volt. (10)
Task 1 - ADC to DAC Test (20)
Connect AN0 to the potentiometer. The adc_spidac_test project reads a 12-bit value from the PIC ADC input, whose voltage is set by the potentiometer, converts this to an 8-bit value by dropping the last four bits, then writes this to the MAX548A DAC using the SPI port. This means that the DAC output voltage follows the potentiometer voltage (POTV) as you adjust the potentiometer. Make a table like the following:
Take measurements to fill in the table. If you like, you can use the meter on VirtualBench so you don't have to move your multimeter probes back and forth. If the values in Bully oscillate too quickly to read even when you're not fiddling with the pot, you have two options:
A. Connect a 0.1 or 1uF cap between AN0 and ground.
B. Take your voltage readings with your meters (which shouldn't change as much), turn off your board, read the line of Bully output, then turn your board back on.
For the final row (VDD), just turn the potentiometer as far as it will go and take your measurements. VDD may not be exactly 3.3 V.
For the other rows, you don't need to get ADC In to match POTV exactly. There will be no grade deduction as long as you are within +/- 5 mV.
Percent error is calculated as follows: (((DAC Out - ADC In) / ADC In )) x 100%. Use the ADC In voltage measurement from Bully, not the hex value.
TA Checkoff
You've filled out a table with your measurements and calculated your percent error.
Explanation
If you are confused by what you just measured, or would like help on Task 2, consult the FAQ.
Task 2 – LM60 Temperature Sensor (40)
The LM60 output increases by 6.25 mV for every degree Celsius increase in temperature, with a DC offset of 424 mV. Therefore, 25 C produces a voltage of (6.25 mV/C * 25 C) + 424 mV = 580.25 mV.
To convert Celsius to Fahrenheit, multiply degrees Celsius by 9/5 then add 32.
Modify my_adc_spidac_test.c to print temperature in degrees Fahrenheit (with two digits to the right of the decimal point) between ADC In and DAC Out. The output should look like this:
Hints:
Be sure you are doing floating point math properly. If you do this: (9/5), the compiler will divide two ints, assume you want an int result, and give you an answer of 1.
In the printf statement, if you don't cast your result as a double, you'll get a warning from the compiler like: my_adc_spidac_test.c:86:12: warning: format expects type 'double', but argument 4 has type 'float' But you can safely ignore this.
Once you have done this, record the temperature of the LM60. Now squeeze it between your fingers for a minute or so and try to get it up to about 90 F. Record the highest temperature you get.
Round the low temperature down to the nearest integer and subtract 2. Round the high temperature up to the nearest integer and add 2. Record these new values. Find the range between them by subtracting.
Our goal is to map the temperature values to DAC output voltages such that the midpoint temperature is represented as ½ VREF, or 1.65 V if VREF = 3.3 V. This way, we will get a wider voltage swing for this limited temperature range and our PIC will act like a digital amplifier.
Now, modify your program to map the temperature that you calculate as a percentage of this range:
Percentage = (measured temp – new low temp) / range
Note that if the input temperature is the midpoint, this calculates as 0.5.
Convert this to a voltage between 0 and 3.3 V, using the formula:
(Desired DAC Voltage) = 3.3 V * percentage.
Multiply by 256 (2^8), to get the 8-bit hex code to write to the DAC so that it outputs the desired DAC voltage.
Modify the print statement in your loop to print the desired DAC voltage and its hex code, as shown below:
Use a voltmeter on the DAC output to check that your onscreen value for DAC output approximately matches what is on the meter. Capture a screenshot of your Bully output, showing a range of values, including your midpoint temperature.
Distance students only: Take a photo showing your meter measuring DAC output next to your BullyCPP output. The DAC output in Bully should approximately match what is being shown on the meter.
TA Checkoff
When you force the temperature sensor to approximately the midpoint temperature, the DAC output voltage is at the midpoint voltage (about 1.65 V) – have a voltmeter hooked up for the demo.
Congratulations, you have completed the last lab. You won't need your breadboard anymore*: Display it proudly on the wall of your apartment, give it as a gift to someone you are trying to impress, or bring it to your next job interview.
Report
Your report must have a title page.
You must textually describe what all your images are showing the reader, like this webpage does. E.g. "Below is a state diagram, showing the states used when implementing my problem." or "Above: Screenshot showing measurement of falling edge time of button press."
Do not invite the grader to inspect screenshots to find particular values, e.g. "Rise time shown below". Rather, state the value in your report. "The rise time was 280 ns as seen below."
Distance students only: Label the first section Prelab. Include a photo of your board with your NetID/name visible. You will not be graded on neatness, but ensure that your photo shows all the components and connections used in this lab. You may take multiple photos if you need to.
Label the first section Task 1. Describe Task 1 so that an outside reader could understand what you measured. Include your table with your measurements. Do not handwrite your table or the values in your table, or you will lose full credit for this task (20 pts).
Verify manually that the ADC and DAC codes in the 1.6 V table row equal approximately 1.6V, using your measured VDD as the reference voltage. For example, I got 0x07c2 for ADC In. Is that supposed to be ~1.6 V? Or am I way off? Do the same for "To Dac". Show your calculations. See Lab 6 for adequate calculations. You do not need to show how you convert hexadecimal numbers into decimal.
Label the second section Task 2. Describe Task 2 so that an outside reader could understand what you did and why this would be useful. Mention the high, low, and midpoint temperatures you used. Include your BullyCPP screenshot. Distance students only: Include your photo of meter and BullyCPP.
Submit your my_adc_spidac_test.c file
Remove the default boilerplate header above the asterisks.
Be sure the file description atop the file is correct.
Add a header of your own including name, course number, lab number, lab section number, date, and filename.
Be sure the indentation and spacing is consistent and neat.
Comment your code, labelling the portions you added and explaining what each line does.
Make sure all existing comments are applicable to the code you are submitting.
If you notice you've made mistakes but the program still somehow runs, fix them.
Build your code and run your program again to make sure they still work right.
Grading
If you do not successfully complete a task and your report fails to mention or glosses over this fact, you will receive a zero for the report portion of your grade as well as for the task. If you attempt to deceive the grader, e.g. by including screenshots not generated by the code you submitted, your instructor will be notified and you will be recommended for an Honor Code violation.
The report is worth 20 points for neatly and coherently presenting your information to a reader. The following non-exhaustive list of errors will result in losing credit from the report portion of the lab grade:
bad screenshots (not cropped properly, blurry, too small)
text is "lab jargon" unintelligible to an outside reader ("I connected my DAC to the board" "Here is the screenshots of my bully.")
text is phrased as instructions to a second party ("Take current measurements.")
text is copy-pasted from the lab writeup rather than using your own words
text is a literal recap of the activities you performed rather than communicating your results and understanding ("First I connected pin 17 to ground. Then I also connected pin 3 to ground. Then I connected pin 2 to VDD. Then I uploaded the project to my board...")
blatantly erroneous text ("The ADC converts digital signals to analog.")
garbled / confusing / gibberish text ("Sure, I can help you with that! In electronic engineering, an ADC or analog-to-digital converter is used to transform analog signals into digital, or binary, encoding. Typically...")
using vague or incorrect terminology ("The ADC (Artificial Device Controller)...")
excessive text
careless use of pagebreaks that leave blank pages, 1-2 lines of text on a page, etc.
general unprofessional appearance
code is untidy
code is missing header and/or contains default header
code has inaccurate/inadequate comments
The tasks are worth 60 points. If your report indicates that you did not successfully complete or do not understand a task, you will lose credit, even if you performed it during the lab. The same is true for tasks performed during the prelab. There are two tasks. The first task is worth 20 points. The second task is worth 40 points. The following non-exhaustive list of errors will result in losing credit from a task:
missing screenshot/table/text/calculations
screenshot of the wrong thing
screenshot shows incorrect results
cannot read screenshot
table is unruly
calculations are wrong
inadequate calculations
code won't build
code performs incorrectly
code is convoluted
code uses incorrect methodology; doesn't follow lab directions.
Lab reports that flagrantly violate submission policy (wrong lab, no screenshots, no title page, no text besides headings/labels, mostly blank, code pasted into pdf, paragraphs of lab text pasted in, extremely sloppy/unprofessional, missing code etc.) will not be accepted. The student will receive a zero for the lab and may resubmit with late penalty.
* Unless you have a grade of D or lower and/or are planning on becoming a TA. In that case: click here.