1.1 Atmel Studio

1.1.0

This tutorial will extend the previous tutorial and explain the Atmel tools required to write in C language a program and flash it into the microcontroller. Atmel Studio under Windows will be selected for such purpose.

Atmel is one of the few big microcontroller vendors that provide all the software tools to work with their microcontrollers for free. This tool set (editor, C/Assembler compilers, linker, etc..) is integrated within Atmel Studio, a cost-free professional development environment for their microcontrollers which can be downloaded from this link.

The first thing we have to do to prepare our host machine is to install Atmel Studio, once the installation is successfully completed we will talk through the different steps to build a program in C and downloaded to the microcontroller.

The microcontroller chosen is the one in a ecTiny841, a low cost ready to fit in a breadboard with an 8 bit Atmel microcontroller.

ecTiny841

ecTiny841

Once Atmel Studio is installed, let’s execute it and the following window will show up.

Atmel Studio. Click on New Project.

Atmel Studio. Click on New Project.

In the menu above we shall click on New Project as a starting point, a new window will pop up inquiring for the type of project to create.

Select “GCC C Executable Project” name it and choose the folder that will contain the project files.

Select “GCC C Executable Project” name it and choose the folder that will contain the project files.

We are going to write a C program that will contain the task to run by our microcontroller within the electronic circuit, there in the configuring window we will select “GCC C Executable Project”, name it as desired and place it in a particular folder to store it in our PC. For this example, we will name it as default and accept by clicking on OK, next step we will be asked what microcontroller we want to work with.

ATtiny841 selection.

ATtiny841 selection.

ATtiny841 should be selected since is the microcontroller present in the board we will use during this example. Click on OK and the project window will show up right after, that will be the editor where we will type our code that will get built and subsequently burnt into the microcontroller to execute a sequence of embedded instructions many times per second.

Project Window.

Project Window.

We are going to write a simple program to blink an LED at 1 Hz, an embedded hello World! Just to verify that we have set everything correctly, we have good communication with our target microcontroller and this one behaves as expected.

We can type the lines listed below in our source file GccApplication1.c:

#include <avr/io.h>
#define F_CPU 8000000UL			//clock frequency
#include <util/delay.h>
 
#define ledPin PORTA0
 
int main(void)
{
    DDRA |= (1<<ledPin);		//output pin
 
    while(1)
    {
		PORTA ^= (1<<ledPin);	//Toggle led
		_delay_ms(500);		//Delay 0.5 s
 
    }
}

Understanding the individual meaning of every line is not necessary and goes beyond the scope of this tutorial which is to install the development environment into a PC, build a program and download it into the microcontroller memory space to check that everything works fine. The previous code will be discussed in the subsequent tutorials.

Microcontroller C code.

Microcontroller C code.

Once we have typed our software Atmel Studio needs to build a file that our microcontroller would understand to run the tasks in question. If we press F7 or click on “Build Solution”, Atmel Studio then compiles the code and generates the file with the machine code instructions to download to the microcontroller.

Press F7 to create the microcontroller binary file.

Press F7 to create the microcontroller binary file.

At the bottom of the Atmel Studio environment there is a log with relevant information about the compilation and link steps taken to generate the binary file. If the steps have been followed correctly, a message stating that the code was built successfully should be visible, we should also see how much program memory (FLASH) and data memory (SRAM) will be taken up once the microcontroller executes the instructions we have just generated. As a rule of thumb we should not exceed 70% of “Data memory Usage” (SRAM) since the stack could then access memory addresses that are in used and then bad things will happen. (More about this in the next tutorials).

The file that we will burn into the microcontroller has the .hex extension and should be part of the build solution in our project folder.

.hex file that will reside in the microcontroller.

.hex file that will reside in the microcontroller.

In order to download the hex file into the microcontroller we will require a programmer.

A hardware programmer connects between the PC and the programming pins of the microcontroller and it does allow to change the state of the non volatile internal memories (FLASH and EEPROM), to read the memory contents as long as there is no protection applied to the microcontroller and change the fuse configuration.

Atmel AVRISP mkII programmer and JTAGICE3 programmer/debugger.

Atmel AVRISP mkII programmer and JTAGICE3 programmer/debugger.

