Unformatted text preview: ME 461 Laboratory #1
Digital I/O Goals: 1.
4. Learn the basics of digital input and output. Use switches as digital inputs with interrupts. Use LEDs as digital outputs. Control a traffic light at an intersection. NOTE: When configuring the port pins for digital I/O, follow this progression. Inputs: 1. Set PxSEL. 2. Set PxDIR. 3. Set PxREN (if pull‐up/down resistor is required). 4. Set PxOUT (if pull‐up/down resistor is enabled). Note: items 5‐7 are required only when pin interrupts are desired. 5. Set PxIES. 6. Clear PxIFG. 7. Set PxIE. Outputs: 1. Set PxSEL. 2. Set PxDIR. 3. Set PxOUT. Also, when setting register contents, use bitwise AND & OR assignments to ensure only the intended bits are modified. See the document “C Review” on the course website for more information. Exercise 1: (30 points) Write a function to return the state of two of the switches you wired in Lab 0. Your function should be passed no parameters, and it should return a char between 0‐3 that represents each of the four possible states. You will need to configure a number of digital I/O control registers (e.g., PxSEL and PxDIR). You will also need to enable the pull‐up/down resistors on the input pins (using PxREN and PxOUT) you wired to the switches (Why?). Note that the things you learned by completing the Prelab assignment should significantly help you to complete this exercise. As with the Prelab assignment, you will likely find the lecture notes (especially Lecture #2 slides 26‐29 and 35‐36), MSP430 User’s Guide (especially the section about Digital I/O) and the MSP430 2272 Datasheet particularly helpful. Also remember that many of the control register names, along with things such as predefined constants with (relatively) easy to remember names, are declared in the “msp430x22x2.h” header file. You are strongly encouraged to become familiar with the contents of this file. You should be able to tell from the diagrams below which type (pull‐up or pull‐down) you need. Pay close attention to the location of the switch in each diagram and where (electrically) you soldered the switch to your board. Pull‐up and pull‐down resistor configurations for a switch. Call the function inside your while loop in the main function every 0.5s. Blink a different LED for each state at a rate of 1Hz (on‐off takes 1s). Don’t forget to configure the pins connected to the LEDs as digital outputs. Hint: this is the default rate in the project creator. Also, print the switch state to the serial port as discussed in Lab 0. Demonstrate your working application to the TA. Exercise 2: (30 points) Now, add a port 2 interrupt service routine to your program by copying the code below and pasting it into your program at the function definition (global) level. // Port 2 interrupt service routine
__interrupt void Port_2(void)
} This code was taken from one of the TI example C files found on the course website. These examples are a great resource for completing lab exercises. Use the four switches wired to port 2 pins to trigger hardware interrupts. You will have to enable the corresponding pin interrupts using the P2IE register and select the trigger edge using the P2IES register. Two of your switches should trigger interrupts on a high‐low edge, and two should trigger interrupts on a low‐high edge. Inside the interrupt service routine, check to see which switch triggered the interrupt by polling the P2IFG register and increment one of four counters depending on which switch was pressed. Don’t forget to clear the interrupt flag for the pin that triggered the interrupt. Lecture #2 slides 44‐51, the MSP MSP430 User’s Guide (especially the section about Digital I/O) and the MSP430 2272 Datasheet will be useful for this exercise. Print the values of these four counters to the terminal window and demonstrate the application to your TA. Exercise 3: (40 points) If you’re an ambitious control engineer (or you’ve driven down Prospect Ave. in rush hour), you may have thought to yourself that you could design a better traffic light controller. Here’s your chance. Configure the 8 pins connected to the LEDs as digital outputs, and the 4 pins connected to the switches as digital inputs. You should enable interrupts on the pins connected to the switches. Use counters as you did in the previous exercise to keep track of switch presses. Sw. 4 LED 5-8 Sw. 3 Your job is to use the four switches and your microcontroller to control the traffic light LEDs. See the schematic below. LED 1-4 Sw. 2 Sw. 1 Intersection Schematic The control logic should follow your basic knowledge of how traffic lights work. As a general framework, the following rules should be satisfied. •
• Cars waiting at the stoplight for some time should trigger the opposite light to turn yellow, then red. You should not have to hold down the switch to indicate a car is waiting. You may indicate more cars waiting by pressing the switch multiple times. If cars are waiting in the turn lane (i.e. the “turn” switch has been pressed), the corresponding turn signal LED should be illuminated for some time as soon as the corresponding “ahead” light turns green. In the real world, the light does not change right away when a car rolls up to an intersection. More presses of the stopped direction’s switch should cause the light to change faster (within some limit). More presses of the driving direction’s switch should prolong the duration of the green light (within some limit). To control timing, operate on the assumption that the Timer A interrupt service routine is called every millisecond. You will learn more about timers before and during the next lab, but for now you may use it to increment counters that keep track of time. Speed up the general time scale of the stoplight so that the demo doesn’t take 5 minutes. You will find lecture #2 slides 57‐60 most useful. Show your working application to your TA. ...
View Full Document
- Spring '08
- TA, digital i/o, interrupt service routine