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 theHello 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 theStart Debugging button or pressingF5.
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 theStep 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 toRefactor > Extract Method.
AExtract Method dialog will appear. Name the functionLED_on.
ClickOK and the code should change. A new function calledLED_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 implementLED_off().
6
Introduce Variable
Next, it is necessary to create a variable for the SW0 state. Highlight the condition inside theif() in themain() while(1) loop. Right-click and go toRefractor > Introduce Variable.
TheIntroduce Variable dialog will appear. Name the variableuint8_t SW0_state.
ClickOK and the code should change. The condition inside theif() statement should now reference a variable assigned to the variable on the line above it.
7
Select the right side of theSW0_state assignment and extract a method forSW0_get_state.
Change the automatically generatedbool return value touint8_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 theLED_set_state() function, right-click on theSW0_state variable and go toRefactor > Rename.
10
TheRename dialog will appear. Rename theSW0_state variable tostate. 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 ofLED_set_state() and all of its references inside the function have been renamed, but the references toSW0_state inmain() have remained the same.
11
The updatedmain() 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 intoLED_set_state().
14
Press 'Reset' and observe how this causes the device to reset and halt code execution at the beginning ofmain().
15
Place the cursor at theLED_set_state() line and click theRun 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.
















