CSE466 Lab 2 - Section D (Extra Credit): “Light Meter”
NOTE: If completed, this Extra Credit
section is worth an additional 20% of the total possible points for your Lab2
grade. This Extra Credit section has an ESTIMATED completion time of
approximately 2-3 hours.
Objective:
Create a dynamically calibrated light meter by reading the
analog value coming from the photoresistor. The values may not swing all the
way from 0 to 0xff, but you will calibrate it to match the light scale listed
in Step 2. One of your buttons should toggle between the raw ADC output
displayed in hex (8 MSBs), and the scaled Weston light meter units. The other
button should toggle you through a state machine that allows you to calibrate a
minimum light level and a maximum light level.
In this section you will learn the following:
- how to use the A/D converter
including using the A/D complete interrupt
- how to scale data without
using floating point math
Helpful Hints
- When interpolating, you
usually use floating-point arithmetic to calculate intermediate values.
Unfortunately, the ATmega16 does not have hardware support for floating
point operations. gcc does provide software emulation of floating point,
but it's extremely inefficient on this chip (1000+ cycles for a 16-bit
divide). Because of this, you are not allowed to use floating point on
this assignment. However, you can use fixed-point arithmetic since the
numbers coming from the ADC are 10 bits wide, and there are 16-bit
integers, you can left shift the values to get up to 6 bits of fractional
precision. Remember that the Weston meter works on a logarithmic scale, as
opposed to the linear scale output by the ADC. A look-up table might be a
good idea here. You'll need more precision on one end of the scale than on
the other.
Scaling should be simple:
ADC value - Min
Value Index of output value
------------------------------- = ------------------------------
Range (Max - Min)
Size of Light Scale
Do not use floating point math!
Suggested Steps:
1. Build
a calibration state machine using two buttons.
Button 1 will be used for the calibration sequence.
- The first button push displays "LO" in hex
display to indicate the system is ready to calibrate the low light point.
- The second button push captures the low calibration
point, and displays "HI", indicating it’s ready to capture the
high calibration point.
- The third push of the button captures the high
calibration setting, and switches to calibrated display mode.
Button 2 toggles between raw data and calibrated/scaled
data.
2. Implement a lookup
table with the following values to be displayed on your 7-segment LED display.
The values in this table should the only values displayed when in calibrated/scaled
data. Values of the calibrated scale are:
0.1
Min. value
0.2
0.3
0.4
0.6
0.8
1.2
1.6
2.4
3.2
4.9
6.5
9.5
13
19
25
38
50
75
99
Max. value
3. Write a function
to scale the ADC data bounded by the calibrated range (Step 1) to the table of
values in Step 2. From the maximum and minimum values obtained during the
calibration (Step 1) a range of reading values is established (possible
examples are 5-200, 51-98, 110-114, 215-255, etc). A conversion should
automatically occur to convert the raw ADC values and output the calibrated
values in Step 2 based on the calibration information collected in Step
1. Here are the specifics:
- The
minimum value obtained during the calibration sequence should correspond
to the minimum of the Weston light scale, which is 0.1.
- The
maximum value obtained during the calibration sequence should correspond
to the minimum of the Weston light scale, which is 99.
- Any
value between the minimum and maximum should be scaled so that it
corresponds to one of the 20 values listed. (See hint)
- If the
raw ADC value goes higher than the maximum established in Step 1, it
should still only display the max value. The values should also not go
below the minimum.
4. If values are
noisy, average 8 or 16 readings.
Deliverables:
- Demonstrate
your light meter to a TA.
- The user should be able to calibrate the light
meter using the method listed in Step1. The user should then be able to
toggle between raw hex values output by the ADC and the calibrated
display based on the light values obtained during the calibration
sequence. The values that should be displayed are listed in Step 2.
- The values should not roll over (overflow) when the
meter saturates.
- Turn
in a hardcopy of your C code
- Do
not use floating point arithmetic anywhere in your code.
- Keep
your interrupt handlers small.