CSE466 Lab 2: “Assembly and Pulse-Width-Modulaton”
Objectives
The goal of this lab is to introduce various features and modules of the
MSP430F2013 while introducing how to program the microcontroller using assembly. In this
lab you will learn the following:
- how to operate an
oscilloscope;
- how to program a timer
interrupt for Pulse Width Modulation output;
- how to change the brightness of a LED with PWM;
- how to use the A/D converter
and photoresistor to change the fade rate.
This lab assignment has 4 required sections. The deliverables section below outlines exactly what should be turned in.
Suggested Reading / Helpful Hints
- The
following sections of the MSP430 datasheet might be helpful for this lab
(listed in table of contents).
- Interrupts
- Timer A
- Analog
to Digital Converter
Suggested Steps
Section A: Interrupts in
Assembly
- Implement
your Part 4 from Lab 1 in assembly code and the examples in the folder you downloaded from here. Use examples from the Assembly (CCE) folder.
Use
msp430x20x3_ta_02.asm and msp430x20x3_sd16A_01.asm as your basis. You will have to determine how to configure CCS for assembly, and you may need to modify some parts of the code as given.
Question 1: a) How many lines of source are required for your assembler and C versions?
b) How large are the resulting binary modules?
Demonstrate your running code to Tom.
Section B: Oscilloscope
Tutorial
- Go through the entire Oscilloscope Tutorial (consult the Oscilloscope Manual as needed).
- Here is a reference on
bypass capacitors, which are used to reduce noise in your circuit: http://www.seattlerobotics.org/encoder/jun97/basics.html.
Question 2: What is
the purpose of the bypass capacitors? Why might they be useful for this lab?
Section C: PWM generation with Interrupt Timer A
- Implement a version of msp430x20x3_ta_16.c in a new project. Notice that this uses P1.2 pin which will require you to disconnect the photoresistor. Use the oscilloscope to verify the operation of the pulse width on P1.2 as you change the value of CCR1. Notice that your code no longer toggles the LED pin directly in the interrupt; instead the CCR1 comparator toggles the line in hardware.
- Next, change your LED connection to pin P1.2. Add code to enable the CCR0 interrupt, and add the appropriate timer vector code. This time, instead of toggling P1OUT, you should change the value loaded into CCR1 to ramp it up toward the value of CCR0 (which controls the period of the output). Upon reaching (CCR0-1) you should then begin decrementing the value loaded into CCR1 until it reaches 1. Then repeat the cycle. You will need to have a state variable to remember the direction, and another global variable to record the CCR1 value. By adjusting CCR0 and your clock divider you should be able to produce a smooth fade of your LED. Use the oscilloscope to verify that the operation is what you expect.
QUESTION 3: What is the advantage of direct hardware control of the PWM output, rather than changing the pin in an interrupt routine?
Demonstrate your smooth fade to Tom.
Section D: Add the
ADC
and Photoresistor
- Now, add the photoresistor back into the circuit. Since we've use the P1.2 pin for our LED, you'll need to consult the 2013 documentation for alternative ADC input pins.
- Re-create your ADC code, adding modifications to convert the voltage on the new pin that you've selected. A helpful example may be msp430x20x3_sd16A_03.c which shows multiple conversions of several inputs.
- Using appropriate scaling, cause your ADC value from the photoresistor to change the fade rate of your LED. This requires you to think about strategy: does your period change (CCR0), or does the increment vlue change (CCR1)?
Question 4: Is the LED affected by the period of the PWM output, or by the duty cycle (on time), or both? Can you run it too fast or too slowly?
Demonstrate your light-driven fader to Tom.
Deliverables
E-mail a single .zip file containing all required documents to cse466-tas@cs.washington.edu
For all files turned in, the comments at the top of the file
should contain:
- Your full name,
login and student number.
- The lab number and the part
of the lab (e.g. “Lab 2, Section A, Part 1” for your source).
Answers to the FOUR Lab Questions.
Commented Code for Sections A, C, and D.