CSEP567 Lab 6: “Pulse Width Modulation”

Objectives

The goal of this lab is to generate various colors using a tri-color LED. To select the color to be output we will create a virtual knob by measuring accelerometer readings through pulse width measurements. In addition, you will also use pulse width modulation to control the brightness of the LEDs. In this lab you will learn the following:

Important Warnings

Do NOT do either of these two things:

  1. The ADXL202EB (the accelerometer evaluation board used in the lab) is not reverse polarity protected. Do not reverse the +5V and ground terminals as it will damage the ADXL202EB and make the part unusable.
  2. Do not drop the ADXL202EB on a hard surface as it may generate several thousand g's of acceleration, enough to damage the accelerometer.

Hints

Suggested Reading

Resources

Brief introduction to AVR Programming
avr-gcc manual
Application notes section for the AVR 8-bit RISC family
Accelerometer(ADXL202)Datasheet
AccelerometerApplication Note on using the Duty Cycle Output
Tri-Color LED Datasheet Note: We will be using Common Anode LEDs.
Color Applets

Suggested Steps

PART 1:

  1. Obtain an accelerometer and cable.Wire your accelerometer module into your breadboard with the cable. The cable for your accelerometer module uses a black wire for ground, red for +5 volts, yellow for the Y-axis output, white wire for the X-axis output, and blue wire for the button (the other end of the button is connected to ground). Attach the Y-axis output to ICP1 (pin 20) and the blue wire to the INT1 pin to detect button presses. Remember to add a pull-up resistor to the button circuit. Plug the X-axis output into an unused row on your breadboard as we will use it later in the lab.
  2. Refer to page 9 of the accelerometer datasheet to determine what resistor to add to the accelerometer board at R1. R1 should be chosen such that the period of the accelerometer’s PWM period is approximately 4ms. Trim the leads to fit.
  3. Review important warnings (YES, AGAIN) and apply power to the circuit. Observe the y-axis output with an oscilloscope. Measure the period and min-max duty cycle.

    Complete Worksheet.

PART 2: (Test your code from Part 1 from the prelab)

  1. Implement a program that uses timer 1 input capture mode, (accelerometer y-axis output to ICP1 pin 20), to determine the positive duty cycle of the y-axis accelerometer. To do this, time the length of the positive pulse of your accelerometer (rising edge to falling edge) and the length of the period (rising edge to next rising edge). Use the 7-segment LED to display a "1" when the accelerometer has a duty cycle less than 50% and a "2" when the accelerometer has a duty cycle greater than or equal to 50%.
    NOTES:

PART 3: (Test your code from Part 2 from the prelab)

  1. New program: Define the x-axis of the accelerometer as an input on INT0 (pin 16).
  2. Use INT0 and Timer2 to create a manual interupt capture routine to determine the positive duty cycle of the x-axis.
  3. Implement a program that measures the x-axis and drives the 7-segment LED as in part 2. 

PART 4: (Integrate Parts 2 & 3 and HSV)

GOAL: To generate various colors on the tri-color LED using the accelerometer to select color and the potentiometer to adjust brightness. A user should be able to press the button and move the accelerometer until they find a color they want. On button release the H & S value should lock in. A user should be able to update the brightness of the color they choose by adjusting the potentiometer at any time.

  1. Use the provided HSV to RGB code to control the tri-color LED. Code
    H->x-axis
    S->y-axis
    V->ADC value of potentiometer (this controls brightness)
  2. To eliminate gitters make sure to average your values (H, S, & V).
  3. The color should only change while the user holds the button on the accelerometer down. Brightness should update anytime the value of the potentiometer is changed.
  4. Suggested timer uses:

Deliverables

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

  1. Demonstrate part 4 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.