Code Stepping in Atmel Studio


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.


Hardware Tools

Software Tools

Tool About Installers
Windows Linux Mac OSX
Atmel® Studio
Integrated Development Environment


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


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.


Halt Execution

Click the Break All button to halt execution.

The yellow arrow on the left edge of the editor window points to the line of code that is about to be executed.



Step Through Code

Step through the code by clicking the Step Into Function Call button to discern whether the comments reflect the code.


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.



Use the same method to implement LED_off().


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.


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.


Now it is possible to implement a method for void LED_set_state(uint8_t state). Extract the method. Atmel Studio will detect the argument SW0_state.


Click OK and the code should change. Now there is a separate method for setting the LED state.


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.



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.


The updated main() should now look something like this:

Click 'Start Debugging and Break' to recompile and start a new debug session.


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.


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().


Press 'Reset' and observe how this causes the device to reset and halt code execution at the beginning of main().


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.


The code is now more readable, and Atmel Studio has been used to step through code running on the ATtiny817.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.