EE 357 Lab 4 Stopwatch 1 Introduction You will work in teams of one or two to write C code for your Coldfire board to implement the functionality of a stopwatch. You will plug your microcontroller board into the Tower system along with a pre-built protoboard with 3 sevensegment displays. This project, though well-defined, will not provide a large amount of pre-written code. It will be up to your creative and design abilities to complete the project. 2 What you will learn This lab will help you integrate the embedded coding concepts and different I/O functionality into a single project. You will need to integrate the concepts of timers, polling and GP I/O to arrive at a working solution. You may pull in code from the basic demo lab/projects that we performed earlier. 3 Background Information and Notes Stopwatch Application This project will implement a stopwatch application that counts upwards in increments of tenths of seconds from 00.0 to 59.9 seconds. It will provide the ability to start, stop, and reset (back to 00.0) the stopwatch. It should also implement a lap feature which freezes the displayed time at the instant the lap button is pressed while still keeping the internal watch time incrementing. When lap is toggled again (or start is pressed again) the internal watch time (which has been running) should be re-displayed and then continue as normal. Tower Prototyping Board This project requires more and different I/O than is present on the MCF52259 board (more than the 4 LED s, 2 push buttons, etc.). To provide the ability to prototype larger projects, we will utilize the Tower connectors. The elevators on the Tower (the sides of the tower where your board plugs into) take signals from your MCU and distribute them to other boards that plug into the tower. You will develop your code using specific pins defined later in the documentation, plug your individual microcontroller board into the development board, and test/debug your program. The stopwatch application requires 3 decimal-digit displays (tens, ones, and tenths of seconds). The prototyping board contains three 7-segment digit displays as shown in Figure 1. To save I/O pins, we will multiplex our data values to each display. We will use a separate 74LS273 8-bit register to control each 7-segment display. By using a separate I/O pin from the MCU to generate a clock strobe to each register we can multiplex these 8 data bit inputs to each register using just PORTTH[7:0]. Last Revised: 4/4/2012 1
74LS273_Tens A Q1 H Q8 ENB 74LS273_Ones A Q1 PORTTH[7:0] 8 H Q8 MCF52259 ENB PORTTG[0] PORTTG[1] PORTTG[2] 74LS273_Tenths A Q1 H Q8 ENB 7-Segment Digit GPIO Port(s) Ten Second Digit [7:0] Data in PORTTH then strobe PORTG[0] One Second Digit [7:0] Data in PORTTH then strobe PORTG[1] Tenth Second Digit [7:0] Data in PORTTH then strobe PORTG[2] Table 1 CLOCK strobe mappings a f g b dp g f e d c b a e c PORTTH 7 6 5 4 3 2 1 0 d dp Figure 1-7 Segment Display connections. Data in PORTTH is then transferred to separate 8-bit registers. The mapping of PORTTH bits to the 7-segments is shown above. 1=ON / 0=OFF. As an example: If we took the value 0x06, wrote it to PORTTH, then turned on PORTG[2], then turned off PORTG[2], the digit 1 would appear on the tenths digit. Note: You can also have the decimal point appear by setting the MSB of PORTTH. 2 Last Revised: 4/4/2012
Other I/O As input to your stopwatch you will use the two push-buttons on your microcontroller board (i.e. the same as those used in CF Lab 1). SW1 will serve as your start/stop button. When pressed it shall simply toggle the state unless in lap mode (more to come ). SW3 will serve as the lap/reset button. When the stopwatch is started/running, a press of SW3 will serve as the lap feature. Any subsequent press of either SW1 or SW3 will toggle the lap feature to return to running display of the current time. When the stopwatch is stopped, a press of SW3 will reset the time to 00.0. Suggested Approach It will likely be beneficial to keep a state or mode variable tracking what mode your stopwatch is in. Presses of the button will cause updates/transitions between modes and states as shown in Figure 2. What should be done in each state (whether the time should be incremented and what should be displayed should be fairly intuitive). You may also find it beneficial to keep an internal representation of the time (separate variables for each digit) and then only update the display I/O every tenth of a second (using some kind of timer interrupt). Stopped/Reset SW3 SW1 SW1 Lap SW3 Running / Started SW1 or SW3 Figure 2 - State Transition Diagram for the Stopwatch In terms of overall structure, there are many ways to organize your program. One suggested approach is to sit in a loop to poll the button inputs and update a state variable. Then inside your timer interrupt handler you can examine the state variable to determine if an update of the internal and/or displayed time is necessary. 4 Prelab You may re-use/modify code from the GPIO, timers, and any other training labs. You will want to download and review pertinent sections of the MCF52259 Reference Manual for specific I/O control register addresses and bit definitions. Last Revised: 4/4/2012 3
5 Procedure 1. Download the skeleton source code from Blackboard. 2. Complete the init_gpio() function provided to set up the appropriate output ports for the digit displays and sets their direction to output as well as the input ports for the pushbuttons (SW1 and SW3). 3. Use/alter code from your previous labs as a basis for completing the rest of the lab. You will need to integrate code from the timer lab to use PIT0 to generate an interrupt every 0.1s, code to poll the button inputs, and code for the main program that contains an infinite loop, simply responding to interrupts or I/O. 4. An array lut (look-up table) has been defined to translate a single decimal digit input to the necessary 7-segment on/off outputs. By accessing lut[0], the bit pattern for representing 0 on a 7-segment display will be returned. We have also defined symbolic names STOPPED, STARTED, LAP for you to use to track what state you are in. However, you are not required to use any of this. It is completely at your discretion in deciding what to use. 5. Your program should meet the following requirements: a. Initialize the count to 00:0 on startup. b. Correctly display all times on the 7-segment displays. c. Starting counting in tenths of seconds when SW1 is pressed and the timer is stopped. d. Stop counting in tenths of seconds when SW1 is pressed and the timer is started. e. Hold the displayed time while continuing internal time updates when SW3 is pressed and the timer is started. f. Update the display with the current internal time and continue counting when SW1 or SW3 is pressed and the display time is being held (i.e. LAP state). g. Reset the time to 00:0 when SW3 is pressed and the timer is stopped. 6. When you are satisfied with your code and it compiles, you may test it on one of the development boards we will provide in lab/class. However, without the development board you can still get an idea if the internal time is being kept correctly by setting a breakpoint in whatever handler updates the time and displays it. View the PORTTH and PORTTG register values via the debugger as well as your time variables/array. This is because the switches are on your individual board and thus all input stimulus can be exercised; just the output displays are missing. 7. Demonstrate your project to an instructor/ta and get their initials on your rubric. 4 Last Revised: 4/4/2012
8. Comment your code with enough information to convey your approach and intentions. Try to organize your code in a coherent fashion. 9. Submit your source file, via Blackboard (Assignments..Labs..Lab 4) attaching only the main.c file in the sources folder of your project. Make sure you click Submit on Blackboard and not just Save. Also, turn in a hardcopy of your main.c file to your TA. Last Revised: 4/4/2012 5
6 EE 357 Lab 4 Grading Rubric Name: Score: / 75 TA Initials: Req. / Guideline Req. 5a 1 Mult Score 4 (Excellent) 3 (Good) 2 (Poor) 1 (Deficient) (0) Failure Always Works Occasional Failure Does not work Req. 5b 2 Req. 5c 2 Req. 5d 2 Req. 5e 3 Req. 5f 3 Req. 5g 2 Req. 7 (demo) Req. 8 2 2 pts. Wellorganized and readable and wellcommented Wellorganized with acceptable formatting and comments Hard to read or poorly organized but commented well Hard to read or poorly organized and with poor commenting Extermely poor organization and formatting Hard Copy 5 pts. Late -10 per day TOTAL 6 Last Revised: 4/4/2012