Lab 2, CMPET 211 – Introduction to Digilent Orbit Booster Pack SUM 15, ADH Lab Summary This lab will introduce the use of the Digilent Orbit Booster Pack, which is a daughter card to be with the Tiva LaunchPad Microcontroller. This lab will reinforce the concept of enabling ports and setting I/O pins, and will provide opportunities for writing C code, understanding what the C code is doing, and debugging the C code. There is an open ended component of the lab which will be used for grading through demonstration of various LED controls through switches and push buttons. Learning Objectives 1. Students will become familiar with the Digilent Orbit Booster Pack used to expand capabilities of the Tiva LaunchPad microcontroller 2. Students will continue to work with and increase familiarity with the CCS IDE, the C programming language (with the introduction of conditional If-Else statements and While loops), and TivaWare library functions. Lab Materials and Software Students will need a Digilent Orbit Booster Pack daughter card, a Tiva TM4C123GXL LaunchPad microcontroller by Texas Instruments, the supplied USB cable and access to Tiva TM4C123 software. The hardware will be provided by the instructor in Week 3. Students should have their own LaunchPad by week 4 (Lab 3). Students need access to CCS, TivaWare, and LMFlashProgrammer software. Setting up the Hardware and Launching the Software Connecting the Tiva LaunchPad 1. Very carefully connect the USB cable to the microcontroller USB port labeled “Debug” (Note: there are two USB ports, the correct one has a small microprocessor board next to it). It is labeled “Debug” and it is NOT labeled “Device.” a. PLEASE be VERY careful when inserting the USB cable – the female connection is VERY fragile. It is a good idea to hold the female USB connector firmly in place while inserting the cable. Do not twist the USB connections, just insert straight in. 2. Ensure the switch next to the on-board USB port is set to “DEBUG” and not “DEVICE”. 3. Connect the other side of the USB cable to an available USB port on the PC. Connecting the Digilent Orbit Booster Pack to the Tiva Launchpad To connect the Orbit Booster Pack, connect the Orbit’s female headers to the Launchpad’s male headers. IMPORTANT: make sure the text “Orbit BoosterPack” is in the same orientation as the “Tiva LaunchPad” text. See Figure 1. Figure 1. ARM and ORBIT Combination. Note that the text at the bottom of each board is in the same orientation. The Digilent Orbit Booster Pack is similar to a “shield” for Arduino hobby MCUs. The Orbit Booster Pack is equipped with an accelerometer, temperature sensor, several LEDs, push buttons, switches, a potentiometer, and an OLED screen. Today we will use the Orbit Booster Pack to further explore setting up I/O pins and controlling LEDs with Switches and pushbuttons. Launching CCS 1. In Lab 1 you created a “workspace” folder called “CMPET 211 Labs” on your USB or pass space. Make sure this folder exists, or recreate it (IMPORTANT NOTE: if you recreate the folder, you NEED to also download Lab 1 from the website, and copy it into this folder—it contains additional support files needed today for Lab 2). 2. Use the internet to browse to http://sites.psu.edu/hilshey/classes/cmpet211/labs/ and download the CMPET 211 Lab 2 zipped folder. 3. Copy the contents of the Lab 2 zipped folder into your workspace folder (see step 1) DO NOT CREATE A SECOND “Lab 2” folder! 4. Launch Code Composer Studio (the shortcut looks like a grey 3x3x3 matrix cube) 5. Set the workspace to the folder described in step 1 (Figure 2). Figure 2: Setting the CCS workspace. 6. The file “main.c” from last week may open within the CCS IDE. In the project tree, drill in to “Lab 2 CMPET 211” folder, and click on “main.c” to open the new lab 2 source code. Note 1: if you don’t have a project tree, try two things. First click on the “CCS edit” icon at the top right side of the CCS window. If this doesn’t work, select the following using the drop down menus: WindowReset Perspective… Note 2: If you don’t see Lab 2, click ProjectImport CCS Projects. Browse to the Lab 2 project folder, select it, and click finished. 7. Your CCS window should look similar to Figure 3 (you may have some different project folders compared to the figure, but you will have “Lab 2 CMPET 211”). Figure 3: The CCS IDE with Lab 2 source code open Doing the Lab, Writing the Code This lab is designed so that the you will type a good amount of C code. Although, you will not be writing your “own” code yet, there is value in retyping prewritten code. This forces you to look at the prewritten code more carefully which may probe you to ask yourself, “what does this line do?” Additionally, there is a benefit in typing lots of code, in that you are bound to make a few typos. Typos are great opportunities to learn debugging; typos aren’t necessarily a bad thing when we’re learning! Only by typing the code and then debugging the errors will a user really understand how to do projects. Note: You will start typing code after Figure 6 below. The sections and figures will describe the functions of the prewritten, provided code. Side Note: Pair Programming You are likely working with a partner or even two. While one writes code, what does the other do? This is an opportunity to apply “Pair Programming.” Here are some salient points taken from Seattle Consultant David Bernstein’s software development blog (tobeagile.com) post “Seven Strategies for Pair Programming”: Engage Driver and Navigator: Pairing is not about taking turns doing the work. Each member of the pair has specific duties, working together, and in parallel. Both the person at the keyboard (driver) and the one looking over their shoulder (navigator) are actively engaged while pairing. Swap roles frequently: Taking turns driving and navigating every 20-60 minutes helps keep collaboration at a maximum. “Handing off the keyboard” also helps propagate knowledge across the pair and ultimately, by pairing with other team members, propagates knowledge throughout the team. Many hyper-productive teams I know pair all of the time. Don’t believe that pairing cuts productivity in half, which would only be true if the limiting factor in software development is typing. When done correctly, pairing can significantly increase the throughput of value created while slashing defects. I know of no other approach to software development that can radiate knowledge and skills throughout a team more rapidly than pairing. Writing and understanding the code Figure 4 displays the provided header files. Recall from Lab 1 that Header files contain all functions you may need to create a project. When using our Tiva LaunchPads, always include these 9 header files. Remember that inclusion of all these lines of code doesn’t necessarily mean that all the code will be part of the machine code. The header files contain all the “possible” functions that will be necessary. When the compiler actually accomplishes the work, it will only include the functions that are required. Figure 4: Header Files to be used with Tiva LaunchPad The next section of provided code (similar to Lab 1) will set up the internal clock (here used to create time delays) and will then set up our Input/Output (I/O) pins. Recall that we first enable ports (each port has a collection of I/O pins) which is similar to “powering them up.” Then we define the pins we want to use as either outputs or inputs. See Figure 5. Interesting note on setting up I/O: The ARM processor is ultimately designed to be highly efficient and to be able to operate for months at a time on batteries. To accomplish this, the device is virtually powered down when it is turned on. The device has a series of ports that then connect to external devices such as UARTs and I2C busses (discussed in future labs/lectures). EVERY TIME a new project is started, the first thing than must be done is to first enable the ports and then set up the pins. Unused ports are never powered; this is how we save energy! Figure 5. Enabling Ports and Configuring Pins As shown in Figure 5, the first port to be enabled is port C (GPIOC). Once the port is enabled, the next step is to configure the pins. In this case, pins 6 and 7 are enabled as output pins. As another example, the bottom line, Port A, Pin 6, is configured as an input pin which will take input readings from Switch 2 on the Orbit Booster Pack. Review Appendix 1 to see which ports house the pins that are tied to the devices we are using. The next lines will declare variables (tells the compiler to create a variable) and then will define them (stores the desired value in the variable). See Figure 6. Figure 6: Declaring and defining a variable Following the variable declaration and definition, you will see a conditional “while” loop in your provided code (the full contents of this loop is not yet written). The while loop will perform its contents (i.e. reading inputs and setting outputs) indefinitely because its condition is set to “1,” which is always true (0 is false, everything else is true, including 1). Nested within the while loop is an If-Else conditional statement and another embedded conditional while loop. The “If” conditional statement “listens for the switch.” When the I/O pin tied to the Orbit Switch 2 reads high (e.g. when Port A, Pin 6 is high), it will perform the commands within the “if” statement. Otherwise if the I/O pin reads low, the “else” conditional statement is executed. The “while” conditional statement (embedded within the first while loop) will be executed if the Orbit Push Button 1 is depressed (i.e. while the pin tied to the push button reads high, execute the commands within the while loop). Now it’s time to write code. Fill in the commands for the If-Else and While statements as shown in Figure 7 and 8. Use Pair Programming technique such that one student types and the other verifies that the typing is correct. Pay attention to case sensitivity. Switch roles at each conditional statement (i.e. one does the “if,” one does the “else,” etc.). Deliverable Question as Part of Graded Demonstration Later, during demonstration of your work, you may be asked to explain what is happening in any of the GPIOPinWrite or GPIOPinRead lines of code shown in figures 7 and 8. You will need to be able to explain why every part of that line is present. These lines of code (or ones just like them) were described in Lab 1. HOT TIP: A phenomenal resource for learning more about any of the common Tiva commands is the TiveWare™ Peripheral Driver Library User’s Guide, which can be found by navigating to C:\ti\TivaWare_C_Series-2.1.1.71\docs and selecting “SW-TM4C-DRL-UG-2.1.1.71” or googling the above document title. Use “control f” to search for the commands you want to know more about. This resource (among others) is also available on Hilshey’s website under the “useful downloads” page. Figure 2. The "IF" Work Figure 3. While Loop for Blinking Lights As shown in Figure 8, if the button is pushed, we write outputs to all the LEDs First we write 1s and then after a little delay time we write 0s. This is repeated until the button is released. Now it is possible to compile the program, create the executable, and run the program. On the tool bar, directly under the “Project” drop down menu, locate and click the “debug” icon (it looks like a green beetle -- see Figure 9). Do not click the drop down arrow next to the debug icon, just click the beetle. This causes the main.c source code to compile and then to be burned onto the microcontroller ROM. If you get errors, skip to the next page. Figure 9: Compiling code and opening the debugger. There are now several options. The designer can step line by line through the program by clicking on the yellow “step over” icon (it is arrow shaped). When ready to continuously run the program, click on the green arrow “resume” icon (see figure 10) in the Debug pane. Figure 10: Running the code on the microcontroller. Click on the red square “stop” icon to return to the text editor. If your code is correct, when Switch 2 is in the Up position, the Orbit LEDs will flash from right to left and the LaunchPad LED will be blue (from the “If” statement). The Orbit LEDs change directions and the LaunchPad LED turns green when the Switch 2 is in the Down Position (from the “Else” statement). All LEDs flash when Orbit Pushbutton 1 is held down (from the “while” statement). HOT DEBUGGING TIPS: if you have “bugs” from typos, common errors stem from: 1.) unbalanced parenthesis or brackets; 2.) missing semi-colons at the end of commands; 3.) misspelled function or variable names; and 4.) incorrect letter case – C is case sensitive. An error icon (red circle with an “x”) will usually show up at the line with the error for misspellings or case errors. The error icon shows up at the command FOLLOWING a missing semi-colon, parenthesis, or bracket (so, look up!). Debugging gets easier with experience, but is not usually easy in the beginning! Refuse to get frustrated! Change the Code, Learn Through Doing Now is your opportunity to experiment. Start slowly and build up. You may want to make a copy of the “Lab 2 CMPET 211” folder in the “project explorer” tree. Don’t just copy “main.c” – copy the whole project folder to save headaches! This way you can preserve the original file and make changes in the copied file. Challenges: 1.) Remove the if/else statement, see results (you do not need to demonstrate this) 2.) Change from button 1 to button 2* 3.) Take your SOS code from Lab 1 (if you got that far – if not, make something similar now) and embed it into the Lab 2 with another “while” tied to Switch 1.* Keep the rest of the Lab 2 code the same. *When using the other switch or push button, you will need to refer to the LaunchPad Evaluation Board and the Orbit Schematics. There is a quick cheat sheet at the end of this tutorial that will provide ports and pins. Remember: Enable Ports, Set Pins. Demonstration/Grading To receive full credit, you must demonstrate at least Challenge 2 in the previous section. You may also be required to explain one or more lines of code. When you are ready to demonstrate, contact your instructor. This Lab should be demonstrated before leaving today. There will be no hard copy deliverables due for this lab. This lab is worth 50% of a standard lab (due to no hardcopy deliverables). If you choose to leave early, you must demonstrate challenge 2 and 3 to receive full credit. Appendix 1: Block Diagram of the Pins Used in Projects