CSE466-03au Lab Assignment 3   Revised 10/17/03 10:30 p.m.

See Revision to Part Four HERE.

Objectives

This is a 4 part lab. In it, you will:

  1. Reimplement your counter from the last lab, but this time implement it in C instead of in assembler. Compare the word sizes of your two versions and put the results in comments at the top of your C file.

  2. Reimplement the counter, but this time, instead of using a delay loop, use a timer interrupt to control the rate of count instead. The counter should also be able to start, stop, and change direction based on the buttons connected to pins 20 and 21. The left (high) decimal point should indicate whether the counter is running, and the right one should indicate whether the counter is counting up or down. Note that even when stopped, it should be able to switch between counting up and counting down.

  3. Read the analog value coming from the potentiometer and output the most significant 8-bits to the leds.

  4. Read the analog value coming from the photoresistor. The values may not swing all the way from 0 to 0xff, but you will calibrate it based on a very bright light source, and a very dark light source to make the output on the displays swing from 0.2 to 50, matching the Weston light meter supplied as a reference. One of your buttons should toggle between the raw ADC output displayed in hex (8 msbs), and the calibrated engineering units corresponding to the Weston light meter. The other button should allow you to start and stop the adc reading so that you can see just a snapshot of the adc values. The Weston light meter is kept in the lab office, on the bench.

In this lab you will learn the following:

Steps

  1. Rewrite the last lab in C. Try to mimic the behavior as much as possible since the idea is to compare the code size of your compiled assembly to the code size of what the C compiler generates. Confirm it works on your board, and then record the code size of the two hex files in the comments at the top of your C file. Both of these numbers will show up in the compiler output. For the assembler, it's listed as "Code" in the program memory usage. It's listed as ".text" in the gcc output.
    End of part 1

  2. If you haven't already done so, , add the pot, photoresistor and buttons to complete the circuit shown in the schematic on your breadboard. Again, before powering your circuit back up, you should confirm that you don't have any shorts between Vcc and ground.

  3. Dump the loop delay routine and replace it with interrupts from timer0. Documentation on writing an interrupt handler is available at http://savannah.nongnu.org/download/avr-libc/doc/avr-libc-user-manual/. From that link, go to the modules section and then the Interrupts and Signals entry. Remember, when writing the interrupt handler routine, you'll want to minimize the amount of code in it. Interrupt handlers are meant to be very fast, and do only the work that's critical to do right that microsecond. There is no way that anyone will be able to notice if your led updates even a few milliseconds late, so the led update code should not be in the handler. The code for the example setup only has a single line in the handler that compiles to 3 assembly instructions (no, it's not a function call). Documentation on timer0 including all the related registers is available in the datasheet (coursepak).

  4. Write code so that pressing the stop-start button halts and resumes counting. It would probably be a good first step to just get the switch debounced (see the hint on debouncing) and get it to turn the decimal point on the high display on and off. You can either use timer2 for debouncing or figure out a way to use timer0 for both the count and debouncing. Remember that if you push the button and hold it for a long time, it should still only register as a single push.

  5. Write code so that pressing the up-down button changes the direction of counting. The decimal point on the low display should be lit when counting upwards. Note that even when the count is stopped, the system should respond to up-down button presses and the decimal point should still update.
    End of part 2

  6. Write the code to read the analog value from the potentiometer. This will require reading the section in the datasheet on the A/D converter. You'll want to use the interrupts to tell you when the conversion is complete. Also note that the A/D has 10-bit resolution, but you only need 8 for the display, so you can just throw away the 2 least significant bits. Note that there's a nice way to do this shown in the datasheet.
    End of part 3

    See Revision to Part Four HERE.
  7. Read all 10-bits from the photoresistor, but since you only have 2 hex displays, still output just the 8 most significant bits to the display. At this point, you'll want to figure out whether brighter light or dimmer light gives you a higher analog value.

  8. Add a display routine that uses the calibration values so that when you calibrate it with a bright light, and darkness, and then shine a medium light on it, it should register 0.2 and 50 (in decimal), calibrated to the Weston light meter we have supplied. This will require you to use the bright and dark values as endpoints and interpolate between them for intermediate values, showing 2 significant digits. See the hint on interpolating. You must convert your values to engineering units of "light" as measured by the Weston meter. The meter works on a logarithmic scale, as opposed to the linear scale output by the ADC. For a discussion of the Weston meter's dial, see http://www.johndesq.com/pinhole/westonscales.htm . The meter is calibrated in foot-Lamberts.
    End of part 4

Helpful Hints

Deliverables

For all files turned in, the comments at the top of the file should contain:

Part 1:

  1. Turn in a hard copy of your code

Part 2:

  1. Demonstrate your counter to a TA. You can either do this during this lab, or during the first 1/2 hour of the next lab.
  2. Turn in hardcopy of your commented C code.

Part 3:

  1. Demonstrate your potentiometer reader to a TA. You can either do this during this lab, or during the first 1/2 hour of the next lab.

Part 4:

  1. Demonstrate your light meter to a TA.
    2.Turn in a hardcopy of your C code