ECE 1200: Computer Engineering 101
Pre-Lab 4
02/12/2026
Introduction to C Programming in Atmel Studio 7
Objective
To become proficient in using a development IDE for writing, simulating, and debugging software
on a target processor. To learn the fundamentals of C programming and understand how C code
is translated into assembly and machine code, laying the foundation for deeper exploration of
embedded systems programming.
Before Lab Day
• Watch your instructors video on Atmel Studio.
• You do not have a handwritten Pre-lab for this lab like Labs 2 and 3.
• This Pre-lab will be done in the laboratory, and the solutions put into a Canvas Quiz.
Background
Software development for an embedded system takes several steps: requirement refinement from the
client, resource requirement estimation with your team, processor/microcontroller selection, tool
chain selection, code system design and testing, code system verification, and system integration.
Requirement refinement is the process of looking at the customer’s requirements, changing
these from performance requirements to software requirements and negotiating with the customer
so that your interpretation of the software requirements meets their expectations for the system.
Resource requirement estimation requires analyzing the software requirements and estimating
how big a computing system will need to be to meet the requirements. Things to consider are
compiled code size, size of variables required, speed of execution, and the number and type of
input/output channels. Platform selection is the identification of processors and single board
computers that meet the estimated requirements. A good method is to identify a processor line
that has several family members and a range of resources in case your estimation is off. Once
a processor family is selected, a tool chain for that processor must be selected. The tool chain
will include a code entry tool, a compiler, and generally a debugger. The tool chain may include a
simulator. The simulator allows for some or all of the code to be simulated without the requirement
for running of the actual hardware. Simulating has the feature of allowing all parts of the processor
to be monitored while the software is being simulated. This is different from the executing in target
hardware where only some parts of the processor can be monitored. Debugging with a hardware
1
debugger is similar to simulating where the software is being executed on target hardware, but
can be paused while certain parts of the processor can be monitored. Debugging by monitoring
outputs from the running program gives the smallest view of what is happening inside the processor.
The remainder of this lab will use the Atmel (now Microchip) ATMega family of processors,
currently the ATMEGA328PB and the Atmel Studio 7 development tools. Microchip studio
supports software based simulation and hardware in the loop debugging. On the tool ribbon you
should see a hammer icon. Clicking on this icon takes you to the menu that allows you to pick a
simulator or hardware in the loop debugger. If you have an Xplained Mini plugged in, it should
appear in the drop down menu. If you do not have an Xplained Mini than you can select software
simulation and perform most of the exercises.
Microchip uses GCC (GNU Compiler Collection) as its compiler. This compiler is a
powerful, open-source compiler system widely used in both academic and professional settings for
developing software across various platforms. Originally designed for Unix-like systems, GCC has
become the standard compiler for many embedded systems, including microcontrollers such as the
ATmega328PB. GCC supports a variety of programming languages, with C and C++ being the
most commonly used in embedded systems development. The compiler’s robustness comes from its
ability to perform extensive optimizations that can enhance code performance and reduce memory
footprint, which is particularly valuable in resource-constrained environments like embedded systems. Additionally, GCC includes a suite of tools for compiling, assembling, linking, and debugging,
making it a comprehensive solution for software development. In Atmel Studio 7, GCC is integrated
to compile C code into machine code that can be directly executed by the microcontroller, ensuring
efficient and reliable software execution.
Compiler optimizations can be controlled with compiler settings. These settings can be controlled by setting flags sent to the compiler during execution (command line flags) or through
directives in the source code. To set command line flags: Project → Settings → Toolchain
→ AVR/GNU C Compiler → Optimization. Setting the optimization on the command line
sets the optimization for the entire program. Another method of controlling optimization is to set
them in the function prototype: int main(void) attribute ((optimize(0)));. This method
of setting optimization only affects those functions that are decorated with this option.
Pre-Laboratory
Program 1
1. Start a New GCC (C or C++) Executable Project:
• In Atmel Studio 7, create a new project that will be written in C or C++ and compiled
using the GCC compiler.
• How to Do This: Go to File → New → Project.
• Save the new project in your Documents directory on your computer as Lab4. This
ensures you have easy access to it and helps keep your files organized.
• Select: GCC C Executable Project
• In the Device Selection pop-up, Select: the ATmega328PB as your target device.
2. Enter Program 1:
Fall 2024
Page 2
1
# include < avr / io .h >
2
int main ( void )
{
5
int count = 0;
3
4
6
/* Replace with your application code */
while (1)
{
count = count + 1;
}
7
8
9
10
11
12
}
• In your new project, the program window should show you starting code within the
main.c file. If not, open the main.c file within the Solutions Explorer window.
• Figure 1 shows the Solution Explorer window. If you do not see it, you must turn it
on with the following in the task bar: View → Solution Explorer
• Write the code above into your new project.
Figure 1: Zoomed in view of Solution Explorer. This window lists the files that will be included
when you compile code.
3. Simulate Program 1 - Broken with Optimizations:
• Run a simulation of the program within Microchip Studio to see how it behaves. You
will have trouble running debug due to the optimizer.
• How to do this: select no tool in the task bar as shown below. Use the scroll down in
the next window,labeled Selected debugger/programmer, to select Simulator.
• Click the green triangle to start debugging; when you hover the mouse over the icon it
should show the label Start Debugging
4. Pause Program:
• Once you click the green button. Atmel Studio will save all files and configurations then
compile files listed in the Solution Explorer. At this point, you should not see anything
happen; the green arrow becomes un-clickable while a pause button becomes clickable
just to the left. Figure 3 shows what you should see now.
Fall 2024
Page 3
Figure 2: Arrow points to the Debugger mode that will be set to Simulator for this Pre-lab assignment.
Figure 3: What you should see after clicking green arrow.
• While the simulation runs, pause the program execution to inspect its current state.
• How to Do This: When your mouse hovers over the correct icon, you will see the label
Break All; click this button.
5. Watch the Processor Registers:
• Monitor the values of the processor’s registers (such as general-purpose registers, the
program counter, etc.) to understand how the program affects the microcontroller’s
state.
• How to Do This: Open the ”Processor Status” or ”Registers” view in Microchip Studio’s
debugging interface to watch the values change as you step through the code; in the task
bar click Debug → Windows → Processor Status.
• Next, you will be using the code stepping tools. Figure 4 shows these tools and you will
click the icon with the semi-circle arrow (Step Over). Step Over executes one line of
code at a time without entering function calls, while ”Step Into” will enter functions to
allow line-by-line debugging within them.
Figure 4: The code stepping tools Step Into, Step Over, and Step Out, respectively.
• Repeatedly click the Step Over icon, while watching the Processor Status. What two
values are changing? Do you see the yellow arrow in the main.c window moving at all?
These will be quiz questions.
Fall 2024
Page 4
6. Add count to the Watch Window:
• Add count to the watch window to monitor its value during execution. This helps you
observe how the program modifies count over time.
• How to Do This: Right-click on the variable count in your code, and select ”Add to
Watch” from the context menu. Alternatively, you can manually add it by typing count
into the watch window.
7. Single Step:
• You should see a watch table with column categories Name, Value, and Type, respectively. Figure 4 shows what you should see. I have whited out some cells.
Figure 5: Normally, the watch window is pinned at the bottom left of the Atmel Studio window.
Some values are whited out for quiz questions.
• Repeatedly click Step Over button in the debugging toolbar, while watching the count
variable in the watch window.
• This is where you will start noticing that you have issues, nothing is changing. You will
have a quiz question on what you see in the Type column.
• What happened? GCC did not detect a return or use for the variable count, so it deleted
the variable to save memory. I will post, in Announcements, Mario 64 optimizer mode
release in the US on You Tube for an interesting story about this topic.
8. Simulate Program 1 - Broken without Optimizations:
Figure 6: This figure shows where the optimization flag can be controlled. You will keep optimization off for the entire semester.
• You will need to turn off optimization to observe the watch change values.
• How to Do This: Compile the program without optimizations enabled. In Microchip Studio, you can control optimization settings through Project Properties → Toolchain
→ AVR/GNU C Compiler → Optimization. See figure 6, you will select None.
Fall 2024
Page 5
• In the gray column, click to the left of the int count = 0 row. Figure 7 is an illustration
of what you should see after clicking.
Figure 7: You should see a red circle where you click in the gray area. When you click the green
arrow, you should see a yellow arrow fill the circle. This indicates where the program will start.
• When you see the red circle, click on the green arrow again to start debugging. Now the
code will start at the red circle.
• Look at the Watch window before clicking the Step Over button. You should now
see an entry for the variable count with some value. On the ATmega328PB, this
variable, count, is stored in a specific location in register. The name count is simply a
human-readable label—the compiler and debugger map to an address in memory. Until
the program explicitly initializes it (for example, setting it to zero), the register location
may contain an undefined value left over from whatever data previously occupied it. This
is why uninitialized variables often appear as random or “garbage” values at startup.
• Click Step Over, you will notice the watch value for count is now 0. Keep clicking and
you will notice the yellow arrow entering the while loop. The yellow arrow represents
the current state of the program.
• You should see count incrementing now. You will be asked to submit a snip of a watch
value in a Canvas quiz.
• Now look at the Processor Status window again. You will see a list of registers
labeled R00 through R31. One of these registers will change when your variable
count is updated. Registers are special high-speed storage locations built directly into
the CPU core of the ATmega328PB. Because they are physically very close to the
arithmetic logic unit (ALU), the processor can read from and write to them in a single
clock cycle.
• You will need to provide a snip image of the Processor Status window after some
increments of your choice. This image can be imported into your quiz assignment on
Canvas.
Fall 2024
Page 6