Objective
In the code comments from the "Hello World Project" page, the assumption was made that the LED is turned on by driving the connected GPIO pin high. This lab uses code stepping to check if this assumption is correct.
This page assumes that you've already completed the Hello World Project. If you haven't yet done so, please go through those steps before continuing with this exercise.
Materials
Hardware Tools
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
Atmel® Studio
Integrated Development Environment |
| | | | |
Procedure
The key buttons used for code stepping are illustrated below, found in the top menu bar or in the 'Debug' menu. The corresponding functionality and keyboard shortcuts are outlined in the table.
Button | Functionality | Keyboard Shortcut |
---|---|---|
Step Into Function Call | F11 | |
Step Over | F10 | |
Step Out of Function Call | Shift + F11 | |
Run to Cursor | Ctrl + F10 | |
Issue System Reset |
1
Start Debug Session
Start a debug session by clicking the Start Debugging button or pressing F5.
If you're coming from the "Hello World Project" page, you don't need to start a debug session unless you ended the previous one.
3
Step Through Code
Step through the code by clicking the Step Into Function Call button to discern whether the comments reflect the code.
4
Extract Method
The result should be that the LED drive assumption is proven wrong, and the "on" and "off" comments are swapped. The line of code commented with /* Turn LED off */ is executed when SW0 is pressed. Highlight this line of code, right-click and go to Refactor > Extract Method.
A Extract Method dialog will appear. Name the function LED_on.
Click OK and the code should change. A new function called LED_on should appear at the top of the file, with a function call where the line of code used to be.
5
LED_off()
Use the same method to implement LED_off().
6
Introduce Variable
Next, it is necessary to create a variable for the SW0 state. Highlight the condition inside the if() in the main() while(1) loop. Right-click and go to Refractor > Introduce Variable.
The Introduce Variable dialog will appear. Name the variable uint8_t SW0_state.
Click OK and the code should change. The condition inside the if() statement should now reference a variable assigned to the variable on the line above it.
7
Select the right side of the SW0_state assignment and extract a method for SW0_get_state.
Change the automatically generated bool return value to uint8_t to avoid having to include an extra header to deal with boolean values.
9
In a larger application, this function may be used for setting the LED state in a context that is irrelevant to the SW0 state. Atmel Studio is capable of contextual renaming, so this feature can be used to easily rename the argument and avoid confusion. Inside the LED_set_state() function, right-click on the SW0_state variable and go to Refactor > Rename.
10
The Rename dialog will appear. Rename the SW0_state variable to state. Atmel Studio will detect all occurrences of the variable with the same context as the one which has been selected, which are presented in a list and able to be individually selected or deselected.
Click 'Rename' and the code should change. Observe that the argument of LED_set_state() and all of its references inside the function have been renamed, but the references to SW0_state in main() have remained the same.
11
The updated main() should now look something like this:
Click 'Start Debugging and Break' to recompile and start a new debug session.
12
Use 'Step Over' to show how the device is able to execute a function call before code execution is halted on the line of code following the function call.
13
Use 'Step Out' to show how the device can complete the current function call before code execution is halted on the line of code following the function call. This is best observed after stepping into LED_set_state().
14
Press 'Reset' and observe how this causes the device to reset and halt code execution at the beginning of main().
15
Place the cursor at the LED_set_state() line and click the Run to Cursor button to show that this causes the device to halt execution when the line is hit for the first time.
Results
The code is now more readable, and Atmel Studio has been used to step through code running on the ATtiny817.