Possibly the most affordable Atmel options that will be fully integrated in Atmel Studio to program the microcontroller and that we can get from the major electronic distributors or Atmel’s website are:

The difference between both is that, the debugger will additionally work as a programmer, a debugger will let us watch the value that internal registers and variables have based on the context of the microcontroller. There are features like setting breakpoints, step by step execution, etc…

When our microcontroller code malfunctions because we have a made an unintentional mistake, software bug, a debugger is one of the most useful tools to help us find those bugs, it could save hours and hours of troubleshooting when there are many lines of code to review. It could be the most appealing option to buy if you are thinking of using Atmel microcontrollers repeatedly.

The programmer or debugger will connect to the conveniently located 6 pin connector on the ecTiny841 top layer.

ecTiny841. Programming connector on top.

ecTiny841. Programming connector on top.

On the programmer side the ribbon cable is normally labeled with some sort of indicator (line, colour, number,…) That identifies where the red mark goes and corresponds with the first pin.

The read mark in lined up with the mark in the connector socket on the board.

The read mark in lined up with the mark in the connector socket on the board.

We will plug the programmer/debugger into a USB port and we will need to supply power to the board. One we know that the IDE has acknowledge the programmer we can hit the “Device Programming” button at the top right section.

Device Programming.

Device Programming.

Then a window will pop up asking about the programmer we have just plugged into the computer, to acknowledge the selection we can click on apply.

Select device and programming tool and apply.

Select device and programming tool and apply.

Next, to burn the .hex file Atmel Studio generated previously we can click on the Memories tab, and in the Flash memory selection menu we will browse to locate the .hex file the compiler just produced during the build and then we are ready to click on Program.

Select the .hex to program into the Flash memory.

Select the .hex to program into the Flash memory.

If the steps have been followed thoroughly, then the LED connected to pin A0 in the ecTiny841 board will commence blinking at 1 Hz. As a proof of having coded and programmed correctly our software that is running on the microcontroller

The other option would have been to used the debugger instead, in that case the steps are identical as described above, including the connection interface.

Nonetheless, the debugger also lets us see what goes on in the microcontroller while this one runs the instructions, to check that out we would need to have the debugger connected and debug option should be enabled in our microcontroller. To set it up we need to change the fuse value, if the same instructions to program the microcontroller are followed, then instead of click on the “Memories” tab we go into the “Fuses” tab.

Debug interface. Enable FUSE DWEN.

Debug interface. Enable FUSE DWEN.

IMPORTANT: Without altering any other settings we check the DWEN box and right after that click on program, we can either complete this with a programmer or debugger, but ever since the DWEN option is programmed in the microcontroller, it will only communicate to the debugger solely and hence will not to a programmer. A debugger will be strictly required to disable the DWEN fuse option.

Once the debugWire interface is enabled the ISP stops working.

Once the debugWire interface is enabled the ISP stops working.

Then we will close the window and press F5 or “Start Debugging” in Atmel Studio, we will select the debugger that is connected to the debugWire interface in the new window that opens up.

Debugger and debugWIRE selection.

Debugger and debugWIRE selection.

We press F5 or click “Start Debugging” again to go into debug mode.

Debug mode.

Debug mode.

In debug mode we can check on what the microcontroller is doing at a greater detail while it executes the recently burnt code, watch register values, input/output ports, memories, etc… It is not difficult to appreciate how useful a debugger becomes when there is an error in our code and the execution get erroneous.

To exit debug mode and disable the DWEN fuse is as easy as selecting “Disable debugWire and Close”

Debug interface disabling.

Debug interface disabling.

If now we go back to the fuses menu and perform a Read then we can check that the DWEN option has been disabled, therefore the board could be use with a programmer via the ISP from next time onwards.

Fuse reading.

Fuse reading.

These are the tools needed to work with Atmel microcontrollers. The IDE is totally free and very complete, if Windows is our OS will be the recommended choice. The hardware programmers costs a few tens of $ but they are completely integrated, and if possible the debugger would be suggested over the programmer only option.

There are third parties that offer cheaper programmers, there is even the possibility of using an Arduino as a programmer, we will talk about it in the next tutorials since it could be a convenient option. But would like to stress that if we want to take the world of programming microcontrollers seriously and spend a lot of fun time working with them, the most sensible approach would be to purchase a debugger designed by Atmel.

Related Links: